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 7c7df16 commit 5b85e39
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 154 deletions.
10 changes: 6 additions & 4 deletions tests/Middleware/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
class DispatcherTest extends DispatcherTestCases
{
/**
* Sets up the middleware dispatcher.
* Sets up the middleware dispatcher instance.
*
* @return void
*/
public function setUp()
{
if (! interface_exists('Interop\Http\ServerMiddleware\MiddlewareInterface')) {
$this->markTestSkipped('Interop Middleware is not installed.');
}
$interface = 'Interop\Http\ServerMiddleware\MiddlewareInterface';

$message = 'http-interop/http-middleware (v0.4.0) is not installed.';

interface_exists($interface) || $this->markTestSkipped($message);

$this->dispatcher = new Dispatcher;
}
Expand Down
118 changes: 58 additions & 60 deletions tests/Middleware/DispatcherTestCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Rougin\Slytherin\Middleware;

use Rougin\Slytherin\Http\ServerRequest;

/**
* Dispatcher Test Cases
*
Expand All @@ -22,17 +24,17 @@ class DispatcherTestCases extends \PHPUnit_Framework_TestCase
*/
public function testProcessMethodWithDoublePassCallback()
{
$this->exists(get_class($this->dispatcher));

$callback = function ($request, $response, $next) {
$this->dispatcher->push(function ($request, $response, $next) {
$response = $next($request, $response)->withStatus(404);

return $response->withHeader('X-Slytherin', time());
};
});

$this->dispatcher->push($callback);
$expected = (integer) 404;

$this->assertEquals(404, $this->response()->getStatusCode());
$result = $this->process()->getStatusCode();

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

/**
Expand All @@ -42,59 +44,63 @@ public function testProcessMethodWithDoublePassCallback()
*/
public function testProcessMethodWithSinglePassCallback()
{
$this->exists(get_class($this->dispatcher));

$stratigility = 'Rougin\Slytherin\Middleware\StratigilityDispatcher';

$wrapper = 'Zend\Stratigility\Middleware\CallableMiddlewareWrapper';

if (is_a($this->dispatcher, $stratigility) && ! class_exists($wrapper)) {
$message = 'Stratigility\'s current version does not accept single pass middlewares.';
$message = 'Stratigility\'s current installed version';

$this->markTestSkipped($message);
$message .= ' does not accept single pass middlewares';

$this->markTestSkipped((string) $message);
}

$time = time();
$time = (integer) time();

$callback = function ($request, $next) use ($time) {
$this->dispatcher->push(function ($request, $next) use ($time) {
$response = $next($request);

return $response->withHeader('X-Slytherin', $time);
};
});

$expected = array((integer) $time);

$this->dispatcher->push($callback);
$result = $this->process()->getHeader('X-Slytherin');

$this->assertEquals(array($time), $this->response()->getHeader('X-Slytherin'));
$this->assertEquals($expected, $result);
}

/**
* Tests DispatcherInterface::process with \Interop\Http\ServerMiddleware\DelegateInterface callback.
* Tests DispatcherInterface::process with DelegateInterface callback.
*
* @return void
*/
public function testProcessMethodWithDelagateInterfaceCallback()
{
$this->exists(get_class($this->dispatcher));

$stratigility = 'Rougin\Slytherin\Middleware\StratigilityDispatcher';

$wrapper = 'Zend\Stratigility\Middleware\CallableMiddlewareWrapper';

if (is_a($this->dispatcher, $stratigility) && ! class_exists($wrapper)) {
$message = 'Stratigility\'s current version does not accept delegates.';
$message = 'Stratigility\'s current version';

$message .= (string) ' does not accept delegates';

$this->markTestSkipped($message);
$this->markTestSkipped((string) $message);
}

$callback = function ($request, $delegate) {
$this->dispatcher->push(function ($request, $delegate) {
$response = $delegate->process($request);

return $response->withHeader('Content-Type', 'application/json');
};
});

$this->dispatcher->push($callback);
$expected = array('application/json');

$this->assertEquals(array('application/json'), $this->response()->getHeader('Content-Type'));
$result = $this->process()->getHeader('Content-Type');

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

/**
Expand All @@ -104,21 +110,27 @@ public function testProcessMethodWithDelagateInterfaceCallback()
*/
public function testProcessMethodWithString()
{
$this->exists(get_class($this->dispatcher));

$stratigility = 'Rougin\Slytherin\Middleware\StratigilityDispatcher';

$wrapper = 'Zend\Stratigility\Middleware\CallableMiddlewareWrapper';

if (is_a($this->dispatcher, $stratigility) && ! class_exists($wrapper)) {
$message = 'Stratigility\'s current version does not PSR-15 middlewares.';
$message = 'Stratigility\'s current version';

$this->markTestSkipped($message);
$message .= ' does not accept PSR-15 middlewares';

$this->markTestSkipped((string) $message);
}

$this->dispatcher->push('Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware');
$interop = 'Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware';

$this->dispatcher->push($interop);

$expected = 500;

$result = $this->process()->getStatusCode();

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

/**
Expand All @@ -128,15 +140,15 @@ public function testProcessMethodWithString()
*/
public function testPushMethodWithArray()
{
$this->exists(get_class($this->dispatcher));
$expected = array('Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware');

$stack = array('Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware');
$expected[] = 'Rougin\Slytherin\Middleware\FinalResponse';

$stack[] = 'Rougin\Slytherin\Middleware\FinalResponse';
$this->dispatcher->push($expected);

$this->dispatcher->push($stack);
$result = $this->dispatcher->stack();

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

/**
Expand All @@ -146,48 +158,34 @@ public function testPushMethodWithArray()
*/
public function testStackMethod()
{
$this->exists(get_class($this->dispatcher));

$this->dispatcher->push('Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware');

$this->dispatcher->push('Rougin\Slytherin\Middleware\FinalResponse');

$this->assertCount(2, $this->dispatcher->stack());
$expected = (integer) 2;

$result = $this->dispatcher->stack();

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

/**
* Processes the defined middleware dispatcher and return its response.
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function response()
protected function process()
{
$server = array();
$server = array('REQUEST_METHOD' => 'GET');

$server['REQUEST_METHOD'] = 'GET';
$server['REQUEST_URI'] = '/';

$server['SERVER_NAME'] = 'localhost';

$server['SERVER_PORT'] = '8000';

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

return $this->dispatcher->process($request, new Delegate);
}

/**
* Verifies the specified dispatcher if it exists.
*
* @param string $dispatcher
* @return void
*/
protected function exists($dispatcher)
{
switch ($dispatcher) {
case 'Rougin\Slytherin\Middleware\StratigilityDispatcher':
if (class_exists('Zend\Stratigility\MiddlewarePipe') === false) {
$this->markTestSkipped('Zend Stratigility is not installed.');
}

break;
}
}
}
14 changes: 8 additions & 6 deletions tests/Middleware/StratigilityDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Rougin\Slytherin\Middleware;

use Zend\Stratigility\MiddlewarePipe;

/**
* Stratigility Dispatcher Test
*
Expand All @@ -11,18 +13,18 @@
class StratigilityDispatcherTest extends DispatcherTestCases
{
/**
* Sets up the middleware dispatcher.
* Sets up the middleware dispatcher instance.
*
* @return void
*/
public function setUp()
{
if (! class_exists('Zend\Stratigility\MiddlewarePipe')) {
$this->markTestSkipped('Zend Stratigility is not installed.');
}
$class = (string) 'Zend\Stratigility\MiddlewarePipe';

$message = 'Zend Stratigility is not installed';

$pipe = new \Zend\Stratigility\MiddlewarePipe;
class_exists($class) || $this->markTestSkipped($message);

$this->dispatcher = new StratigilityDispatcher($pipe);
$this->dispatcher = new StratigilityDispatcher(new MiddlewarePipe);
}
}
33 changes: 25 additions & 8 deletions tests/Routing/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Rougin\Slytherin\Routing;

use Rougin\Slytherin\Fixture\Classes\NewClass;

/**
* Dispatcher Test
*
Expand All @@ -21,8 +23,11 @@ public function setUp()

$router = new Router($routes);

$router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index');
$router->post('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@store');
$router->prefix('', 'Rougin\Slytherin\Fixture\Classes');

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

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

$router->get('/hi', function () {
return 'Hi and this is a callback';
Expand All @@ -42,15 +47,21 @@ public function testDispatchMethodWithClassAndFastRouteRouter()

$router = new FastRouteRouter;

$router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index');
$router->prefix('', 'Rougin\Slytherin\Fixture\Classes');

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

$dispatcher = new Dispatcher($router);

$controller = new \Rougin\Slytherin\Fixture\Classes\NewClass;
$controller = new NewClass;

list($function) = $dispatcher->dispatch('GET', '/');

$this->assertEquals($controller->index(), $this->result($function));
$expected = (string) $controller->index();

$result = $this->result($function);

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

/**
Expand All @@ -64,14 +75,20 @@ public function testDispatchMethodWithClassAndPhrouteRouter()

$router = new PhrouteRouter;

$router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index');
$router->prefix('', 'Rougin\Slytherin\Fixture\Classes');

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

$dispatcher = new Dispatcher($router);

$controller = new \Rougin\Slytherin\Fixture\Classes\NewClass;
$controller = new NewClass;

list($function) = $dispatcher->dispatch('GET', '/');

$this->assertEquals($controller->index(), $this->result($function));
$expected = (string) $controller->index();

$result = $this->result($function);

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

0 comments on commit 5b85e39

Please sign in to comment.