public function testBaseUri() { $app = new App(['router' => ['baseUri' => '/sub-path', 'routes' => ['/.*' => function ($req, ResponseInterface $res, callable $next) { return $next($req, $res->withHeader('X-Global', 'global as f**k')); }, '/:controller?/:action?/:id?.:format?' => Dispatcher::class]]]); $app->append(Router::class); $response = $app->run(new ServerRequest((new Uri())->withPath('/sub-path/some-controller'))); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertTrue($response->hasHeader('x-controller'), 'x-controller /some-controller'); $this->assertEquals('some-controller', $response->getHeaderLine('x-controller')); $this->assertTrue($response->hasHeader('x-action'), 'x-action /some-controller'); $this->assertEquals('index', $response->getHeaderLine('x-action')); $this->assertTrue($response->hasHeader('x-id'), 'x-id /some-controller'); $this->assertEquals('', $response->getHeaderLine('x-id')); $this->assertTrue($response->hasHeader('x-format'), 'x-format /some-controller'); $this->assertEquals('html', $response->getHeaderLine('x-format')); $this->assertTrue($response->hasHeader('x-global'), 'x-global /some-controller'); $this->assertEquals('global as f**k', $response->getHeaderLine('x-global')); $response = $app->run(new ServerRequest((new Uri())->withPath('/sub-path/user/details/1.json'))); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertTrue($response->hasHeader('x-controller'), 'x-controller /user/details...'); $this->assertEquals('user', $response->getHeaderLine('x-controller')); $this->assertTrue($response->hasHeader('x-action'), 'x-action /user/details...'); $this->assertEquals('details', $response->getHeaderLine('x-action')); $this->assertTrue($response->hasHeader('x-id'), 'x-id /user/details...'); $this->assertEquals('1', $response->getHeaderLine('x-id')); $this->assertTrue($response->hasHeader('x-format'), 'x-format /user/details...'); $this->assertEquals('json', $response->getHeaderLine('x-format')); $this->assertTrue($response->hasHeader('x-global'), 'x-global /user/details...'); $this->assertEquals('global as f**k', $response->getHeaderLine('x-global')); }
public function testMiddleware() { $app = new App(['path' => __DIR__]); $app->append(function ($request, ResponseInterface $response, $next) { return $next($request, $response->withHeader('test', 'test value')); }); $response = $app->run(); $this->assertEquals(301, $response->getStatusCode()); $this->assertEquals('It works!', $response->getReasonPhrase()); $this->assertEquals('{Data from Database}, {Data from Model}', (string) $response->getBody()); $this->assertEquals('test value', $response->getHeaderLine('test')); }
public function testDispatcher() { $app = new App(['controller' => ['nameSpace' => 'My\\App\\Controller', 'modules' => ['adm' => 'Admin'], 'loader' => ['enabled' => true, 'path' => __DIR__ . '/test-app/controllers']]]); $app->append(Dispatcher::class); $response = $app->run(new ServerRequest()); $this->assertEquals(100, $response->getStatusCode()); $response = $app->run((new ServerRequest())->withAttribute('action', 'two')); $this->assertEquals(101, $response->getStatusCode()); $response = $app->run((new ServerRequest())->withAttribute('action', 'three')); $this->assertEquals(102, $response->getStatusCode()); $response = $app->run((new ServerRequest())->withAttribute('controller', 'other')); $this->assertEquals(103, $response->getStatusCode()); $response = $app->run((new ServerRequest())->withAttribute('controller', 'other')->withAttribute('action', 'five')); $this->assertEquals(104, $response->getStatusCode()); $response = $app->run((new ServerRequest())->withAttribute('controller', 'other')->withAttribute('action', 'six')); $this->assertEquals(105, $response->getStatusCode()); $response = $app->run((new ServerRequest())->withAttribute('module', 'adm')->withAttribute('controller', 'other')); $this->assertEquals(100, $response->getStatusCode()); $response = $app->run((new ServerRequest())->withAttribute('module', 'adm')->withAttribute('controller', 'other')->withAttribute('action', 'five')); $this->assertEquals(101, $response->getStatusCode()); $response = $app->run((new ServerRequest())->withAttribute('module', 'adm')->withAttribute('controller', 'other')->withAttribute('action', 'six')); $this->assertEquals(102, $response->getStatusCode()); }