Skip to content

Commit

Permalink
Merge pull request #1593 from jim-parry/testing/test
Browse files Browse the repository at this point in the history
Flesh out the Test package testing
  • Loading branch information
jim-parry authored Dec 9, 2018
2 parents 29ca3bc + 3e9d21c commit b6bc2fe
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 13 deletions.
2 changes: 1 addition & 1 deletion system/Test/CIUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function assertCloseEnough(int $expected, $actual, string $message = '',
* where the result is close but not exactly equal to the
* expected time, for reasons beyond our control.
*
* @param integer $expected
* @param mixed $expected
* @param mixed $actual
* @param string $message
* @param integer $tolerance
Expand Down
4 changes: 2 additions & 2 deletions system/Test/FeatureResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,15 @@ public function assertSeeInField(string $field, string $value = null)
/**
* Returns the response's body as JSON
*
* @return mixed|string
* @return mixed|false
*/
public function getJSON()
{
$response = $this->response->getJSON();

if (is_null($response))
{
$this->fail('The Response contained invalid JSON.');
return false;
}

return $response;
Expand Down
5 changes: 4 additions & 1 deletion system/Test/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FeatureTestCase extends CIDatabaseTestCase
/**
* Enabled auto clean op buffer after request call
*
* @var bool
* @var boolean
*/
protected $clean = true;

Expand Down Expand Up @@ -122,10 +122,13 @@ public function call(string $method, string $path, array $params = null)
->run($this->routes, true);

// Clean up any open output buffers
// not relevant to unit testing
// @codeCoverageIgnoreStart
if (ob_get_level() > 0 && $this->clean)
{
ob_end_clean();
}
// @codeCoverageIgnoreEnd

$featureResponse = new FeatureResponse($response);

Expand Down
39 changes: 39 additions & 0 deletions tests/system/Test/FeatureResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

class FeatureResponseTest extends CIUnitTestCase
{

/**
* @var FeatureResponse
*/
protected $feature;

/**
* @var Response
*/
Expand Down Expand Up @@ -44,6 +46,15 @@ public function testIsOKSuccess()
$this->assertTrue($this->feature->isOK());
}

public function testIsOKEmpty()
{
$this->getFeatureResponse('Hi there');
$this->response->setStatusCode(200);
$this->response->setBody('');

$this->assertFalse($this->feature->isOK());
}

public function testAssertSee()
{
$this->getFeatureResponse('<h1>Hello World</h1>');
Expand Down Expand Up @@ -78,6 +89,14 @@ public function testAssertDontSeeElement()
$this->feature->assertDontSeeElement('h1.para');
}

public function testAssertSeeLink()
{
$this->getFeatureResponse('<h1 class="header"><a href="http://example.com/hello">Hello</a> <span>World</span></h1>');

$this->feature->assertSeeElement('h1');
$this->feature->assertSeeLink('Hello');
}

public function testAssertSeeInField()
{
$this->getFeatureResponse('<html><body><input type="text" name="user[name]" value="Foobar"></body></html>');
Expand Down Expand Up @@ -147,6 +166,14 @@ public function testAssertHeader()
$this->feature->assertHeader('foo', 'bar');
}

public function testAssertHeaderMissing()
{
$this->getFeatureResponse('<h1>Hello World</h1>', [], ['foo' => 'bar']);

$this->feature->assertHeader('foo');
$this->feature->assertHeaderMissing('banana');
}

public function testAssertCookie()
{
$this->getFeatureResponse('<h1>Hello World</h1>');
Expand Down Expand Up @@ -184,6 +211,17 @@ public function testGetJSON()
$this->assertEquals($formatter->format(['foo' => 'bar']), $this->feature->getJSON());
}

public function testInvalidJSON()
{
$this->getFeatureResponse('<h1>Hello World</h1>');
$this->response->setJSON('');
$config = new \Config\Format();
$formatter = $config->getFormatter('application/json');

// this should fail because of empty JSON
$this->assertFalse($this->feature->getJSON());
}

public function testGetXML()
{
$this->getFeatureResponse(['foo' => 'bar']);
Expand Down Expand Up @@ -265,4 +303,5 @@ protected function getFeatureResponse($body = null, array $responseOptions = [],

$this->feature = new FeatureResponse($this->response);
}

}
25 changes: 18 additions & 7 deletions tests/system/Test/FeatureTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testCallGet()
'get',
'home',
function () {
return 'Hello World';
return 'Hello World';
},
],
]);
Expand All @@ -44,7 +44,7 @@ public function testCallSimpleGet()
'add',
'home',
function () {
return 'Hello World';
return 'Hello World';
},
],
]);
Expand All @@ -64,7 +64,7 @@ public function testCallPost()
'post',
'home',
function () {
return 'Hello World';
return 'Hello World';
},
],
]);
Expand All @@ -80,7 +80,7 @@ public function testCallPut()
'put',
'home',
function () {
return 'Hello World';
return 'Hello World';
},
],
]);
Expand All @@ -96,7 +96,7 @@ public function testCallPatch()
'patch',
'home',
function () {
return 'Hello World';
return 'Hello World';
},
],
]);
Expand All @@ -112,7 +112,7 @@ public function testCallOptions()
'options',
'home',
function () {
return 'Hello World';
return 'Hello World';
},
],
]);
Expand All @@ -128,7 +128,7 @@ public function testCallDelete()
'delete',
'home',
function () {
return 'Hello World';
return 'Hello World';
},
],
]);
Expand All @@ -137,4 +137,15 @@ function () {
$response->assertSee('Hello World');
}

public function testSession()
{
$response = $this->withSession([
'fruit' => 'apple',
'greeting' => 'hello',
])->get('home');

$response->assertSessionHas('fruit', 'apple');
$response->assertSessionMissing('popcorn');
}

}
30 changes: 30 additions & 0 deletions tests/system/Test/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
class TestCaseTest extends \CIUnitTestCase
{

// protected function tearDown()
// {
// $buffer = ob_clean();
// if (ob_get_level() > 0)
// {
// ob_end_clean();
// }
// }
//
public function testGetPrivatePropertyWithObject()
{
$obj = new __TestForReflectionHelper();
Expand Down Expand Up @@ -76,4 +85,25 @@ public function testPHPUnitHeadersEmitted()
$this->assertHeaderNotEmitted('Set-Cookie: foo=bar;');
}

//--------------------------------------------------------------------
public function testCloseEnough()
{
$this->assertCloseEnough(1, 1);
$this->assertCloseEnough(1, 0);
$this->assertCloseEnough(1, 2);
}

public function testCloseEnoughString()
{
$this->assertCloseEnoughString(strtotime('10:00:00'), strtotime('09:59:59'));
$this->assertCloseEnoughString(strtotime('10:00:00'), strtotime('10:00:00'));
$this->assertCloseEnoughString(strtotime('10:00:00'), strtotime('10:00:01'));
}

public function testCloseEnoughStringBadLength()
{
$result = $this->assertCloseEnoughString('apples & oranges', 'apples');
$this->assertFalse($result, 'Different string lengths should have returned false');
}

}
5 changes: 3 additions & 2 deletions user_guide_src/source/testing/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ between expected and actual time is within the prescribed tolerance.::
$timer->start('longjohn', strtotime('-11 minutes'));
$this->assertCloseEnough(11 * 60, $timer->getElapsedTime('longjohn'));

The above test will allow the actual time to be either 600 or 601 seconds.
The above test will allow the actual time to be either 660 or 661 seconds.

**assertCloseEnoughString($expected, $actual, $message='', $tolerance=1)**

Expand All @@ -163,7 +163,8 @@ between expected and actual time, formatted as strings, is within the prescribed
$timer->start('longjohn', strtotime('-11 minutes'));
$this->assertCloseEnoughString(11 * 60, $timer->getElapsedTime('longjohn'));

The above test will allow the actual time to be either 600 or 601 seconds.
The above test will allow the actual time to be either 660 or 661 seconds.


Accessing Protected/Private Properties
--------------------------------------
Expand Down

0 comments on commit b6bc2fe

Please sign in to comment.