summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Router/Runner/ControllerTest.php63
-rw-r--r--tests/support/TestHelpers.php19
2 files changed, 58 insertions, 24 deletions
diff --git a/tests/Router/Runner/ControllerTest.php b/tests/Router/Runner/ControllerTest.php
index c1b1801..5bbbeca 100644
--- a/tests/Router/Runner/ControllerTest.php
+++ b/tests/Router/Runner/ControllerTest.php
@@ -6,6 +6,7 @@ use Jasny\Router\Route;
use Jasny\Router\Runner;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\StreamInterface;
use Jasny\Router\TestHelpers;
@@ -39,44 +40,58 @@ class ControllerTest extends \PHPUnit_Framework_TestCase
$this->assertSame($finalResponse, $result);
}
- /**
- * @expectedException RuntimeException
- * @expectedExceptionMessage Can not route to controller 'DoesNotExistsController': class not exists
- */
- public function testInvalidClass()
+ public function invalidProvider()
{
- $request = $this->createMock(ServerRequestInterface::class);
- $response = $this->createMock(ResponseInterface::class);
+ $runnerNotExists = $this->createPartialMock(Runner\Controller::class, ['instantiate']);
+ $runnerNotExists->expects($this->never())->method('instantiate');
- $route = $this->createMock(Route::class);
- $route->controller = 'does-not-exists';
-
- $request->expects($this->once())->method('getAttribute')->with('route')->willReturn($route);
+ $runnerNotCallable = $this->createPartialMock(Runner\Controller::class, ['instantiate', 'getClass']);
+ $runnerNotCallable->expects($this->once())->method('getClass')->with('foo-bar-zoo')->willReturn('stdClass');
+ $runnerNotCallable->expects($this->never())->method('instantiate');
- $runner = $this->getMockBuilder(Runner\Controller::class)->setMethods(['instantiate'])->getMock();
- $runner->expects($this->never())->method('instantiate');
-
- $runner($request, $response);
+ return [
+ [
+ $runnerNotExists,
+ "Can't route to controller 'FooBarZooController': class not exists"
+ ],
+ [
+ $runnerNotCallable,
+ "Can't route to controller 'stdClass': class does not have '__invoke' method"
+ ]
+ ];
}
/**
- * @expectedException RuntimeException
- * @expectedExceptionMessage Can not route to controller 'stdClass': class does not have '__invoke' method
+ * @dataProvider invalidProvider
+ *
+ * @param Runner $runner
+ * @param string $message
*/
- public function testInvokeNotCallable()
+ public function testInvokeInvalid(Runner $runner, $message)
{
$request = $this->createMock(ServerRequestInterface::class);
+ $request->method('getProtocolVersion')->willReturn('1.1');
+
+ $body = $this->createMock(StreamInterface::class);
+ $body->expects($this->once())->method('write')->with('Not found');
+
+ $notFound = $this->createMock(ResponseInterface::class);
+ $notFound->expects($this->once())->method('getBody')->willReturn($body);
+
$response = $this->createMock(ResponseInterface::class);
+ $response->expects($this->once())->method('withProtocolVersion')->with('1.1')->willReturnSelf();
+ $response->expects($this->once())->method('withStatus')->with(404)->willReturnSelf();
+ $response->expects($this->once())->method('withHeader')->with('Content-Type', 'text/plain')
+ ->willReturn($notFound);
$route = $this->createMock(Route::class);
- $route->controller = 'foo';
+ $route->controller = 'foo-bar-zoo';
$request->expects($this->once())->method('getAttribute')->with('route')->willReturn($route);
- $runner = $this->getMockBuilder(Runner\Controller::class)->setMethods(['instantiate', 'getClass'])->getMock();
- $runner->expects($this->once())->method('getClass')->with('foo')->willReturn('stdClass');
- $runner->expects($this->never())->method('instantiate');
-
- $runner($request, $response);
+ $result = @$runner($request, $response);
+
+ $this->assertSame($notFound, $result);
+ $this->assertLastError(E_USER_NOTICE, $message);
}
}
diff --git a/tests/support/TestHelpers.php b/tests/support/TestHelpers.php
index e4fdff7..3781851 100644
--- a/tests/support/TestHelpers.php
+++ b/tests/support/TestHelpers.php
@@ -27,4 +27,23 @@ trait TestHelpers
return $callback;
}
+
+ /**
+ * Assert a non-fatal error
+ *
+ * @param int $type
+ * @param string $message
+ */
+ protected function assertLastError($type, $message)
+ {
+ $error = error_get_last();
+
+ $expect = compact('type', 'message');
+
+ if (is_array($error)) {
+ $error = array_intersect_key($error, $expect);
+ }
+
+ $this->assertEquals($expect, $error);
+ }
}