Skip to content

Commit

Permalink
Update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Feb 15, 2018
1 parent fb8e268 commit 7c7df16
Show file tree
Hide file tree
Showing 10 changed files with 669 additions and 369 deletions.
224 changes: 140 additions & 84 deletions tests/Application/ApplicationTestCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Rougin\Slytherin\Application;

use Rougin\Slytherin\Http\ServerRequest;
use Rougin\Slytherin\Routing\Router;

/**
* Application Test Cases
*
Expand All @@ -16,7 +19,7 @@ class ApplicationTestCases extends \PHPUnit_Framework_TestCase
protected $application;

/**
* Prepares the application instance.
* Sets up the application instance.
*
* @return void
*/
Expand All @@ -26,188 +29,230 @@ public function setUp()
}

/**
* Tests Application::run.
* Tests Application::handle.
*
* @return void
*/
public function testRunMethod()
public function testHandleMethod()
{
$this->expectOutputString('Hello');
$request = $this->request('GET', '/store');

$this->application->run();
$expected = (string) 'Store';

$response = $this->application->handle($request);

$result = (string) $response->getBody();

$this->assertEquals($expected, $result);
}

/**
* Tests Application::handle.
* Tests Application::handle with a callback as result.
*
* @return void
*/
public function testHandleMethod()
public function testHandleMethodWithCallback()
{
$request = $this->request('GET', '/store');
$request = $this->request('GET', '/callback');

$expected = 'Hello, this is a callback';

$response = $this->application->handle($request);

$result = $this->application->handle($request);
$result = (string) $response->getBody();

$this->assertEquals('Store', (string) $result->getBody());
$this->assertEquals($expected, $result);
}

/**
* Tests the handle() method with a response as result.
* Tests Application::handle with a HTTP 401 response as result.
*
* @return void
*/
public function testHandleMethodWithResponse()
public function testHandleMethodWithHttp401Response()
{
$request = $this->request('GET', '/response');
$request = $this->request('GET', '/error');

$expected = 'Hello with error response';

$response = $this->application->handle($request);

$result = $this->application->handle($request);
$result = (string) $response->getBody();

$this->assertEquals('Hello with response', (string) $result->getBody());
$this->assertEquals($expected, $result);
}

/**
* Tests the handle() method with a HTTP 401 response as result.
* Tests Application::handle with a callback middleware as result.
*
* @return void
*/
public function testHandleMethodWithHttp401Response()
public function testHandleMethodWithMiddleware()
{
$request = $this->request('GET', '/error');
$interface = 'Interop\Http\ServerMiddleware\MiddlewareInterface';

interface_exists($interface) || $this->markTestSkipped('PSR-15 is not installed.');

$request = $this->request('GET', '/middleware');

$result = $this->application->handle($request);
$expected = (string) 'Loaded with middleware';

$this->assertEquals('Hello with error response', (string) $result->getBody());
$response = $this->application->handle($request);

$result = (string) $response->getBody();

$this->assertEquals($expected, $result);
}

/**
* Tests the handle() method with an updated server request.
* Tests Application::handle with an optional parameter as result.
*
* @return void
*/
public function testHandleMethodWithServerRequest()
public function testHandleMethodWithOptionalParameter()
{
$request = $this->request('GET', '/request', array('test' => 'Hello with request'));
$request = $this->request('GET', '/optional');

$expected = (string) 'Hello';

$result = $this->application->handle($request);
$response = $this->application->handle($request);

$this->assertEquals('Hello with request', (string) $result->getBody());
$result = (string) $response->getBody();

$this->assertEquals($expected, $result);
}

/**
* Tests the handle() method with a parameter as result.
* Tests Application::handle with a parameter as result.
*
* @return void
*/
public function testHandleMethodWithParameter()
{
$request = $this->request('GET', '/parameter');

$result = $this->application->handle($request);
$expected = 'Hello';

$response = $this->application->handle($request);

$result = (string) $response->getBody();

$this->assertEquals('Hello', (string) $result->getBody());
$this->assertEquals($expected, $result);
}

/**
* Tests the handle() method with an optional parameter as result.
* Tests Application::handle with a PUT HTTP method.
*
* @return void
*/
public function testHandleMethodWithOptionalParameter()
public function testHandleMethodWithPutHttpMethod()
{
$request = $this->request('GET', '/optional');
$request = $this->request('PUT', '/hello');

$expected = 'Hello from PUT HTTP method';

$response = $this->application->handle($request);

$result = $this->application->handle($request);
$result = (string) $response->getBody();

$this->assertEquals('Hello', (string) $result->getBody());
$this->assertEquals($expected, $result);
}

/**
* Tests the handle() method with a type hinted parameter as result.
* Tests Application::handle with a response as result.
*
* @return void
*/
public function testHandleMethodWithTypehintedParameter()
public function testHandleMethodWithResponse()
{
$interface = 'Rougin\Slytherin\Routing\DispatcherInterface';

$dispatcher = \Rougin\Slytherin\Application::container()->get($interface);
$request = $this->request('GET', '/response');

// TODO: Implement resolving of type hinted parameters from container to PhrouteResolver
if (is_a($dispatcher, 'Rougin\Slytherin\Routing\PhrouteDispatcher')) {
$this->markTestSkipped('Resolving type hinted parameters are not yet implemented in Phroute.');
}
$expected = 'Hello with response';

$request = $this->request('GET', '/typehint/202');
$response = $this->application->handle($request);

$result = $this->application->handle($request);
$result = (string) $response->getBody();

$this->assertEquals(202, $result->getStatusCode());
$this->assertEquals($expected, $result);
}

/**
* Tests the handle() method with a callback as result.
* Tests Application::handle with an updated server request.
*
* @return void
*/
public function testHandleMethodWithCallback()
public function testHandleMethodWithServerRequest()
{
$request = $this->request('GET', '/callback');
$data = array('test' => 'Hello with request');

$result = $this->application->handle($request);
$request = $this->request('GET', '/request', $data);

$this->assertEquals('Hello, this is a callback', (string) $result->getBody());
$expected = 'Hello with request';

$response = $this->application->handle($request);

$result = (string) $response->getBody();

$this->assertEquals($expected, $result);
}

/**
* Tests the handle() method with a callback as result.
* Tests the handle() method with a type hinted parameter as result.
*
* @return void
*/
public function testHandleMethodWithMiddleware()
public function testHandleMethodWithTypehintedParameter()
{
if (! interface_exists('Interop\Http\ServerMiddleware\MiddlewareInterface')) {
$this->markTestSkipped('Interop Middleware is not installed.');
$interface = 'Rougin\Slytherin\Routing\DispatcherInterface';

$dispatcher = Application::container()->get($interface);

// TODO: Implement resolving of type hinted parameters from container to PhrouteResolver
if (is_a($dispatcher, 'Rougin\Slytherin\Routing\PhrouteDispatcher')) {
$this->markTestSkipped('Resolving type hinted parameters are not yet implemented in Phroute.');
}

$request = $this->request('GET', '/middleware');
$request = $this->request('GET', '/typehint/202');

$expected = (integer) 202;

$result = $this->application->handle($request);
$response = $this->application->handle($request);

$this->assertEquals('Loaded with middleware', (string) $result->getBody());
$result = $response->getStatusCode();

$this->assertEquals($expected, $result);
}

/**
* Tests the handle() method with a PUT HTTP method.
* Tests Application::run.
*
* @return void
*/
public function testHandleMethodWithPutHttpMethod()
public function testRunMethod()
{
$request = $this->request('PUT', '/hello');

$result = $this->application->handle($request);
$this->expectOutputString('Hello');

$this->assertEquals('Hello from PUT HTTP method', (string) $result->getBody());
$this->application->run();
}

/**
* Prepares the HTTP method and the URI of the request.
*
* @param string $httpMethod
* @param string $uriEndpoint
* @param string $method
* @param string $uri
* @param array $data
* @return \Psr\Http\Message\ServerRequestInterface
*/
protected function request($httpMethod, $uriEndpoint, $data = array(), $server = array())
protected function request($method, $uri, $data = array(), $server = array())
{
$server['REQUEST_METHOD'] = $httpMethod;
$server['REQUEST_URI'] = $uriEndpoint;
$server['REQUEST_METHOD'] = $method;
$server['REQUEST_URI'] = $uri;
$server['SERVER_NAME'] = 'localhost';
$server['SERVER_PORT'] = '8000';

$request = new \Rougin\Slytherin\Http\ServerRequest($server);
$request = new ServerRequest($server);

switch ($httpMethod) {
switch ($method) {
case 'GET':
$request = $request->withQueryParams($data);

Expand Down Expand Up @@ -237,20 +282,31 @@ protected function request($httpMethod, $uriEndpoint, $data = array(), $server =
*/
protected function router()
{
$last = 'Rougin\Slytherin\Fixture\Middlewares\LastMiddleware';

$router = new \Rougin\Slytherin\Routing\Router;

$router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index');
$router->get('/store', 'Rougin\Slytherin\Fixture\Classes\NewClass@store');
$router->get('/request', 'Rougin\Slytherin\Fixture\Classes\WithServerRequestInterface@index');
$router->get('/response', 'Rougin\Slytherin\Fixture\Classes\WithResponseInterface@index');
$router->get('/error', 'Rougin\Slytherin\Fixture\Classes\WithResponseInterface@error');
$router->get('/parameter', 'Rougin\Slytherin\Fixture\Classes\WithParameter@index');
$router->get('/optional', 'Rougin\Slytherin\Fixture\Classes\WithOptionalParameter@index');
$router->get('/middleware', 'Rougin\Slytherin\Fixture\Classes\NewClass@index', $last);
$router->put('/hello', 'Rougin\Slytherin\Fixture\Classes\WithPutHttpMethod@index');
$router->get('/typehint/:code', 'Rougin\Slytherin\Fixture\Classes\WithResponseInterface@typehint');
$middleware = 'Rougin\Slytherin\Fixture\Middlewares\LastMiddleware';

$router = new Router;

$router->prefix('', 'Rougin\Slytherin\Fixture\Classes');

$router->get('/', 'NewClass@index');

$router->get('/store', 'NewClass@store');

$router->get('/request', 'WithServerRequestInterface@index');

$router->get('/response', 'WithResponseInterface@index');

$router->get('/error', 'WithResponseInterface@error');

$router->get('/parameter', 'WithParameter@index');

$router->get('/optional', 'WithOptionalParameter@index');

$router->get('/middleware', 'NewClass@index', $middleware);

$router->put('/hello', 'WithPutHttpMethod@index');

$router->get('/typehint/:code', 'WithResponseInterface@typehint');

$router->get('/callback', function () {
return 'Hello, this is a callback';
Expand Down
Loading

0 comments on commit 7c7df16

Please sign in to comment.