diff --git a/.travis.yml b/.travis.yml index 64b9a4d..ec1116d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,7 @@ language: php php: - - 7.0 - - 7.1 - 7.2 + - 7.3 env: global: diff --git a/composer.json b/composer.json index c3da73a..4b1ec1f 100644 --- a/composer.json +++ b/composer.json @@ -3,11 +3,12 @@ "description": "Silex with enhanced DI", "type": "project", "require": { - "silex/silex": "^1.3" + "php": ">=7.2", + "silex/silex": "^2.3" }, "require-dev": { "clearbooks/di-container-mock": "1.0.0", - "phpunit/phpunit": "^6" + "phpunit/phpunit": "^8" }, "license": "LGPL-3.0", "authors": [ diff --git a/src/ApplicationBuilder.php b/src/ApplicationBuilder.php index f55f305..bd7f774 100644 --- a/src/ApplicationBuilder.php +++ b/src/ApplicationBuilder.php @@ -14,6 +14,6 @@ abstract class ApplicationBuilder public static function build( ContainerInterface $container, Application $app ) { $app['callback_resolver'] = new CallbackResolver( $container, $app ); - $app['resolver'] = new ControllerResolver( $app, $container ); + $app['resolver'] = new ControllerResolver( $container ); } -} \ No newline at end of file +} diff --git a/src/ControllerResolver.php b/src/ControllerResolver.php index 3d89e0e..39b3c02 100644 --- a/src/ControllerResolver.php +++ b/src/ControllerResolver.php @@ -2,11 +2,12 @@ namespace Clearbooks\Dilex; use Interop\Container\ContainerInterface; use Silex\Application; +use Symfony\Component\HttpFoundation\Request; /** * Class ControllerResolver */ -class ControllerResolver extends \Silex\ControllerResolver +class ControllerResolver extends \Symfony\Component\HttpKernel\Controller\ControllerResolver { /** * @var ContainerInterface @@ -14,12 +15,11 @@ class ControllerResolver extends \Silex\ControllerResolver private $container; /** - * @param Application $app * @param ContainerInterface $container */ - public function __construct( Application $app, ContainerInterface $container ) + public function __construct( ContainerInterface $container ) { - parent::__construct( $app ); + parent::__construct(); $this->container = $container; } @@ -34,4 +34,4 @@ protected function createController( $controller ) } return [$this->container->get( $controller ), 'execute']; } -} \ No newline at end of file +} diff --git a/test/ApplicationTest.php b/test/ApplicationTest.php new file mode 100644 index 0000000..1865d61 --- /dev/null +++ b/test/ApplicationTest.php @@ -0,0 +1,75 @@ + 'value1', + 'test2' => 'value2' + ]; + + private const ECHO_ENDPOINT = '/echo'; + private const ERROR_ENDPOINT = '/error'; + + private $error; + private $app; + + public function setUp(): void + { + parent::setUp(); + + $this->error = null; + $this->app = new Dilex(); + ApplicationBuilder::build( new MockContainer( [ + EchoController::class => new EchoController(), + ErrorThrowingController::class => new ErrorThrowingController() + ] ), $this->app ); + $this->app->post(self::ECHO_ENDPOINT, EchoController::class); + $this->app->post(self::ERROR_ENDPOINT, ErrorThrowingController::class); + + $this->app->error(function ($e) { + $this->error = $e; + }); + } + + /** + * @test + */ + public function givenApplicationWithController_handlesRequest(): void + { + $content = json_encode(self::TEST_PAYLOAD); + + $request = Request::create(self::ECHO_ENDPOINT, Request::METHOD_POST, [], [], [], [], $content); + + ob_start(); + $this->app->run($request); + $body = ob_get_clean(); + + $this->assertEquals($content, $body); + $this->assertNull($this->error); + } + + /** + * @test + */ + public function givenApplicationWithController_handlesErrors(): void + { + $content = json_encode(self::TEST_PAYLOAD); + + $request = Request::create(self::ERROR_ENDPOINT, Request::METHOD_POST, [], [], [], [], $content); + + ob_start(); + $this->app->run($request); + $body = ob_get_clean(); + + $this->assertNotEquals($content, $body); + $this->assertNotNull($this->error); + } +} diff --git a/test/CallbackResolverTest.php b/test/CallbackResolverTest.php index a7420f9..6678d2e 100644 --- a/test/CallbackResolverTest.php +++ b/test/CallbackResolverTest.php @@ -33,7 +33,7 @@ private function resolve( $callback ) /** * Set up */ - public function setUp() + public function setUp(): void { $this->app = new Dilex; $this->mockContainer = new MockContainer( [ MiddlewareDummy::class => new MiddlewareDummy ] ); @@ -42,19 +42,19 @@ public function setUp() /** * @test - * @expectedException \Exception */ public function givenNullName_throwException() { + $this->expectException(\Exception::class); $this->resolve( null ); } /** * @test - * @expectedException \Exception */ public function givenClassWhichIsNotMiddleware_throwException() { + $this->expectException(\Exception::class); $this->resolve( stdClass::class ); } diff --git a/test/ControllerResolverTest.php b/test/ControllerResolverTest.php index c37d498..9a763bc 100644 --- a/test/ControllerResolverTest.php +++ b/test/ControllerResolverTest.php @@ -17,11 +17,6 @@ class ControllerResolverTest extends TestCase */ private $resolver; - /** - * @var Dilex - */ - private $app; - /** * Get a request for the given controller * @param $controller @@ -43,11 +38,11 @@ private function resolve( $controller ) /** * Set up */ - public function setUp() + public function setUp(): void { $this->app = new Dilex(); $this->mockContainer = new MockContainer( [ EndpointDummy::class => new EndpointDummy ] ); - $this->resolver = new ControllerResolver( $this->app, $this->mockContainer ); + $this->resolver = new ControllerResolver( $this->mockContainer ); } /** @@ -60,10 +55,10 @@ public function givenNullName_returnFalse() /** * @test - * @expectedException \Exception */ public function givenClassWhichIsNotAnEndpoint_throwException() { + $this->expectException(\Exception::class); $this->assertFalse( $this->resolve( stdClass::class ) ); } diff --git a/test/EchoController.php b/test/EchoController.php new file mode 100644 index 0000000..d60f732 --- /dev/null +++ b/test/EchoController.php @@ -0,0 +1,19 @@ +getContent(), + 200 + ); + } +} diff --git a/test/ErrorThrowingController.php b/test/ErrorThrowingController.php new file mode 100644 index 0000000..92a7380 --- /dev/null +++ b/test/ErrorThrowingController.php @@ -0,0 +1,15 @@ +