Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test/featuretestcase #1977

Merged
merged 3 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,9 @@ public function map(array $routes = [], array $options = null): RouteCollectionI
* Example:
* $routes->add('news', 'Posts::index');
*
* @param string $from
* @param array|string $to
* @param array $options
* @param string $from
* @param array|string $to
* @param array $options
*
* @return RouteCollectionInterface
*/
Expand Down Expand Up @@ -1432,4 +1432,18 @@ private function determineCurrentSubdomain()
}

//--------------------------------------------------------------------

/**
* Reset the routes, so that a FeatureTestCase can provide the
* explicit ones needed for it.
*/
public function resetRoutes()
{
$this->routes = ['*' => []];
foreach ($this->defaultHTTPMethods as $verb)
{
$this->routes[$verb] = [];
}
}

}
9 changes: 6 additions & 3 deletions system/Test/FeatureResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
*/
class FeatureResponse extends TestCase
{

/**
* @var \CodeIgniter\HTTP\Response
*/
Expand All @@ -61,9 +62,10 @@ public function __construct(Response $response = null)
{
$this->response = $response;

if (is_string($this->response->getBody()))
$body = $response->getBody();
if (! empty($body) && is_string($body))
{
$this->domParser = (new DOMParser())->withString($this->response->getBody());
$this->domParser = (new DOMParser())->withString($body);
}
}

Expand Down Expand Up @@ -124,7 +126,7 @@ public function assertRedirect()
*/
public function assertStatus(int $code)
{
$this->assertEquals($code, (int)$this->response->getStatusCode());
$this->assertEquals($code, (int) $this->response->getStatusCode());
}

/**
Expand Down Expand Up @@ -397,4 +399,5 @@ public function getXML()
{
return $this->response->getXML();
}

}
25 changes: 23 additions & 2 deletions system/Test/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
*/
class FeatureTestCase extends CIDatabaseTestCase
{

/**
* If present, will override application
* routes when using call().
Expand Down Expand Up @@ -82,6 +83,11 @@ class FeatureTestCase extends CIDatabaseTestCase
* Sets a RouteCollection that will override
* the application's route collection.
*
* Example routes:
* [
* ['get', 'home', 'Home::index']
* ]
*
* @param array $routes
*
* @return $this
Expand All @@ -92,6 +98,7 @@ protected function withRoutes(array $routes = null)

if ($routes)
{
$collection->resetRoutes();
foreach ($routes as $route)
{
$collection->{$route[0]}($route[1], $route[2]);
Expand Down Expand Up @@ -149,18 +156,31 @@ public function call(string $method, string $path, array $params = null)
$request = $this->setupRequest($method, $path, $params);
$request = $this->populateGlobals($method, $request, $params);

// Make sure the RouteCollection knows what method we're using...
if (! empty($this->routes))
{
$this->routes->setHTTPVerb($method);
}

// Make sure any other classes that might call the request
// instance get the right one.
Services::injectMock('request', $request);
$_SERVER['REQUEST_METHOD'] = $method;

$response = $this->app
->setRequest($request)
->run($this->routes, true);
->setRequest($request)
->run($this->routes, true);

$output = ob_get_contents();
if (empty($response->getBody()) && ! empty($output))
{
$response->setBody($output);
}

// Clean up any open output buffers
// not relevant to unit testing
// @codeCoverageIgnoreStart

if (ob_get_level() > 0 && $this->clean)
{
ob_end_clean();
Expand Down Expand Up @@ -311,4 +331,5 @@ protected function populateGlobals(string $method, Request $request, array $para

return $request;
}

}
19 changes: 19 additions & 0 deletions tests/_support/Controllers/Popcorn.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,23 @@ public function index3()
return $response;
}

public function canyon()
{
echo 'Hello-o-o';
}

public function cat()
{
}

public function json()
{
$this->responsd(['answer' => 42]);
}

public function xml()
{
$this->respond('<my><pet>cat</pet></my>');
}

}
71 changes: 54 additions & 17 deletions tests/system/Test/FeatureTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
use CodeIgniter\Test\FeatureResponse;

/**
* @group DatabaseLive
* @group DatabaseLive
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class FeatureTestCaseTest extends FeatureTestCase
{
Expand All @@ -14,7 +16,6 @@ protected function setUp()
parent::setUp();

$this->skipEvents();
$this->clean = false;
}

public function testCallGet()
Expand All @@ -30,9 +31,6 @@ function () {
]);
$response = $this->get('home');

// close open buffer
ob_end_clean();

$response->assertSee('Hello World');
$response->assertDontSee('Again');
}
Expand All @@ -44,7 +42,7 @@ public function testCallSimpleGet()
'add',
'home',
function () {
return 'Hello World';
return 'Hello Earth';
},
],
]);
Expand All @@ -53,7 +51,7 @@ function () {
$this->assertInstanceOf(FeatureResponse::class, $response);
$this->assertInstanceOf(\CodeIgniter\HTTP\Response::class, $response->response);
$this->assertTrue($response->isOK());
$this->assertEquals('Hello World', $response->response->getBody());
$this->assertEquals('Hello Earth', $response->response->getBody());
$this->assertEquals(200, $response->response->getStatusCode());
}

Expand All @@ -64,13 +62,13 @@ public function testCallPost()
'post',
'home',
function () {
return 'Hello World';
return 'Hello Mars';
},
],
]);
$response = $this->post('home');

$response->assertSee('Hello World');
$response->assertSee('Hello Mars');
}

public function testCallPut()
Expand All @@ -80,13 +78,13 @@ public function testCallPut()
'put',
'home',
function () {
return 'Hello World';
return 'Hello Pluto';
},
],
]);
$response = $this->put('home');

$response->assertSee('Hello World');
$response->assertSee('Hello Pluto');
}

public function testCallPatch()
Expand All @@ -96,13 +94,13 @@ public function testCallPatch()
'patch',
'home',
function () {
return 'Hello World';
return 'Hello Jupiter';
},
],
]);
$response = $this->patch('home');

$response->assertSee('Hello World');
$response->assertSee('Hello Jupiter');
}

public function testCallOptions()
Expand All @@ -112,13 +110,13 @@ public function testCallOptions()
'options',
'home',
function () {
return 'Hello World';
return 'Hello George';
},
],
]);
$response = $this->options('home');

$response->assertSee('Hello World');
$response->assertSee('Hello George');
}

public function testCallDelete()
Expand All @@ -128,13 +126,13 @@ public function testCallDelete()
'delete',
'home',
function () {
return 'Hello World';
return 'Hello Wonka';
},
],
]);
$response = $this->delete('home');

$response->assertSee('Hello World');
$response->assertSee('Hello Wonka');
}

public function testSession()
Expand All @@ -148,4 +146,43 @@ public function testSession()
$response->assertSessionMissing('popcorn');
}

public function testReturns()
{
$this->withRoutes([
[
'get',
'home',
'Tests\Support\Controllers\Popcorn::index',
],
]);
$response = $this->get('home');
$response->assertSee('Hi');
}

public function testIgnores()
{
$this->withRoutes([
[
'get',
'home',
'Tests\Support\Controllers\Popcorn::cat',
],
]);
$response = $this->get('home');
$response->assertEmpty($response->response->getBody());
}

public function testEchoes()
{
$this->withRoutes([
[
'get',
'home',
'Tests\Support\Controllers\Popcorn::canyon',
],
]);
$response = $this->get('home');
$response->assertSee('Hello-o-o');
}

}
10 changes: 7 additions & 3 deletions user_guide_src/source/testing/feature.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,20 @@ Shorthand methods for each of the HTTP verbs exist to ease typing and make thing
Setting Different Routes
------------------------

You can use a custom collection of routes by passing an array of routes into the ``withRoutes()`` method. This will
You can use a custom collection of routes by passing an array of "routes" into the ``withRoutes()`` method. This will
override any existing routes in the system::

$routes = [
'users' => 'UserController::list'
];
[ 'get', 'users', 'UserController::list' ]
];

$result = $this->withRoutes($routes)
->get('users');

Each of the "routes" is a 3 element array containing the HTTP verb (or "add" for all),
the URI to match, and the routing destination.


Setting Session Values
----------------------

Expand Down