diff --git a/src/Middleware/CliParamsMiddleware.php b/src/Middleware/CliParamsMiddleware.php index da243a15..683a8e86 100644 --- a/src/Middleware/CliParamsMiddleware.php +++ b/src/Middleware/CliParamsMiddleware.php @@ -12,10 +12,15 @@ class CliParamsMiddleware implements MiddlewareInterface * @var array */ private $argv; + /** + * @var + */ + private $currentSapi; - public function __construct(array $argv) + public function __construct(array $argv, $currentSapi) { $this->argv = $argv; + $this->currentSapi = $currentSapi; } /** @@ -46,7 +51,7 @@ class CliParamsMiddleware implements MiddlewareInterface public function __invoke(Request $request, Response $response, callable $out = null) { // When not in CLI, just call next middleware - if (php_sapi_name() !== 'cli') { + if ($this->currentSapi !== 'cli') { return $out($request, $response); } diff --git a/src/Middleware/Factory/CliParamsMiddlewareFactory.php b/src/Middleware/Factory/CliParamsMiddlewareFactory.php index b806cbeb..9be7f52e 100644 --- a/src/Middleware/Factory/CliParamsMiddlewareFactory.php +++ b/src/Middleware/Factory/CliParamsMiddlewareFactory.php @@ -24,6 +24,9 @@ class CliParamsMiddlewareFactory implements FactoryInterface */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - return new CliParamsMiddleware(isset($_SERVER['argv']) ? $_SERVER['argv'] : []); + return new CliParamsMiddleware( + isset($_SERVER['argv']) ? $_SERVER['argv'] : [], + php_sapi_name() + ); } } diff --git a/tests/Middleware/CliParamsMiddlewareTest.php b/tests/Middleware/CliParamsMiddlewareTest.php new file mode 100644 index 00000000..e07d05c0 --- /dev/null +++ b/tests/Middleware/CliParamsMiddlewareTest.php @@ -0,0 +1,91 @@ +__invoke( + ServerRequestFactory::fromGlobals(), + $originalResponse, + function ($req, $resp) use (&$invoked) { + $invoked = true; + return $resp; + } + ); + + $this->assertSame($originalResponse, $response); + $this->assertTrue($invoked); + } + + /** + * @test + */ + public function nonSuccessRouteResultJustInvokesNextMiddleware() + { + $middleware = new CliParamsMiddleware([], 'cli'); + + $invoked = false; + $originalResponse = new Response(); + $routeResult = $this->prophesize(RouteResult::class); + $routeResult->isSuccess()->willReturn(false)->shouldBeCalledTimes(1); + + $response = $middleware->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute(RouteResult::class, $routeResult->reveal()), + $originalResponse, + function ($req, $resp) use (&$invoked) { + $invoked = true; + return $resp; + } + ); + + $this->assertSame($originalResponse, $response); + $this->assertTrue($invoked); + } + + /** + * @test + */ + public function properRouteWillInjectAttributeInResponse() + { + $expectedLongUrl = 'http://www.google.com'; + $middleware = new CliParamsMiddleware(['foo', 'bar', $expectedLongUrl], 'cli'); + + $invoked = false; + $originalResponse = new Response(); + $routeResult = $this->prophesize(RouteResult::class); + $routeResult->isSuccess()->willReturn(true)->shouldBeCalledTimes(1); + $routeResult->getMatchedRouteName()->willReturn('cli-generate-shortcode')->shouldBeCalledTimes(1); + /** @var ServerRequestInterface $request */ + $request = null; + + $response = $middleware->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute(RouteResult::class, $routeResult->reveal()), + $originalResponse, + function ($req, $resp) use (&$invoked, &$request) { + $invoked = true; + $request = $req; + return $resp; + } + ); + + $this->assertSame($originalResponse, $response); + $this->assertEquals($expectedLongUrl, $request->getAttribute('longUrl')); + $this->assertTrue($invoked); + } +} diff --git a/tests/Middleware/Factory/CliParamsMiddlewareFactoryTest.php b/tests/Middleware/Factory/CliParamsMiddlewareFactoryTest.php new file mode 100644 index 00000000..3fad4bab --- /dev/null +++ b/tests/Middleware/Factory/CliParamsMiddlewareFactoryTest.php @@ -0,0 +1,29 @@ +factory = new CliParamsMiddlewareFactory(); + } + + /** + * @test + */ + public function serviceIsCreated() + { + $instance = $this->factory->__invoke(new ServiceManager(), ''); + $this->assertInstanceOf(CliParamsMiddleware::class, $instance); + } +}