Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php namespace Slim\Tests; use Prophecy\Argument; use Psr\Container\ContainerInterface;..

Decoded Output download

<?php
  namespace Slim\Tests; use Prophecy\Argument; use Psr\Container\ContainerInterface; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Log\LoggerInterface; use ReflectionClass; use ReflectionProperty; use RuntimeException; use Slim\App; use Slim\CallableResolver; use Slim\Exception\HttpMethodNotAllowedException; use Slim\Exception\HttpNotFoundException; use Slim\Handlers\Strategies\RequestResponseArgs; use Slim\Handlers\Strategies\RequestResponseNamedArgs; use Slim\Interfaces\CallableResolverInterface; use Slim\Interfaces\MiddlewareDispatcherInterface; use Slim\Interfaces\RouteCollectorInterface; use Slim\Interfaces\RouteCollectorProxyInterface; use Slim\Interfaces\RouteParserInterface; use Slim\Middleware\BodyParsingMiddleware; use Slim\Middleware\ErrorMiddleware; use Slim\Middleware\RoutingMiddleware; use Slim\MiddlewareDispatcher; use Slim\Routing\RouteCollector; use Slim\Routing\RouteCollectorProxy; use Slim\Routing\RouteContext; use Slim\Tests\Mocks\MockAction; use stdClass; use function array_key_exists; use function array_shift; use function count; use function ini_set; use function json_encode; use function strtolower; use function sys_get_temp_dir; use function tempnam; class AppTest extends TestCase { public static function setupBeforeClass() : void { ini_set("error_log", tempnam(sys_get_temp_dir(), "slim")); } public function testDoesNotUseContainerAsServiceLocator() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $containerProphecy->has(Argument::type("string"))->shouldNotHaveBeenCalled(); $containerProphecy->get(Argument::type("string"))->shouldNotHaveBeenCalled(); } public function testGetContainer() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $this->assertSame($containerProphecy->reveal(), $app->getContainer()); } public function testGetCallableResolverReturnsInjectedInstance() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $app = new App($responseFactoryProphecy->reveal(), null, $callableResolverProphecy->reveal()); $this->assertSame($callableResolverProphecy->reveal(), $app->getCallableResolver()); } public function testCreatesCallableResolverWhenNull() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $callableResolver = new CallableResolver($containerProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal(), null); $this->assertEquals($callableResolver, $app->getCallableResolver()); } public function testGetRouteCollectorReturnsInjectedInstance() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeParserProphecy = $this->prophesize(RouteParserInterface::class); $routeCollectorProphecy->getRouteParser()->willReturn($routeParserProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal(), null, null, $routeCollectorProphecy->reveal()); $this->assertSame($routeCollectorProphecy->reveal(), $app->getRouteCollector()); } public function testCreatesRouteCollectorWhenNullWithInjectedContainer() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), $containerProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal(), $callableResolverProphecy->reveal()); $this->assertEquals($routeCollector, $app->getRouteCollector()); } public function testGetMiddlewareDispatcherGetsSeededAndReturnsInjectedInstance() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $middlewareDispatcherProphecy = $this->prophesize(MiddlewareDispatcherInterface::class); $middlewareDispatcherProphecy->seedMiddlewareStack(Argument::any())->shouldBeCalledOnce(); $app = new App($responseFactoryProphecy->reveal(), null, null, null, null, $middlewareDispatcherProphecy->reveal()); $this->assertSame($middlewareDispatcherProphecy->reveal(), $app->getMiddlewareDispatcher()); } public function lowerCaseRequestMethodsProvider() : array { return array(array("get"), array("post"), array("put"), array("patch"), array("delete"), array("options")); } public function testGetPostPutPatchDeleteOptionsMethods(string $method) : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("Hello World"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn($method); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $methodName = strtolower($method); $app = new App($responseFactoryProphecy->reveal()); $app->{$methodName}("/", function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("Hello World", (string) $response->getBody()); } public function testAnyRoute() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("Hello World"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->any("/", function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); foreach ($this->upperCaseRequestMethodsProvider() as $methods) { $method = $methods[0]; $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn($method); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("Hello World", (string) $response->getBody()); } } public function upperCaseRequestMethodsProvider() : array { return array(array("GET"), array("POST"), array("PUT"), array("PATCH"), array("DELETE"), array("OPTIONS")); } public function testMapRoute(string $method) : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("Hello World"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn($method); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app = new App($responseFactoryProphecy->reveal()); $app->map(array($method), "/", function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("Hello World", (string) $response->getBody()); } public function testRedirectRoute() : void { $from = "/from"; $to = "/to"; $routeCreatedResponse = $this->prophesize(ResponseInterface::class); $handlerCreatedResponse = $this->prophesize(ResponseInterface::class); $handlerCreatedResponse->getStatusCode()->willReturn(301); $handlerCreatedResponse->getHeaderLine("Location")->willReturn($to); $handlerCreatedResponse->withHeader(Argument::type("string"), Argument::type("string"))->will(function ($args) { $this->getHeader($args[0])->willReturn($args[1]); return $this; }); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($routeCreatedResponse->reveal()); $responseFactoryProphecy->createResponse(301)->willReturn($handlerCreatedResponse->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn($from); $uriProphecy->__toString()->willReturn($to); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app = new App($responseFactoryProphecy->reveal()); $app->redirect($from, $to, 301); $response = $app->handle($requestProphecy->reveal()); $responseFactoryProphecy->createResponse(301)->shouldHaveBeenCalled(); $this->assertSame(301, $response->getStatusCode()); $this->assertSame($to, $response->getHeaderLine("Location")); } public function testRouteWithInternationalCharacters() : void { $path = "/\xd0\xbd\320\276\xd0\xb2\xd0\xbe\xd1\x81\321\202\320\270"; $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("Hello World"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get($path, function () use($responseProphecy) { return $responseProphecy->reveal(); }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn($path); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("Hello World", (string) $response->getBody()); } public function routePatternsProvider() : array { return array(array(''), array("/"), array("foo"), array("/foo"), array("/foo/")); } public function testRoutePatterns(string $pattern) : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $app->get($pattern, function () { }); $routeCollector = $app->getRouteCollector(); $route = $routeCollector->lookupRoute("route0"); $this->assertSame($pattern, $route->getPattern()); } public function routeGroupsDataProvider() : array { return array("empty group with empty route" => array(array('', ''), ''), "empty group with single slash route" => array(array('', "/"), "/"), "empty group with segment route that does not end in aSlash" => array(array('', "/foo"), "/foo"), "empty group with segment route that ends in aSlash" => array(array('', "/foo/"), "/foo/"), "group single slash with empty route" => array(array("/", ''), "/"), "group single slash with single slash route" => array(array("/", "/"), "//"), "group single slash with segment route that does not end in aSlash" => array(array("/", "/foo"), "//foo"), "group single slash with segment route that ends in aSlash" => array(array("/", "/foo/"), "//foo/"), "group segment with empty route" => array(array("/foo", ''), "/foo"), "group segment with single slash route" => array(array("/foo", "/"), "/foo/"), "group segment with segment route that does not end in aSlash" => array(array("/foo", "/bar"), "/foo/bar"), "group segment with segment route that ends in aSlash" => array(array("/foo", "/bar/"), "/foo/bar/"), "empty group with nested group segment with an empty route" => array(array('', "/foo", ''), "/foo"), "empty group with nested group segment with single slash route" => array(array('', "/foo", "/"), "/foo/"), "group single slash with empty nested group and segment route without leading slash" => array(array("/", '', "foo"), "/foo"), "group single slash with empty nested group and segment route" => array(array("/", '', "/foo"), "//foo"), "group single slash with single slash group and segment route without leading slash" => array(array("/", "/", "foo"), "//foo"), "group single slash with single slash nested group and segment route" => array(array("/", "/", "/foo"), "///foo"), "group single slash with nested group segment with an empty route" => array(array("/", "/foo", ''), "//foo"), "group single slash with nested group segment with single slash route" => array(array("/", "/foo", "/"), "//foo/"), "group single slash with nested group segment with segment route" => array(array("/", "/foo", "/bar"), "//foo/bar"), "group single slash with nested group segment with segment route that has aTrailing slash" => array(array("/", "/foo", "/bar/"), "//foo/bar/"), "empty group with empty nested group and segment route without leading slash" => array(array('', '', "foo"), "foo"), "empty group with empty nested group and segment route" => array(array('', '', "/foo"), "/foo"), "empty group with single slash group and segment route without leading slash" => array(array('', "/", "foo"), "/foo"), "empty group with single slash nested group and segment route" => array(array('', "/", "/foo"), "//foo"), "empty group with nested group segment with segment route" => array(array('', "/foo", "/bar"), "/foo/bar"), "empty group with nested group segment with segment route that has aTrailing slash" => array(array('', "/foo", "/bar/"), "/foo/bar/"), "group segment with empty nested group and segment route without leading slash" => array(array("/foo", '', "bar"), "/foobar"), "group segment with empty nested group and segment route" => array(array("/foo", '', "/bar"), "/foo/bar"), "group segment with single slash nested group and segment route" => array(array("/foo", "/", "bar"), "/foo/bar"), "group segment with single slash nested group and slash segment route" => array(array("/foo", "/", "/bar"), "/foo//bar"), "two group segments with empty route" => array(array("/foo", "/bar", ''), "/foo/bar"), "two group segments with single slash route" => array(array("/foo", "/bar", "/"), "/foo/bar/"), "two group segments with segment route" => array(array("/foo", "/bar", "/baz"), "/foo/bar/baz"), "two group segments with segment route that has aTrailing slash" => array(array("/foo", "/bar", "/baz/"), "/foo/bar/baz/")); } public function testGroupClosureIsBoundToThisClass() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $testCase = $this; $app->group("/foo", function () use($testCase) { $testCase->assertSame($testCase, $this); }); } public function testRouteGroupCombinations(array $sequence, string $expectedPath) : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $processSequence = function (RouteCollectorProxy $app, array $sequence, $processSequence) { $path = array_shift($sequence); if (count($sequence)) { $app->group($path, function (RouteCollectorProxy $group) use(&$sequence, $processSequence) { $processSequence($group, $sequence, $processSequence); }); } else { $app->get($path, function () { }); } }; $processSequence($app, $sequence, $processSequence); $routeCollector = $app->getRouteCollector(); $route = $routeCollector->lookupRoute("route0"); $this->assertSame($expectedPath, $route->getPattern()); } public function testRouteGroupPattern() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryInterface = $responseFactoryProphecy->reveal(); $app = new App($responseFactoryInterface); $group = $app->group("/foo", function () { }); $this->assertSame("/foo", $group->getPattern()); } public function testAddMiddleware() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("Hello World"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::cetera())->will(function () use($responseProphecy) { return $responseProphecy->reveal(); }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) { $request = $args[0]; $handler = $args[1]; return $handler->handle($request); }); $app->add($middlewareProphecy->reveal()); $app->addMiddleware($middlewareProphecy2->reveal()); $app->get("/", function (ServerRequestInterface $request, $response) { return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $middlewareProphecy->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->shouldHaveBeenCalled(); $middlewareProphecy2->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->shouldHaveBeenCalled(); $this->assertSame($responseProphecy->reveal(), $response); } public function testAddMiddlewareUsingDeferredResolution() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("Hello World"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::cetera())->willReturn($responseProphecy->reveal()); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has("middleware")->willReturn(true); $containerProphecy->get("middleware")->willReturn($middlewareProphecy); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $app->add("middleware"); $app->get("/", function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("Hello World", (string) $response->getBody()); } public function testAddRoutingMiddleware() : void { $responseFactory = $this->prophesize(ResponseFactoryInterface::class)->reveal(); $app = new App($responseFactory); $routingMiddleware = $app->addRoutingMiddleware(); $middlewareDispatcherProperty = new ReflectionProperty(App::class, "middlewareDispatcher"); $middlewareDispatcherProperty->setAccessible(true); $middlewareDispatcher = $middlewareDispatcherProperty->getValue($app); $tipProperty = new ReflectionProperty(MiddlewareDispatcher::class, "tip"); $tipProperty->setAccessible(true); $tip = $tipProperty->getValue($middlewareDispatcher); $reflection = new ReflectionClass($tip); $middlewareProperty = $reflection->getProperty("middleware"); $middlewareProperty->setAccessible(true); $this->assertSame($routingMiddleware, $middlewareProperty->getValue($tip)); $this->assertInstanceOf(RoutingMiddleware::class, $routingMiddleware); } public function testAddErrorMiddleware() : void { $responseFactory = $this->prophesize(ResponseFactoryInterface::class)->reveal(); $logger = $this->prophesize(LoggerInterface::class)->reveal(); $app = new App($responseFactory); $errorMiddleware = $app->addErrorMiddleware(true, true, true, $logger); $middlewareDispatcherProperty = new ReflectionProperty(App::class, "middlewareDispatcher"); $middlewareDispatcherProperty->setAccessible(true); $middlewareDispatcher = $middlewareDispatcherProperty->getValue($app); $tipProperty = new ReflectionProperty(MiddlewareDispatcher::class, "tip"); $tipProperty->setAccessible(true); $tip = $tipProperty->getValue($middlewareDispatcher); $reflection = new ReflectionClass($tip); $middlewareProperty = $reflection->getProperty("middleware"); $middlewareProperty->setAccessible(true); $this->assertSame($errorMiddleware, $middlewareProperty->getValue($tip)); $this->assertInstanceOf(ErrorMiddleware::class, $errorMiddleware); } public function testAddBodyParsingMiddleware() : void { $responseFactory = $this->prophesize(ResponseFactoryInterface::class)->reveal(); $app = new App($responseFactory); $bodyParsingMiddleware = $app->addBodyParsingMiddleware(); $middlewareDispatcherProperty = new ReflectionProperty(App::class, "middlewareDispatcher"); $middlewareDispatcherProperty->setAccessible(true); $middlewareDispatcher = $middlewareDispatcherProperty->getValue($app); $tipProperty = new ReflectionProperty(MiddlewareDispatcher::class, "tip"); $tipProperty->setAccessible(true); $tip = $tipProperty->getValue($middlewareDispatcher); $reflection = new ReflectionClass($tip); $middlewareProperty = $reflection->getProperty("middleware"); $middlewareProperty->setAccessible(true); $this->assertSame($bodyParsingMiddleware, $middlewareProperty->getValue($tip)); $this->assertInstanceOf(BodyParsingMiddleware::class, $bodyParsingMiddleware); } public function testAddMiddlewareOnRoute() : void { $output = ''; $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("Hello World"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "In1"; $response = $handler->handle($request); $output .= "Out1"; return $response; }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "In2"; $response = $handler->handle($request); $output .= "Out2"; return $response; }); $app = new App($responseFactoryProphecy->reveal()); $app->get("/", function (ServerRequestInterface $request, ResponseInterface $response) use(&$output) { $output .= "Center"; return $response; })->add($middlewareProphecy->reveal())->addMiddleware($middlewareProphecy2->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); $this->assertSame("In2In1CenterOut1Out2", $output); } public function testAddMiddlewareOnRouteGroup() : void { $output = ''; $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("Hello World"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "In1"; $response = $handler->handle($request); $output .= "Out1"; return $response; }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "In2"; $response = $handler->handle($request); $output .= "Out2"; return $response; }); $app = new App($responseFactoryProphecy->reveal()); $app->group("/foo", function (RouteCollectorProxy $proxy) use(&$output) { $proxy->get("/bar", function (ServerRequestInterface $request, ResponseInterface $response) use(&$output) { $output .= "Center"; return $response; }); })->add($middlewareProphecy->reveal())->addMiddleware($middlewareProphecy2->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/foo/bar"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); $this->assertSame("In2In1CenterOut1Out2", $output); } public function testAddMiddlewareOnTwoRouteGroup() : void { $output = ''; $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("Hello World"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "In1"; $response = $handler->handle($request); $output .= "Out1"; return $response; }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "In2"; $response = $handler->handle($request); $output .= "Out2"; return $response; }); $middlewareProphecy3 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy3->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "In3"; $response = $handler->handle($request); $output .= "Out3"; return $response; }); $app = new App($responseFactoryProphecy->reveal()); $app->group("/foo", function (RouteCollectorProxyInterface $group) use($middlewareProphecy2, $middlewareProphecy3, &$output) { $group->group("/fizz", function (RouteCollectorProxyInterface $group) { $group->get("/buzz", function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); }); $group->group("/bar", function (RouteCollectorProxyInterface $group) use($middlewareProphecy3, &$output) { $group->get("/baz", function (ServerRequestInterface $request, ResponseInterface $response) use(&$output) { $output .= "Center"; return $response; })->add($middlewareProphecy3->reveal()); })->add($middlewareProphecy2->reveal()); })->add($middlewareProphecy->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/foo/bar/baz"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); $this->assertSame("In1In2In3CenterOut3Out2Out1", $output); } public function testAddMiddlewareAsStringNotImplementingInterfaceThrowsException() : void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage("A middleware must be an object/class name referencing an implementation of " . "MiddlewareInterface or a callable with a matching signature."); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $app->add(new stdClass()); } public function testInvokeReturnMethodNotAllowed() : void { $this->expectException(HttpMethodNotAllowedException::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $app->get("/", function () { }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("POST"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); } public function testInvokeWithMatchingRoute() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("Hello World"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("/", function (ServerRequestInterface $request, $response) { return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("Hello World", (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithSetArgument() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("/", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write("Hello {$args["name"]}"); return $response; })->setArgument("name", "World"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("Hello World", (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithSetArguments() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("/", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write("{$args["greeting"]} {$args["name"]}"); return $response; })->setArguments(array("greeting" => "Hello", "name" => "World")); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("Hello World", (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseStrategy() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("/Hello/{name}", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write("Hello {$args["name"]}"); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/Hello/World"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("Hello World", (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseArgStrategy() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->getRouteCollector()->setDefaultInvocationStrategy(new RequestResponseArgs()); $app->get("/Hello/{name}", function (ServerRequestInterface $request, ResponseInterface $response, $name) { $response->getBody()->write("Hello {$name}"); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/Hello/World"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("Hello World", (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseNamedArgsStrategy() : void { if (PHP_VERSION_ID < 80000) { $this->markTestSkipped("Named arguments are not supported in PHP versions prior to 8.0"); } $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->getRouteCollector()->setDefaultInvocationStrategy(new RequestResponseNamedArgs()); $app->get("/{greeting}/{name}", function (ServerRequestInterface $request, ResponseInterface $response, $name, $greeting) { $response->getBody()->write("{$greeting} {$name}"); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/Hello/World"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("Hello World", (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithNamedParameterOverwritesSetArgument() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("/Hello/{name}", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write("Hello {$args["name"]}"); return $response; })->setArgument("name", "World!"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/Hello/World"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("Hello World", (string) $response->getBody()); } public function testInvokeWithoutMatchingRoute() : void { $this->expectException(HttpNotFoundException::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); } public function testInvokeWithCallableRegisteredInContainer() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("Hello World"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $handler = new class { public function foo(ServerRequestInterface $request, ResponseInterface $response) { return $response; } }; $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has("handler")->willReturn(true); $containerProphecy->get("handler")->willReturn($handler); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $app->get("/", "handler:foo"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("Hello World", (string) $response->getBody()); } public function testInvokeWithNonExistentMethodOnCallableRegisteredInContainer() : void { $this->expectException(RuntimeException::class); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $handler = new class { }; $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has("handler")->willReturn(true); $containerProphecy->get("handler")->willReturn($handler); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $app->get("/", "handler:method_does_not_exist"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); } public function testInvokeWithCallableInContainerViaCallMagicMethod() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $mockAction = new MockAction(); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has("handler")->willReturn(true); $containerProphecy->get("handler")->willReturn($mockAction); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $app->get("/", "handler:foo"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $expectedPayload = json_encode(array("name" => "foo", "arguments" => array())); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame($expectedPayload, (string) $response->getBody()); } public function testInvokeFunctionName() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); function handle($request, ResponseInterface $response) { $response->getBody()->write("Hello World"); return $response; } $app = new App($responseFactoryProphecy->reveal()); $app->get("/", __NAMESPACE__ . "\handle"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("Hello World", (string) $response->getBody()); } public function testCurrentRequestAttributesAreNotLostWhenAddingRouteArguments() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("/Hello/{name}", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write($request->getAttribute("greeting") . " " . $args["name"]); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/Hello/World"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()->withAttribute("greeting", "Hello")); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("Hello World", (string) $response->getBody()); } public function testCurrentRequestAttributesAreNotLostWhenAddingRouteArgumentsRequestResponseArg() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->getRouteCollector()->setDefaultInvocationStrategy(new RequestResponseArgs()); $app->get("/Hello/{name}", function (ServerRequestInterface $request, ResponseInterface $response, $name) { $response->getBody()->write($request->getAttribute("greeting") . " " . $name); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/Hello/World"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()->withAttribute("greeting", "Hello")); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("Hello World", (string) $response->getBody()); } public function testRun() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $streamProphecy->read(1)->willReturn("_"); $streamProphecy->read("11")->will(function () { $this->eof()->willReturn(true); return $this->reveal()->__toString(); }); $streamProphecy->eof()->willReturn(false); $streamProphecy->isSeekable()->willReturn(true); $streamProphecy->rewind()->willReturn(true); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseProphecy->getStatusCode()->willReturn(200); $responseProphecy->getHeaders()->willReturn(array("Content-Length" => array("11"))); $responseProphecy->getProtocolVersion()->willReturn("1.1"); $responseProphecy->getReasonPhrase()->willReturn(''); $responseProphecy->getHeaderLine("Content-Length")->willReturn("11"); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("/", function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write("Hello World"); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->run($requestProphecy->reveal()); $this->expectOutputString("Hello World"); } public function testRunWithoutPassingInServerRequest() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $streamProphecy->read(1)->willReturn("_"); $streamProphecy->read("11")->will(function () { $this->eof()->willReturn(true); return $this->reveal()->__toString(); }); $streamProphecy->eof()->willReturn(false); $streamProphecy->isSeekable()->willReturn(true); $streamProphecy->rewind()->willReturn(true); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseProphecy->getStatusCode()->willReturn(200); $responseProphecy->getHeaders()->willReturn(array("Content-Length" => array("11"))); $responseProphecy->getProtocolVersion()->willReturn("1.1"); $responseProphecy->getReasonPhrase()->willReturn(''); $responseProphecy->getHeaderLine("Content-Length")->willReturn("11"); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("/", function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write("Hello World"); return $response; }); $app->run(); $this->expectOutputString("Hello World"); } public function testHandleReturnsEmptyResponseBodyWithHeadRequestMethod() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseProphecy->withBody(Argument::type(StreamInterface::class))->will(function ($args) { $this->getBody()->willReturn($args[0]); return $this; }); $emptyStreamProphecy = $this->prophesize(StreamInterface::class); $emptyStreamProphecy->__toString()->willReturn(''); $emptyResponseProphecy = $this->prophesize(ResponseInterface::class); $emptyResponseProphecy->getBody()->willReturn($emptyStreamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal(), $emptyResponseProphecy->reveal()); $called = 0; $app = new App($responseFactoryProphecy->reveal()); $app->get("/", function (ServerRequestInterface $request, ResponseInterface $response) use(&$called) { $called++; $response->getBody()->write("Hello World"); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("HEAD"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame(1, $called); $this->assertEmpty((string) $response->getBody()); } public function testCanBeReExecutedRecursivelyDuringDispatch() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseHeaders = array(); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseProphecy->getStatusCode()->willReturn(200); $responseProphecy->getHeader(Argument::type("string"))->will(function ($args) use(&$responseHeaders) { return $responseHeaders[$args[0]]; }); $responseProphecy->withAddedHeader(Argument::type("string"), Argument::type("string"))->will(function ($args) use(&$responseHeaders) { $key = $args[0]; $value = $args[1]; if (!isset($responseHeaders[$key])) { $responseHeaders[$key] = array(); } $responseHeaders[$key][] = $value; return $this; }); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse(Argument::type("integer"))->will(function ($args) use($responseProphecy) { $responseProphecy->getStatusCode()->willReturn($args[0]); return $responseProphecy->reveal(); }); $app = new App($responseFactoryProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use($app, $responseFactoryProphecy) { $request = $args[0]; if ($request->hasHeader("X-NESTED")) { return $responseFactoryProphecy->reveal()->createResponse(204)->withAddedHeader("X-TRACE", "nested"); } $response = $app->handle($request->withAddedHeader("X-NESTED", "1")); return $response->withAddedHeader("X-TRACE", "outer"); }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) { $request = $args[0]; $handler = $args[1]; $response = $handler->handle($request); $response->getBody()->write("1"); return $response; }); $app->add($middlewareProphecy->reveal())->add($middlewareProphecy2->reveal()); $app->get("/", function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $responseHeaders = array(); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->hasHeader(Argument::type("string"))->will(function ($args) use(&$responseHeaders) { return array_key_exists($args[0], $responseHeaders); }); $requestProphecy->withAddedHeader(Argument::type("string"), Argument::type("string"))->will(function ($args) use(&$responseHeaders) { $key = $args[0]; $value = $args[1]; if (!isset($responseHeaders[$key])) { $responseHeaders[$key] = array(); } $responseHeaders[$key][] = $value; return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame(204, $response->getStatusCode()); $this->assertSame(array("nested", "outer"), $response->getHeader("X-TRACE")); $this->assertSame("11", (string) $response->getBody()); } public function testContainerSetToRoute() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("Hello World"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has("handler")->willReturn(true); $containerProphecy->get("handler")->willReturn(function () use($responseProphecy) { return $responseProphecy->reveal(); }); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $routeCollector = $app->getRouteCollector(); $routeCollector->map(array("GET"), "/", "handler"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("Hello World", (string) $response->getBody()); } public function testAppIsARequestHandler() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $this->assertInstanceOf(RequestHandlerInterface::class, $app); } public function testInvokeSequentialProcessToAPathWithOptionalArgsAndWithoutOptionalArgs() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("/Hello[/{name}]", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write((string) count($args)); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/Hello/World"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("1", (string) $response->getBody()); $uriProphecy2 = $this->prophesize(UriInterface::class); $uriProphecy2->getPath()->willReturn("/Hello"); $requestProphecy2 = $this->prophesize(ServerRequestInterface::class); $requestProphecy2->getMethod()->willReturn("GET"); $requestProphecy2->getUri()->willReturn($uriProphecy2->reveal()); $requestProphecy2->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy2->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $streamProphecy->__toString()->willReturn(''); $response = $app->handle($requestProphecy2->reveal()); $this->assertSame("0", (string) $response->getBody()); } public function testInvokeSequentialProcessToAPathWithOptionalArgsAndWithoutOptionalArgsAndKeepSetedArgs() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("/Hello[/{name}]", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write((string) count($args)); return $response; })->setArgument("extra", "value"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/Hello/World"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("2", (string) $response->getBody()); $uriProphecy2 = $this->prophesize(UriInterface::class); $uriProphecy2->getPath()->willReturn("/Hello"); $requestProphecy2 = $this->prophesize(ServerRequestInterface::class); $requestProphecy2->getMethod()->willReturn("GET"); $requestProphecy2->getUri()->willReturn($uriProphecy2->reveal()); $requestProphecy2->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy2->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $streamProphecy->__toString()->willReturn(''); $response = $app->handle($requestProphecy2->reveal()); $this->assertSame("1", (string) $response->getBody()); } public function testInvokeSequentialProcessAfterAddingAnotherRouteArgument() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("string"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $route = $app->get("/Hello[/{name}]", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write((string) count($args)); return $response; })->setArgument("extra", "value"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("/Hello/World"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("GET"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTE)->willReturn($route); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("string"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("2", (string) $response->getBody()); $route->setArgument("extra2", "value2"); $streamProphecy->__toString()->willReturn(''); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("3", (string) $response->getBody()); } } ?>

Did this file decode correctly?

Original Code

<?php
  namespace Slim\Tests; use Prophecy\Argument; use Psr\Container\ContainerInterface; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Log\LoggerInterface; use ReflectionClass; use ReflectionProperty; use RuntimeException; use Slim\App; use Slim\CallableResolver; use Slim\Exception\HttpMethodNotAllowedException; use Slim\Exception\HttpNotFoundException; use Slim\Handlers\Strategies\RequestResponseArgs; use Slim\Handlers\Strategies\RequestResponseNamedArgs; use Slim\Interfaces\CallableResolverInterface; use Slim\Interfaces\MiddlewareDispatcherInterface; use Slim\Interfaces\RouteCollectorInterface; use Slim\Interfaces\RouteCollectorProxyInterface; use Slim\Interfaces\RouteParserInterface; use Slim\Middleware\BodyParsingMiddleware; use Slim\Middleware\ErrorMiddleware; use Slim\Middleware\RoutingMiddleware; use Slim\MiddlewareDispatcher; use Slim\Routing\RouteCollector; use Slim\Routing\RouteCollectorProxy; use Slim\Routing\RouteContext; use Slim\Tests\Mocks\MockAction; use stdClass; use function array_key_exists; use function array_shift; use function count; use function ini_set; use function json_encode; use function strtolower; use function sys_get_temp_dir; use function tempnam; class AppTest extends TestCase { public static function setupBeforeClass() : void { ini_set("\x65\x72\x72\157\162\137\154\157\147", tempnam(sys_get_temp_dir(), "\x73\x6c\x69\155")); } public function testDoesNotUseContainerAsServiceLocator() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $containerProphecy->has(Argument::type("\163\x74\x72\151\156\147"))->shouldNotHaveBeenCalled(); $containerProphecy->get(Argument::type("\x73\x74\x72\x69\156\x67"))->shouldNotHaveBeenCalled(); } public function testGetContainer() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $this->assertSame($containerProphecy->reveal(), $app->getContainer()); } public function testGetCallableResolverReturnsInjectedInstance() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $app = new App($responseFactoryProphecy->reveal(), null, $callableResolverProphecy->reveal()); $this->assertSame($callableResolverProphecy->reveal(), $app->getCallableResolver()); } public function testCreatesCallableResolverWhenNull() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $callableResolver = new CallableResolver($containerProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal(), null); $this->assertEquals($callableResolver, $app->getCallableResolver()); } public function testGetRouteCollectorReturnsInjectedInstance() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeParserProphecy = $this->prophesize(RouteParserInterface::class); $routeCollectorProphecy->getRouteParser()->willReturn($routeParserProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal(), null, null, $routeCollectorProphecy->reveal()); $this->assertSame($routeCollectorProphecy->reveal(), $app->getRouteCollector()); } public function testCreatesRouteCollectorWhenNullWithInjectedContainer() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), $containerProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal(), $callableResolverProphecy->reveal()); $this->assertEquals($routeCollector, $app->getRouteCollector()); } public function testGetMiddlewareDispatcherGetsSeededAndReturnsInjectedInstance() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $middlewareDispatcherProphecy = $this->prophesize(MiddlewareDispatcherInterface::class); $middlewareDispatcherProphecy->seedMiddlewareStack(Argument::any())->shouldBeCalledOnce(); $app = new App($responseFactoryProphecy->reveal(), null, null, null, null, $middlewareDispatcherProphecy->reveal()); $this->assertSame($middlewareDispatcherProphecy->reveal(), $app->getMiddlewareDispatcher()); } public function lowerCaseRequestMethodsProvider() : array { return array(array("\x67\x65\x74"), array("\160\x6f\x73\164"), array("\160\165\164"), array("\160\141\164\x63\150"), array("\x64\x65\154\145\x74\145"), array("\x6f\160\164\151\x6f\x6e\163")); } public function testGetPostPutPatchDeleteOptionsMethods(string $method) : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("\x48\145\154\154\157\x20\127\x6f\x72\154\x64"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn($method); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\x74\x72\x69\x6e\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $methodName = strtolower($method); $app = new App($responseFactoryProphecy->reveal()); $app->{$methodName}("\57", function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("\x48\145\x6c\154\157\40\x57\x6f\162\154\x64", (string) $response->getBody()); } public function testAnyRoute() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("\110\x65\154\154\157\x20\x57\157\162\x6c\x64"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->any("\57", function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); foreach ($this->upperCaseRequestMethodsProvider() as $methods) { $method = $methods[0]; $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn($method); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\x74\x72\151\156\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("\x48\x65\x6c\x6c\157\x20\127\157\x72\x6c\144", (string) $response->getBody()); } } public function upperCaseRequestMethodsProvider() : array { return array(array("\x47\105\x54"), array("\120\x4f\123\x54"), array("\120\x55\124"), array("\x50\101\124\103\110"), array("\104\105\114\105\x54\105"), array("\x4f\x50\124\111\x4f\116\123")); } public function testMapRoute(string $method) : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("\110\x65\x6c\154\x6f\40\127\x6f\x72\x6c\x64"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\57"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn($method); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\164\162\151\156\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app = new App($responseFactoryProphecy->reveal()); $app->map(array($method), "\57", function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("\110\x65\154\x6c\x6f\x20\x57\x6f\x72\x6c\144", (string) $response->getBody()); } public function testRedirectRoute() : void { $from = "\57\146\162\157\155"; $to = "\57\x74\157"; $routeCreatedResponse = $this->prophesize(ResponseInterface::class); $handlerCreatedResponse = $this->prophesize(ResponseInterface::class); $handlerCreatedResponse->getStatusCode()->willReturn(301); $handlerCreatedResponse->getHeaderLine("\114\157\143\x61\164\x69\157\156")->willReturn($to); $handlerCreatedResponse->withHeader(Argument::type("\163\x74\162\151\x6e\x67"), Argument::type("\x73\x74\162\x69\x6e\147"))->will(function ($args) { $this->getHeader($args[0])->willReturn($args[1]); return $this; }); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($routeCreatedResponse->reveal()); $responseFactoryProphecy->createResponse(301)->willReturn($handlerCreatedResponse->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn($from); $uriProphecy->__toString()->willReturn($to); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\x74\162\151\x6e\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app = new App($responseFactoryProphecy->reveal()); $app->redirect($from, $to, 301); $response = $app->handle($requestProphecy->reveal()); $responseFactoryProphecy->createResponse(301)->shouldHaveBeenCalled(); $this->assertSame(301, $response->getStatusCode()); $this->assertSame($to, $response->getHeaderLine("\114\157\143\x61\x74\151\157\x6e")); } public function testRouteWithInternationalCharacters() : void { $path = "\57\xd0\xbd\320\276\xd0\xb2\xd0\xbe\xd1\x81\321\202\320\270"; $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("\110\x65\154\154\157\40\127\157\x72\154\144"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get($path, function () use($responseProphecy) { return $responseProphecy->reveal(); }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn($path); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\105\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\164\162\x69\156\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("\x48\145\x6c\x6c\x6f\x20\127\x6f\162\154\144", (string) $response->getBody()); } public function routePatternsProvider() : array { return array(array(''), array("\57"), array("\x66\x6f\x6f"), array("\57\146\157\157"), array("\x2f\146\x6f\157\x2f")); } public function testRoutePatterns(string $pattern) : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $app->get($pattern, function () { }); $routeCollector = $app->getRouteCollector(); $route = $routeCollector->lookupRoute("\162\157\165\164\x65\60"); $this->assertSame($pattern, $route->getPattern()); } public function routeGroupsDataProvider() : array { return array("\145\x6d\160\x74\x79\x20\x67\162\157\x75\x70\40\167\x69\x74\150\40\145\155\160\x74\171\x20\162\157\165\164\145" => array(array('', ''), ''), "\x65\x6d\160\164\x79\x20\x67\x72\x6f\x75\x70\x20\x77\151\x74\x68\x20\163\x69\156\x67\154\145\x20\163\x6c\x61\x73\x68\40\162\157\x75\164\145" => array(array('', "\57"), "\57"), "\x65\x6d\x70\164\171\x20\147\162\157\x75\x70\40\x77\151\x74\150\x20\163\145\147\x6d\145\156\x74\x20\x72\157\x75\164\145\40\x74\x68\x61\164\x20\144\x6f\145\x73\40\156\157\x74\40\x65\156\144\40\x69\x6e\x20\x61\x53\x6c\x61\x73\x68" => array(array('', "\x2f\146\157\157"), "\x2f\x66\157\x6f"), "\x65\x6d\x70\x74\x79\x20\147\x72\157\165\160\x20\167\x69\164\150\40\x73\145\147\x6d\145\x6e\x74\40\162\157\165\x74\145\x20\x74\150\x61\x74\40\145\x6e\144\163\x20\151\x6e\40\x61\123\154\x61\163\150" => array(array('', "\x2f\146\x6f\157\x2f"), "\x2f\x66\157\x6f\x2f"), "\x67\x72\x6f\165\x70\x20\x73\x69\x6e\x67\154\x65\x20\163\154\x61\163\x68\40\167\151\164\x68\x20\145\x6d\x70\164\171\x20\x72\x6f\x75\x74\x65" => array(array("\57", ''), "\57"), "\147\162\157\165\160\40\163\x69\156\x67\x6c\x65\x20\x73\x6c\141\163\x68\40\167\x69\164\x68\x20\x73\151\x6e\147\x6c\145\x20\x73\154\x61\x73\150\40\x72\157\x75\x74\x65" => array(array("\57", "\x2f"), "\x2f\x2f"), "\x67\162\157\165\x70\x20\163\151\156\x67\x6c\x65\x20\163\x6c\x61\x73\x68\40\x77\151\x74\150\x20\163\145\x67\155\145\156\164\40\162\x6f\165\x74\x65\x20\x74\150\141\164\x20\x64\x6f\145\x73\40\156\x6f\164\40\x65\x6e\x64\x20\151\156\40\141\123\x6c\141\163\x68" => array(array("\x2f", "\x2f\146\157\157"), "\x2f\x2f\x66\x6f\157"), "\x67\x72\157\x75\x70\40\x73\151\156\x67\x6c\x65\40\x73\x6c\141\x73\x68\40\167\151\x74\150\x20\163\x65\x67\x6d\145\x6e\164\40\162\157\x75\164\x65\40\164\150\x61\x74\x20\x65\156\x64\163\40\x69\156\40\x61\123\x6c\141\163\150" => array(array("\x2f", "\57\146\157\x6f\57"), "\x2f\x2f\x66\157\x6f\x2f"), "\147\162\157\165\x70\x20\x73\x65\147\155\145\x6e\164\x20\x77\151\164\150\x20\x65\155\x70\x74\x79\40\x72\157\165\x74\145" => array(array("\x2f\146\157\x6f", ''), "\x2f\x66\x6f\x6f"), "\x67\x72\x6f\165\160\40\x73\x65\x67\x6d\145\156\164\x20\x77\x69\x74\x68\x20\163\x69\156\147\154\x65\40\163\154\x61\163\150\x20\x72\157\165\x74\145" => array(array("\x2f\x66\x6f\157", "\57"), "\57\146\157\x6f\57"), "\x67\x72\x6f\x75\160\40\x73\x65\147\x6d\145\156\x74\40\x77\x69\x74\150\x20\x73\x65\147\x6d\145\156\164\x20\162\x6f\165\164\145\40\x74\x68\x61\164\x20\144\x6f\145\x73\x20\x6e\x6f\164\40\145\156\144\x20\x69\156\40\141\123\154\141\163\150" => array(array("\57\146\157\x6f", "\x2f\x62\141\x72"), "\57\146\157\157\57\x62\x61\162"), "\147\162\x6f\165\x70\40\163\x65\147\155\x65\x6e\164\x20\x77\x69\x74\x68\x20\x73\x65\147\155\x65\156\x74\40\162\157\x75\164\145\40\164\x68\x61\x74\40\x65\x6e\144\163\x20\151\x6e\40\x61\123\x6c\141\x73\x68" => array(array("\x2f\146\157\157", "\57\142\141\x72\57"), "\57\x66\x6f\x6f\x2f\142\x61\x72\57"), "\145\x6d\x70\164\x79\40\147\x72\x6f\x75\160\40\x77\x69\164\150\40\x6e\145\x73\x74\x65\144\40\x67\x72\157\x75\160\40\163\145\147\x6d\x65\x6e\164\40\x77\x69\x74\x68\x20\141\156\40\145\155\160\164\171\40\x72\x6f\x75\164\145" => array(array('', "\57\146\157\x6f", ''), "\x2f\146\157\x6f"), "\145\x6d\x70\x74\x79\x20\x67\x72\x6f\x75\x70\40\167\151\x74\150\40\x6e\145\x73\x74\145\x64\40\x67\x72\x6f\165\x70\40\163\x65\x67\155\x65\x6e\x74\x20\167\x69\164\150\x20\x73\x69\x6e\147\x6c\x65\40\163\154\x61\163\150\40\x72\157\165\164\x65" => array(array('', "\x2f\x66\157\x6f", "\57"), "\57\146\157\157\x2f"), "\147\x72\157\165\x70\40\x73\x69\156\147\154\145\x20\163\154\x61\163\x68\40\x77\151\x74\x68\x20\x65\x6d\x70\x74\x79\x20\156\145\163\164\145\144\x20\x67\162\157\x75\160\40\x61\x6e\x64\40\163\145\147\155\x65\156\x74\x20\x72\x6f\x75\164\145\x20\x77\x69\x74\x68\157\x75\x74\x20\x6c\x65\x61\144\151\x6e\x67\40\163\154\141\x73\150" => array(array("\x2f", '', "\146\157\157"), "\57\x66\157\x6f"), "\147\x72\157\x75\x70\x20\163\x69\156\147\x6c\x65\40\x73\154\x61\163\150\x20\x77\x69\x74\x68\x20\x65\x6d\x70\164\x79\40\x6e\x65\x73\x74\x65\x64\x20\x67\x72\x6f\x75\160\40\141\x6e\144\40\163\x65\147\155\x65\x6e\164\40\162\x6f\x75\164\x65" => array(array("\x2f", '', "\x2f\x66\x6f\157"), "\x2f\x2f\146\x6f\x6f"), "\x67\162\x6f\165\160\x20\163\151\156\x67\x6c\x65\40\x73\x6c\141\x73\x68\40\x77\x69\164\x68\40\163\x69\x6e\x67\x6c\145\x20\x73\154\141\163\150\40\x67\162\157\x75\160\40\141\x6e\x64\x20\x73\145\147\155\x65\x6e\164\40\x72\x6f\x75\164\x65\40\x77\x69\164\150\157\165\164\x20\154\145\x61\x64\151\x6e\147\40\163\x6c\141\163\x68" => array(array("\57", "\57", "\x66\x6f\x6f"), "\x2f\x2f\146\x6f\157"), "\147\162\157\165\x70\x20\163\151\156\147\154\x65\x20\x73\154\141\x73\x68\x20\167\x69\164\x68\40\x73\x69\x6e\x67\x6c\x65\40\x73\x6c\141\x73\150\x20\156\145\x73\164\x65\144\40\147\162\157\x75\160\40\x61\156\x64\x20\x73\145\x67\x6d\x65\x6e\x74\40\x72\157\165\x74\145" => array(array("\x2f", "\x2f", "\x2f\146\157\x6f"), "\57\57\57\x66\157\x6f"), "\x67\162\157\165\160\40\x73\x69\x6e\x67\154\145\x20\x73\154\x61\x73\150\x20\x77\151\x74\150\x20\x6e\145\163\164\x65\x64\x20\147\162\157\x75\x70\40\163\x65\x67\155\145\x6e\x74\x20\167\x69\164\x68\40\141\x6e\40\145\155\x70\x74\x79\x20\162\157\x75\x74\x65" => array(array("\57", "\x2f\x66\x6f\x6f", ''), "\x2f\57\146\157\x6f"), "\x67\162\x6f\x75\x70\x20\163\151\156\147\154\x65\40\163\x6c\141\163\x68\x20\167\x69\x74\150\40\156\145\163\x74\145\144\x20\147\162\x6f\x75\160\x20\163\x65\147\155\x65\156\164\x20\167\x69\164\150\x20\163\x69\x6e\x67\x6c\145\x20\x73\154\x61\163\x68\x20\162\157\x75\164\145" => array(array("\x2f", "\57\146\x6f\157", "\x2f"), "\x2f\57\146\157\x6f\x2f"), "\147\162\157\x75\x70\x20\x73\151\156\147\x6c\145\x20\163\154\x61\x73\150\x20\167\x69\164\x68\x20\x6e\x65\x73\x74\x65\144\x20\147\162\157\165\160\x20\163\x65\x67\155\145\x6e\164\x20\x77\x69\164\150\x20\x73\145\147\155\x65\156\164\x20\x72\x6f\165\x74\145" => array(array("\x2f", "\57\x66\157\x6f", "\57\x62\x61\x72"), "\57\57\146\x6f\157\57\142\x61\x72"), "\147\x72\157\165\x70\x20\x73\151\x6e\147\154\145\x20\x73\154\141\x73\150\40\x77\x69\x74\x68\40\156\145\x73\x74\x65\x64\40\x67\162\x6f\165\160\40\x73\145\x67\155\145\156\164\40\167\x69\164\150\x20\x73\x65\147\x6d\x65\x6e\164\x20\x72\x6f\x75\x74\x65\x20\164\150\x61\x74\40\150\x61\163\40\x61\124\x72\141\x69\154\151\156\147\40\x73\x6c\141\x73\150" => array(array("\57", "\x2f\146\x6f\x6f", "\57\x62\x61\x72\x2f"), "\57\x2f\x66\157\157\x2f\x62\141\162\57"), "\145\155\x70\164\171\x20\x67\x72\x6f\165\160\40\167\151\164\150\x20\145\155\160\164\171\40\x6e\145\x73\164\x65\144\x20\x67\x72\x6f\x75\x70\40\x61\156\144\40\163\x65\147\155\145\156\x74\40\162\157\165\x74\x65\x20\x77\151\x74\150\157\165\x74\40\154\145\141\x64\151\156\x67\x20\x73\x6c\141\x73\x68" => array(array('', '', "\x66\157\x6f"), "\x66\x6f\157"), "\145\155\x70\164\x79\x20\147\x72\157\x75\x70\40\x77\151\x74\x68\x20\x65\x6d\160\x74\x79\x20\x6e\x65\163\164\145\144\40\x67\162\x6f\165\x70\40\x61\x6e\144\40\163\x65\147\155\145\156\x74\40\162\157\x75\x74\145" => array(array('', '', "\x2f\146\157\157"), "\x2f\x66\x6f\x6f"), "\145\155\x70\164\171\40\x67\162\157\165\x70\40\x77\151\164\150\x20\163\x69\x6e\x67\154\x65\40\163\x6c\141\163\x68\40\147\162\157\165\160\40\x61\156\x64\x20\163\145\147\x6d\145\x6e\164\x20\x72\x6f\165\x74\x65\40\x77\x69\x74\150\x6f\165\x74\40\x6c\145\x61\144\x69\x6e\147\x20\163\154\x61\163\x68" => array(array('', "\57", "\146\157\x6f"), "\57\146\157\157"), "\145\155\x70\x74\171\x20\147\x72\157\165\x70\40\167\151\x74\x68\x20\163\x69\x6e\147\x6c\x65\x20\x73\154\141\x73\150\x20\156\145\x73\x74\x65\x64\40\x67\x72\157\165\160\x20\141\156\144\40\163\x65\147\x6d\145\x6e\164\40\x72\157\x75\x74\145" => array(array('', "\57", "\57\146\x6f\x6f"), "\x2f\x2f\146\x6f\157"), "\145\x6d\x70\164\171\x20\x67\x72\157\165\x70\x20\167\x69\164\150\40\156\145\x73\164\x65\144\40\147\x72\x6f\x75\x70\40\x73\x65\147\x6d\x65\156\x74\40\167\151\x74\150\40\x73\x65\x67\155\145\x6e\164\x20\x72\x6f\165\164\145" => array(array('', "\x2f\146\157\x6f", "\57\142\x61\162"), "\x2f\x66\x6f\157\57\x62\141\162"), "\x65\155\160\x74\x79\40\147\162\157\x75\x70\x20\x77\x69\164\150\x20\156\145\x73\x74\x65\x64\x20\x67\x72\x6f\x75\x70\x20\163\145\147\x6d\145\156\164\x20\x77\151\164\x68\40\x73\145\147\155\x65\156\x74\40\162\x6f\165\x74\x65\x20\x74\150\x61\x74\40\150\x61\163\x20\141\x54\x72\141\x69\154\151\156\x67\x20\163\x6c\x61\x73\150" => array(array('', "\x2f\x66\157\157", "\x2f\142\141\x72\x2f"), "\x2f\x66\x6f\x6f\x2f\142\x61\x72\57"), "\x67\162\x6f\x75\160\40\163\145\147\x6d\145\x6e\164\x20\x77\151\x74\150\x20\145\x6d\160\164\171\40\156\145\163\164\x65\x64\40\x67\x72\157\x75\160\x20\141\156\144\x20\x73\x65\x67\155\x65\x6e\164\40\162\x6f\x75\164\x65\40\x77\x69\164\150\x6f\x75\x74\x20\154\x65\x61\x64\x69\156\147\40\163\154\141\163\150" => array(array("\x2f\146\x6f\x6f", '', "\142\141\x72"), "\57\x66\157\x6f\x62\x61\x72"), "\147\x72\x6f\165\x70\40\163\145\x67\155\x65\x6e\164\40\167\151\x74\150\x20\145\x6d\x70\x74\171\x20\156\145\x73\164\x65\x64\40\147\x72\x6f\x75\160\40\x61\x6e\144\40\x73\x65\x67\x6d\145\x6e\164\x20\162\157\165\164\x65" => array(array("\x2f\146\x6f\x6f", '', "\x2f\142\141\162"), "\57\x66\157\157\57\142\141\162"), "\147\x72\157\x75\160\x20\x73\145\147\155\145\156\x74\40\x77\151\x74\150\x20\x73\x69\x6e\x67\x6c\145\40\x73\154\x61\163\150\x20\156\x65\x73\164\145\144\40\147\x72\157\165\160\x20\141\156\144\x20\163\x65\147\155\145\x6e\164\40\162\x6f\165\164\145" => array(array("\x2f\146\157\x6f", "\57", "\x62\x61\x72"), "\x2f\146\157\157\x2f\142\141\x72"), "\x67\162\157\x75\160\40\x73\145\x67\x6d\145\x6e\164\40\x77\x69\x74\x68\40\163\151\156\147\x6c\145\x20\x73\154\x61\x73\x68\x20\156\x65\x73\x74\x65\x64\40\147\x72\x6f\x75\x70\x20\141\x6e\x64\40\x73\x6c\141\x73\150\x20\x73\x65\147\155\x65\156\164\40\x72\157\x75\164\x65" => array(array("\57\146\x6f\x6f", "\x2f", "\x2f\142\141\162"), "\x2f\x66\x6f\157\x2f\x2f\142\141\x72"), "\164\x77\157\x20\x67\x72\157\165\x70\x20\163\145\147\155\x65\156\164\x73\x20\167\151\x74\150\x20\145\155\x70\164\x79\40\162\157\x75\x74\145" => array(array("\57\146\157\x6f", "\x2f\142\x61\162", ''), "\x2f\x66\157\x6f\57\142\x61\x72"), "\x74\x77\x6f\x20\147\162\x6f\165\x70\x20\163\x65\x67\155\145\x6e\164\x73\x20\x77\x69\x74\150\x20\x73\x69\156\x67\154\145\x20\163\x6c\x61\x73\x68\x20\162\157\165\164\x65" => array(array("\x2f\x66\x6f\157", "\57\x62\141\162", "\x2f"), "\x2f\x66\x6f\157\x2f\x62\141\162\x2f"), "\164\x77\157\x20\147\162\157\165\160\40\x73\145\147\155\x65\x6e\x74\x73\40\167\x69\x74\x68\x20\x73\145\x67\x6d\x65\156\164\40\162\x6f\165\x74\x65" => array(array("\57\146\157\157", "\x2f\x62\x61\x72", "\x2f\x62\x61\172"), "\57\x66\x6f\x6f\x2f\142\141\162\x2f\142\141\172"), "\164\167\x6f\x20\147\162\157\165\x70\x20\163\145\147\x6d\145\156\164\x73\x20\x77\151\x74\x68\x20\163\x65\x67\x6d\x65\156\x74\40\x72\x6f\x75\164\145\x20\x74\150\x61\164\40\x68\x61\x73\x20\141\124\162\x61\x69\x6c\x69\x6e\x67\40\163\x6c\141\163\150" => array(array("\x2f\146\157\x6f", "\x2f\142\141\162", "\57\142\141\172\x2f"), "\x2f\x66\x6f\157\57\142\141\162\57\x62\x61\172\57")); } public function testGroupClosureIsBoundToThisClass() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $testCase = $this; $app->group("\x2f\146\x6f\157", function () use($testCase) { $testCase->assertSame($testCase, $this); }); } public function testRouteGroupCombinations(array $sequence, string $expectedPath) : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $processSequence = function (RouteCollectorProxy $app, array $sequence, $processSequence) { $path = array_shift($sequence); if (count($sequence)) { $app->group($path, function (RouteCollectorProxy $group) use(&$sequence, $processSequence) { $processSequence($group, $sequence, $processSequence); }); } else { $app->get($path, function () { }); } }; $processSequence($app, $sequence, $processSequence); $routeCollector = $app->getRouteCollector(); $route = $routeCollector->lookupRoute("\x72\157\165\x74\x65\x30"); $this->assertSame($expectedPath, $route->getPattern()); } public function testRouteGroupPattern() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryInterface = $responseFactoryProphecy->reveal(); $app = new App($responseFactoryInterface); $group = $app->group("\x2f\146\x6f\x6f", function () { }); $this->assertSame("\x2f\x66\x6f\157", $group->getPattern()); } public function testAddMiddleware() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("\110\145\154\x6c\157\40\127\157\162\x6c\144"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::cetera())->will(function () use($responseProphecy) { return $responseProphecy->reveal(); }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) { $request = $args[0]; $handler = $args[1]; return $handler->handle($request); }); $app->add($middlewareProphecy->reveal()); $app->addMiddleware($middlewareProphecy2->reveal()); $app->get("\x2f", function (ServerRequestInterface $request, $response) { return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\x74\x72\151\x6e\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $middlewareProphecy->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->shouldHaveBeenCalled(); $middlewareProphecy2->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->shouldHaveBeenCalled(); $this->assertSame($responseProphecy->reveal(), $response); } public function testAddMiddlewareUsingDeferredResolution() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("\110\x65\x6c\x6c\157\40\x57\x6f\162\154\144"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::cetera())->willReturn($responseProphecy->reveal()); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has("\x6d\151\144\x64\154\145\167\141\x72\x65")->willReturn(true); $containerProphecy->get("\x6d\x69\x64\x64\154\145\x77\141\x72\145")->willReturn($middlewareProphecy); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $app->add("\x6d\151\144\144\x6c\145\x77\141\162\x65"); $app->get("\57", function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("\x48\x65\x6c\154\x6f\40\127\x6f\162\154\x64", (string) $response->getBody()); } public function testAddRoutingMiddleware() : void { $responseFactory = $this->prophesize(ResponseFactoryInterface::class)->reveal(); $app = new App($responseFactory); $routingMiddleware = $app->addRoutingMiddleware(); $middlewareDispatcherProperty = new ReflectionProperty(App::class, "\x6d\x69\x64\x64\154\145\167\x61\x72\x65\x44\x69\x73\160\141\x74\143\150\145\x72"); $middlewareDispatcherProperty->setAccessible(true); $middlewareDispatcher = $middlewareDispatcherProperty->getValue($app); $tipProperty = new ReflectionProperty(MiddlewareDispatcher::class, "\164\151\160"); $tipProperty->setAccessible(true); $tip = $tipProperty->getValue($middlewareDispatcher); $reflection = new ReflectionClass($tip); $middlewareProperty = $reflection->getProperty("\x6d\x69\144\144\x6c\145\167\x61\x72\x65"); $middlewareProperty->setAccessible(true); $this->assertSame($routingMiddleware, $middlewareProperty->getValue($tip)); $this->assertInstanceOf(RoutingMiddleware::class, $routingMiddleware); } public function testAddErrorMiddleware() : void { $responseFactory = $this->prophesize(ResponseFactoryInterface::class)->reveal(); $logger = $this->prophesize(LoggerInterface::class)->reveal(); $app = new App($responseFactory); $errorMiddleware = $app->addErrorMiddleware(true, true, true, $logger); $middlewareDispatcherProperty = new ReflectionProperty(App::class, "\x6d\151\x64\x64\x6c\x65\167\141\x72\145\x44\x69\163\x70\x61\164\x63\x68\145\162"); $middlewareDispatcherProperty->setAccessible(true); $middlewareDispatcher = $middlewareDispatcherProperty->getValue($app); $tipProperty = new ReflectionProperty(MiddlewareDispatcher::class, "\164\151\160"); $tipProperty->setAccessible(true); $tip = $tipProperty->getValue($middlewareDispatcher); $reflection = new ReflectionClass($tip); $middlewareProperty = $reflection->getProperty("\x6d\151\144\144\x6c\145\x77\141\x72\x65"); $middlewareProperty->setAccessible(true); $this->assertSame($errorMiddleware, $middlewareProperty->getValue($tip)); $this->assertInstanceOf(ErrorMiddleware::class, $errorMiddleware); } public function testAddBodyParsingMiddleware() : void { $responseFactory = $this->prophesize(ResponseFactoryInterface::class)->reveal(); $app = new App($responseFactory); $bodyParsingMiddleware = $app->addBodyParsingMiddleware(); $middlewareDispatcherProperty = new ReflectionProperty(App::class, "\155\151\144\x64\154\145\167\x61\x72\x65\x44\151\x73\x70\x61\x74\143\x68\x65\x72"); $middlewareDispatcherProperty->setAccessible(true); $middlewareDispatcher = $middlewareDispatcherProperty->getValue($app); $tipProperty = new ReflectionProperty(MiddlewareDispatcher::class, "\x74\x69\x70"); $tipProperty->setAccessible(true); $tip = $tipProperty->getValue($middlewareDispatcher); $reflection = new ReflectionClass($tip); $middlewareProperty = $reflection->getProperty("\155\x69\x64\x64\x6c\145\x77\x61\162\145"); $middlewareProperty->setAccessible(true); $this->assertSame($bodyParsingMiddleware, $middlewareProperty->getValue($tip)); $this->assertInstanceOf(BodyParsingMiddleware::class, $bodyParsingMiddleware); } public function testAddMiddlewareOnRoute() : void { $output = ''; $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("\110\145\x6c\154\157\40\x57\x6f\x72\154\x64"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "\111\x6e\61"; $response = $handler->handle($request); $output .= "\117\165\x74\x31"; return $response; }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "\x49\156\62"; $response = $handler->handle($request); $output .= "\x4f\x75\164\62"; return $response; }); $app = new App($responseFactoryProphecy->reveal()); $app->get("\57", function (ServerRequestInterface $request, ResponseInterface $response) use(&$output) { $output .= "\x43\x65\156\164\x65\x72"; return $response; })->add($middlewareProphecy->reveal())->addMiddleware($middlewareProphecy2->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\105\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\x74\162\151\156\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); $this->assertSame("\x49\x6e\62\x49\156\x31\x43\145\156\164\x65\x72\117\165\164\x31\117\165\x74\62", $output); } public function testAddMiddlewareOnRouteGroup() : void { $output = ''; $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("\110\x65\154\154\x6f\x20\127\157\162\154\144"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "\x49\156\61"; $response = $handler->handle($request); $output .= "\117\165\x74\x31"; return $response; }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "\x49\x6e\x32"; $response = $handler->handle($request); $output .= "\117\x75\x74\x32"; return $response; }); $app = new App($responseFactoryProphecy->reveal()); $app->group("\x2f\x66\x6f\157", function (RouteCollectorProxy $proxy) use(&$output) { $proxy->get("\x2f\142\141\x72", function (ServerRequestInterface $request, ResponseInterface $response) use(&$output) { $output .= "\x43\145\x6e\164\145\x72"; return $response; }); })->add($middlewareProphecy->reveal())->addMiddleware($middlewareProphecy2->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f\146\157\157\x2f\x62\x61\x72"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\x47\105\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\164\x72\x69\x6e\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); $this->assertSame("\x49\156\62\x49\156\61\x43\145\156\x74\x65\x72\117\165\x74\x31\117\x75\164\x32", $output); } public function testAddMiddlewareOnTwoRouteGroup() : void { $output = ''; $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("\110\x65\x6c\154\157\x20\x57\157\x72\154\144"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "\x49\156\61"; $response = $handler->handle($request); $output .= "\117\165\x74\x31"; return $response; }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "\x49\x6e\62"; $response = $handler->handle($request); $output .= "\x4f\x75\x74\x32"; return $response; }); $middlewareProphecy3 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy3->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use(&$output) { $request = $args[0]; $handler = $args[1]; $output .= "\x49\156\63"; $response = $handler->handle($request); $output .= "\x4f\165\164\x33"; return $response; }); $app = new App($responseFactoryProphecy->reveal()); $app->group("\x2f\x66\157\x6f", function (RouteCollectorProxyInterface $group) use($middlewareProphecy2, $middlewareProphecy3, &$output) { $group->group("\57\x66\x69\x7a\172", function (RouteCollectorProxyInterface $group) { $group->get("\x2f\x62\165\x7a\x7a", function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); }); $group->group("\x2f\142\x61\x72", function (RouteCollectorProxyInterface $group) use($middlewareProphecy3, &$output) { $group->get("\x2f\142\141\172", function (ServerRequestInterface $request, ResponseInterface $response) use(&$output) { $output .= "\x43\x65\156\x74\x65\x72"; return $response; })->add($middlewareProphecy3->reveal()); })->add($middlewareProphecy2->reveal()); })->add($middlewareProphecy->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f\x66\157\x6f\x2f\142\141\162\57\142\x61\172"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\105\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\164\x72\151\156\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); $this->assertSame("\x49\156\x31\x49\x6e\x32\x49\x6e\63\103\145\156\164\x65\162\x4f\x75\x74\x33\117\165\164\x32\x4f\165\164\61", $output); } public function testAddMiddlewareAsStringNotImplementingInterfaceThrowsException() : void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage("\101\x20\x6d\x69\144\x64\x6c\x65\167\x61\162\145\x20\x6d\x75\x73\164\x20\x62\145\x20\x61\x6e\40\x6f\142\x6a\x65\143\164\57\143\154\x61\x73\163\40\156\141\x6d\145\40\x72\x65\146\x65\x72\145\x6e\x63\151\156\x67\x20\x61\156\40\x69\155\160\154\145\155\145\x6e\164\141\164\151\x6f\x6e\40\157\x66\x20" . "\x4d\x69\144\144\154\145\167\x61\x72\145\x49\x6e\x74\145\x72\146\x61\x63\145\x20\x6f\162\40\141\40\x63\141\x6c\154\141\142\x6c\x65\x20\167\151\x74\150\40\141\40\x6d\141\164\143\x68\x69\156\147\x20\163\151\147\156\141\164\165\x72\145\56"); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $app->add(new stdClass()); } public function testInvokeReturnMethodNotAllowed() : void { $this->expectException(HttpMethodNotAllowedException::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $app->get("\x2f", function () { }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\120\x4f\x53\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\x74\162\151\156\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); } public function testInvokeWithMatchingRoute() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("\110\145\x6c\154\157\40\x57\x6f\162\x6c\x64"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("\57", function (ServerRequestInterface $request, $response) { return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\57"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\105\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\x74\162\x69\156\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("\110\x65\x6c\x6c\157\40\x57\x6f\162\154\x64", (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithSetArgument() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\163\x74\x72\151\x6e\147"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("\57", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write("\x48\x65\x6c\x6c\x6f\x20{$args["\x6e\x61\155\145"]}"); return $response; })->setArgument("\x6e\141\155\x65", "\127\x6f\x72\154\x64"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\x47\x45\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\x74\x72\x69\x6e\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("\x48\x65\x6c\154\x6f\x20\127\x6f\162\154\x64", (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithSetArguments() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\163\164\x72\x69\x6e\147"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("\57", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write("{$args["\x67\162\x65\x65\164\x69\156\x67"]}\40{$args["\156\x61\155\x65"]}"); return $response; })->setArguments(array("\147\162\145\145\x74\x69\x6e\147" => "\110\145\x6c\x6c\x6f", "\x6e\141\155\145" => "\x57\157\162\154\x64")); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\57"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\x47\x45\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\x74\162\151\x6e\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("\x48\x65\x6c\154\157\40\x57\x6f\162\x6c\144", (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseStrategy() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\x73\164\162\x69\x6e\x67"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("\x2f\x48\x65\154\154\x6f\x2f\x7b\156\x61\155\145\x7d", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write("\110\145\154\x6c\157\x20{$args["\156\141\x6d\x65"]}"); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f\x48\145\154\x6c\x6f\57\127\157\x72\x6c\144"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\x47\x45\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\x74\162\151\156\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("\x48\x65\x6c\154\157\40\x57\157\x72\x6c\x64", (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseArgStrategy() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\163\x74\162\151\156\x67"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->getRouteCollector()->setDefaultInvocationStrategy(new RequestResponseArgs()); $app->get("\57\x48\145\154\154\157\x2f\x7b\x6e\x61\x6d\145\x7d", function (ServerRequestInterface $request, ResponseInterface $response, $name) { $response->getBody()->write("\110\145\x6c\x6c\157\x20{$name}"); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f\x48\145\154\154\x6f\x2f\127\x6f\x72\154\x64"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\164\162\151\156\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("\110\x65\x6c\154\157\40\x57\157\162\x6c\144", (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseNamedArgsStrategy() : void { if (PHP_VERSION_ID < 80000) { $this->markTestSkipped("\116\141\155\145\144\x20\141\162\x67\x75\155\145\x6e\164\163\x20\141\x72\145\x20\156\157\164\40\x73\x75\x70\160\157\162\x74\145\144\40\x69\156\x20\120\110\120\40\166\145\x72\163\151\x6f\x6e\163\x20\160\162\151\x6f\x72\40\x74\x6f\40\x38\x2e\x30"); } $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\x73\164\x72\151\156\x67"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->getRouteCollector()->setDefaultInvocationStrategy(new RequestResponseNamedArgs()); $app->get("\57\173\x67\x72\145\145\x74\151\156\147\175\57\173\x6e\x61\x6d\x65\x7d", function (ServerRequestInterface $request, ResponseInterface $response, $name, $greeting) { $response->getBody()->write("{$greeting}\x20{$name}"); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\57\110\x65\x6c\154\x6f\57\x57\x6f\x72\x6c\144"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\164\x72\x69\x6e\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("\110\x65\154\154\x6f\40\x57\157\x72\154\x64", (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithNamedParameterOverwritesSetArgument() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\x73\164\x72\151\x6e\x67"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("\x2f\x48\145\154\x6c\x6f\x2f\173\x6e\x61\x6d\x65\175", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write("\x48\x65\154\154\157\x20{$args["\x6e\141\155\145"]}"); return $response; })->setArgument("\x6e\141\x6d\145", "\x57\157\x72\154\144\41"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\57\110\145\x6c\154\x6f\x2f\127\157\x72\x6c\x64"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\164\162\151\156\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("\110\145\x6c\154\157\x20\127\x6f\162\154\x64", (string) $response->getBody()); } public function testInvokeWithoutMatchingRoute() : void { $this->expectException(HttpNotFoundException::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\164\162\151\156\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); } public function testInvokeWithCallableRegisteredInContainer() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("\x48\145\154\x6c\x6f\x20\127\x6f\x72\154\144"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $handler = new class { public function foo(ServerRequestInterface $request, ResponseInterface $response) { return $response; } }; $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has("\150\x61\x6e\144\154\145\x72")->willReturn(true); $containerProphecy->get("\x68\x61\x6e\144\x6c\x65\x72")->willReturn($handler); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $app->get("\57", "\150\141\156\144\x6c\145\x72\x3a\146\157\x6f"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\57"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\x74\x72\x69\156\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("\x48\x65\x6c\154\157\x20\127\x6f\162\154\x64", (string) $response->getBody()); } public function testInvokeWithNonExistentMethodOnCallableRegisteredInContainer() : void { $this->expectException(RuntimeException::class); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $handler = new class { }; $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has("\150\x61\156\144\x6c\145\x72")->willReturn(true); $containerProphecy->get("\x68\x61\x6e\144\154\x65\x72")->willReturn($handler); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $app->get("\x2f", "\150\141\x6e\x64\154\145\x72\x3a\x6d\x65\x74\x68\x6f\x64\x5f\x64\157\145\x73\137\x6e\157\x74\137\145\x78\x69\163\164"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\164\162\151\x6e\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); } public function testInvokeWithCallableInContainerViaCallMagicMethod() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\x73\x74\162\151\x6e\x67"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $mockAction = new MockAction(); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has("\x68\x61\156\x64\x6c\x65\162")->willReturn(true); $containerProphecy->get("\x68\141\156\x64\x6c\x65\x72")->willReturn($mockAction); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $app->get("\x2f", "\150\x61\x6e\x64\154\145\x72\x3a\x66\x6f\x6f"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\57"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\105\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\164\x72\x69\156\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $expectedPayload = json_encode(array("\x6e\x61\x6d\145" => "\146\x6f\157", "\141\162\147\165\155\145\156\x74\163" => array())); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame($expectedPayload, (string) $response->getBody()); } public function testInvokeFunctionName() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\x73\x74\x72\151\x6e\x67"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); function handle($request, ResponseInterface $response) { $response->getBody()->write("\x48\145\x6c\154\x6f\x20\x57\157\162\154\x64"); return $response; } $app = new App($responseFactoryProphecy->reveal()); $app->get("\x2f", __NAMESPACE__ . "\x5c\150\x61\156\x64\154\x65"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\x74\x72\x69\x6e\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("\x48\x65\154\x6c\x6f\x20\x57\157\162\x6c\144", (string) $response->getBody()); } public function testCurrentRequestAttributesAreNotLostWhenAddingRouteArguments() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\x73\x74\x72\151\x6e\x67"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("\x2f\x48\x65\154\x6c\157\x2f\x7b\x6e\141\x6d\x65\175", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write($request->getAttribute("\147\162\x65\x65\x74\x69\156\147") . "\x20" . $args["\x6e\141\155\x65"]); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f\x48\145\x6c\154\157\57\127\x6f\x72\154\144"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\105\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\x74\162\x69\156\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()->withAttribute("\x67\162\145\145\164\x69\156\x67", "\110\145\154\154\157")); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("\x48\x65\x6c\x6c\x6f\40\x57\157\162\154\144", (string) $response->getBody()); } public function testCurrentRequestAttributesAreNotLostWhenAddingRouteArgumentsRequestResponseArg() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\x73\x74\162\151\156\x67"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->getRouteCollector()->setDefaultInvocationStrategy(new RequestResponseArgs()); $app->get("\x2f\x48\145\x6c\x6c\x6f\57\x7b\156\x61\155\145\x7d", function (ServerRequestInterface $request, ResponseInterface $response, $name) { $response->getBody()->write($request->getAttribute("\147\x72\x65\145\x74\x69\x6e\147") . "\40" . $name); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\57\x48\x65\154\154\157\57\127\157\162\x6c\x64"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\164\x72\x69\156\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()->withAttribute("\x67\162\x65\145\164\x69\156\147", "\x48\x65\x6c\154\157")); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame("\110\x65\154\154\x6f\x20\127\x6f\x72\x6c\144", (string) $response->getBody()); } public function testRun() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\x73\164\x72\x69\156\147"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $streamProphecy->read(1)->willReturn("\x5f"); $streamProphecy->read("\x31\61")->will(function () { $this->eof()->willReturn(true); return $this->reveal()->__toString(); }); $streamProphecy->eof()->willReturn(false); $streamProphecy->isSeekable()->willReturn(true); $streamProphecy->rewind()->willReturn(true); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseProphecy->getStatusCode()->willReturn(200); $responseProphecy->getHeaders()->willReturn(array("\103\157\156\x74\145\x6e\164\55\x4c\145\x6e\x67\164\x68" => array("\61\61"))); $responseProphecy->getProtocolVersion()->willReturn("\61\x2e\x31"); $responseProphecy->getReasonPhrase()->willReturn(''); $responseProphecy->getHeaderLine("\103\x6f\156\164\145\x6e\x74\x2d\x4c\145\156\x67\x74\x68")->willReturn("\61\61"); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("\57", function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write("\x48\x65\154\154\157\40\127\157\162\x6c\144"); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\x74\x72\151\156\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->run($requestProphecy->reveal()); $this->expectOutputString("\x48\x65\154\x6c\x6f\x20\x57\157\x72\x6c\x64"); } public function testRunWithoutPassingInServerRequest() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\x73\164\162\151\x6e\x67"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $streamProphecy->read(1)->willReturn("\x5f"); $streamProphecy->read("\x31\61")->will(function () { $this->eof()->willReturn(true); return $this->reveal()->__toString(); }); $streamProphecy->eof()->willReturn(false); $streamProphecy->isSeekable()->willReturn(true); $streamProphecy->rewind()->willReturn(true); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseProphecy->getStatusCode()->willReturn(200); $responseProphecy->getHeaders()->willReturn(array("\x43\x6f\156\x74\x65\x6e\164\55\x4c\x65\x6e\147\164\x68" => array("\x31\x31"))); $responseProphecy->getProtocolVersion()->willReturn("\61\56\61"); $responseProphecy->getReasonPhrase()->willReturn(''); $responseProphecy->getHeaderLine("\x43\157\x6e\x74\x65\156\164\x2d\x4c\x65\x6e\147\164\x68")->willReturn("\61\x31"); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("\x2f", function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write("\x48\x65\x6c\x6c\157\x20\x57\x6f\x72\154\144"); return $response; }); $app->run(); $this->expectOutputString("\110\145\x6c\x6c\157\x20\x57\x6f\162\x6c\x64"); } public function testHandleReturnsEmptyResponseBodyWithHeadRequestMethod() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\163\x74\162\x69\156\x67"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseProphecy->withBody(Argument::type(StreamInterface::class))->will(function ($args) { $this->getBody()->willReturn($args[0]); return $this; }); $emptyStreamProphecy = $this->prophesize(StreamInterface::class); $emptyStreamProphecy->__toString()->willReturn(''); $emptyResponseProphecy = $this->prophesize(ResponseInterface::class); $emptyResponseProphecy->getBody()->willReturn($emptyStreamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal(), $emptyResponseProphecy->reveal()); $called = 0; $app = new App($responseFactoryProphecy->reveal()); $app->get("\57", function (ServerRequestInterface $request, ResponseInterface $response) use(&$called) { $called++; $response->getBody()->write("\110\x65\154\x6c\x6f\x20\x57\157\x72\154\x64"); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\x48\105\101\x44"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\x74\162\x69\156\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame(1, $called); $this->assertEmpty((string) $response->getBody()); } public function testCanBeReExecutedRecursivelyDuringDispatch() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\163\164\162\x69\156\x67"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseHeaders = array(); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseProphecy->getStatusCode()->willReturn(200); $responseProphecy->getHeader(Argument::type("\163\164\x72\x69\156\147"))->will(function ($args) use(&$responseHeaders) { return $responseHeaders[$args[0]]; }); $responseProphecy->withAddedHeader(Argument::type("\x73\x74\162\x69\x6e\x67"), Argument::type("\x73\x74\162\x69\x6e\x67"))->will(function ($args) use(&$responseHeaders) { $key = $args[0]; $value = $args[1]; if (!isset($responseHeaders[$key])) { $responseHeaders[$key] = array(); } $responseHeaders[$key][] = $value; return $this; }); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse(Argument::type("\x69\x6e\164\x65\147\145\x72"))->will(function ($args) use($responseProphecy) { $responseProphecy->getStatusCode()->willReturn($args[0]); return $responseProphecy->reveal(); }); $app = new App($responseFactoryProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) use($app, $responseFactoryProphecy) { $request = $args[0]; if ($request->hasHeader("\x58\x2d\116\x45\x53\124\105\x44")) { return $responseFactoryProphecy->reveal()->createResponse(204)->withAddedHeader("\x58\x2d\124\122\101\103\x45", "\x6e\145\x73\164\x65\144"); } $response = $app->handle($request->withAddedHeader("\x58\55\116\105\x53\x54\x45\x44", "\x31")); return $response->withAddedHeader("\x58\x2d\124\x52\x41\103\105", "\x6f\x75\x74\x65\162"); }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process(Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class))->will(function ($args) { $request = $args[0]; $handler = $args[1]; $response = $handler->handle($request); $response->getBody()->write("\61"); return $response; }); $app->add($middlewareProphecy->reveal())->add($middlewareProphecy2->reveal()); $app->get("\x2f", function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\57"); $responseHeaders = array(); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\x47\x45\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->hasHeader(Argument::type("\163\x74\162\151\x6e\x67"))->will(function ($args) use(&$responseHeaders) { return array_key_exists($args[0], $responseHeaders); }); $requestProphecy->withAddedHeader(Argument::type("\x73\x74\162\151\x6e\147"), Argument::type("\163\x74\162\x69\156\147"))->will(function ($args) use(&$responseHeaders) { $key = $args[0]; $value = $args[1]; if (!isset($responseHeaders[$key])) { $responseHeaders[$key] = array(); } $responseHeaders[$key][] = $value; return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame(204, $response->getStatusCode()); $this->assertSame(array("\x6e\x65\x73\164\145\x64", "\x6f\165\164\145\x72"), $response->getHeader("\130\x2d\x54\122\x41\x43\x45")); $this->assertSame("\x31\61", (string) $response->getBody()); } public function testContainerSetToRoute() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn("\x48\x65\154\x6c\x6f\40\x57\157\162\154\x64"); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has("\150\x61\x6e\x64\154\145\x72")->willReturn(true); $containerProphecy->get("\150\141\156\144\154\x65\162")->willReturn(function () use($responseProphecy) { return $responseProphecy->reveal(); }); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $routeCollector = $app->getRouteCollector(); $routeCollector->map(array("\107\x45\x54"), "\57", "\x68\141\x6e\144\x6c\x65\162"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\57"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\x47\105\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\x73\164\x72\151\x6e\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("\110\145\x6c\154\x6f\x20\x57\157\x72\x6c\x64", (string) $response->getBody()); } public function testAppIsARequestHandler() : void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $this->assertInstanceOf(RequestHandlerInterface::class, $app); } public function testInvokeSequentialProcessToAPathWithOptionalArgsAndWithoutOptionalArgs() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\163\x74\162\x69\156\x67"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("\57\110\145\x6c\154\157\133\57\173\156\x61\155\145\x7d\135", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write((string) count($args)); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f\x48\x65\x6c\x6c\157\x2f\127\x6f\162\x6c\144"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\105\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\164\x72\x69\156\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("\x31", (string) $response->getBody()); $uriProphecy2 = $this->prophesize(UriInterface::class); $uriProphecy2->getPath()->willReturn("\x2f\110\x65\154\x6c\x6f"); $requestProphecy2 = $this->prophesize(ServerRequestInterface::class); $requestProphecy2->getMethod()->willReturn("\107\105\x54"); $requestProphecy2->getUri()->willReturn($uriProphecy2->reveal()); $requestProphecy2->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy2->withAttribute(Argument::type("\x73\x74\162\151\x6e\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $streamProphecy->__toString()->willReturn(''); $response = $app->handle($requestProphecy2->reveal()); $this->assertSame("\60", (string) $response->getBody()); } public function testInvokeSequentialProcessToAPathWithOptionalArgsAndWithoutOptionalArgsAndKeepSetedArgs() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\163\164\162\x69\x6e\147"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get("\57\x48\x65\154\154\x6f\133\x2f\x7b\156\141\x6d\x65\x7d\x5d", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write((string) count($args)); return $response; })->setArgument("\x65\170\164\x72\x61", "\x76\141\154\x75\x65"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\57\x48\x65\154\154\x6f\57\x57\x6f\x72\x6c\144"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\x47\x45\x54"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\x74\162\151\156\147"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("\62", (string) $response->getBody()); $uriProphecy2 = $this->prophesize(UriInterface::class); $uriProphecy2->getPath()->willReturn("\x2f\x48\145\x6c\x6c\x6f"); $requestProphecy2 = $this->prophesize(ServerRequestInterface::class); $requestProphecy2->getMethod()->willReturn("\x47\x45\x54"); $requestProphecy2->getUri()->willReturn($uriProphecy2->reveal()); $requestProphecy2->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy2->withAttribute(Argument::type("\163\x74\x72\x69\x6e\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $streamProphecy->__toString()->willReturn(''); $response = $app->handle($requestProphecy2->reveal()); $this->assertSame("\61", (string) $response->getBody()); } public function testInvokeSequentialProcessAfterAddingAnotherRouteArgument() : void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type("\163\164\162\x69\156\147"))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $route = $app->get("\57\x48\x65\x6c\154\157\133\x2f\173\x6e\x61\155\145\x7d\135", function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write((string) count($args)); return $response; })->setArgument("\145\170\164\162\x61", "\166\x61\154\165\145"); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn("\x2f\x48\x65\154\154\157\57\x57\x6f\x72\154\x64"); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn("\107\x45\124"); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTE)->willReturn($route); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type("\163\164\x72\x69\x6e\x67"), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("\x32", (string) $response->getBody()); $route->setArgument("\145\170\164\162\141\x32", "\166\x61\154\x75\145\x32"); $streamProphecy->__toString()->willReturn(''); $response = $app->handle($requestProphecy->reveal()); $this->assertSame("\63", (string) $response->getBody()); } }

Function Calls

None

Variables

None

Stats

MD5 d21f93e3e05e08e97898974009608e6f
Eval Count 0
Decode Time 148 ms