From 94777ef55ac08eb412efd59bd0875afc1bd054a4 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Wed, 23 Jan 2019 09:43:23 -0500 Subject: [PATCH] Test Router --- test/Http/RouterTest.php | 59 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/test/Http/RouterTest.php b/test/Http/RouterTest.php index 407f6df..3d3b106 100644 --- a/test/Http/RouterTest.php +++ b/test/Http/RouterTest.php @@ -1,14 +1,69 @@ getController = $this->createMock( GetController::class ); + $this->postController = $this->createMock( PostController::class ); + $this->router = new Router( $this->getController, $this->postController ); + $this->kernel = $this->createMock( Kernel::class ); + } + public function testRouter() { - // TODO implement me - $this->assertTrue( false ); + $testCases = array( + array( + 'id' => 'GET', + 'request' => Request::create( 'https://foo.com', Request::METHOD_GET ), + 'expectedController' => array( $this->getController, 'handle' ), + ), + array( + 'id' => 'POST', + 'request' => Request::create( 'https://foo.com', Request::METHOD_POST ), + 'expectedController' => array( $this->postController, 'handle' ), + ), + array( + 'id' => 'MethodNotAllowed', + 'request' => Request::create( 'https://foo.com', Request::METHOD_PUT ), + 'expectedException' => MethodNotAllowedHttpException::class, + ), + ); + foreach( $testCases as $testCase ) { + $request = $testCase['request']; + $event = new GetResponseEvent( + $this->kernel, $request, HttpKernelInterface::MASTER_REQUEST + ); + if ( array_key_exists( 'expectedException', $testCase ) ) { + $this->expectException( $testCase['expectedException'] ); + } + $this->router->route( $event ); + $this->assertEquals( + $testCase['expectedController'], + $request->attributes->get( '_controller' ), + "Error on test $testCase[id]" + ); + } } } ?>