Exemple #1
0
 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'));
 }
Exemple #2
0
 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'));
 }
Exemple #3
0
 /**
  * @param ServerRequestInterface $request
  *
  * @return $this
  */
 public function route(ServerRequestInterface $request)
 {
     /** @var Route[] $routes */
     $routes = array_reverse($this->routes);
     foreach ($routes as $route) {
         $path = $request->getUri()->getPath();
         $baseUri = $this->getOption('baseUri', '');
         $len = strlen($baseUri);
         if ($len > 0) {
             if (strncmp($baseUri, $path, $len) !== 0) {
                 continue;
             }
             $path = substr($path, $len);
         }
         if (empty($path)) {
             $path = '/';
         }
         $vars = null;
         if (!in_array($request->getMethod(), $route->getMethods()) || ($vars = $route->match($path)) === false) {
             continue;
         }
         $subRequest = $request;
         foreach ($vars as $key => $value) {
             $subRequest = $subRequest->withAttribute($key, $value);
         }
         $this->app->prepend(function ($request, $response, $next) use($route, $subRequest) {
             $handler = $this->app->prepareMiddleware($route->getHandler());
             return $handler($subRequest, $response, $next);
         });
     }
     return $this;
 }
 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());
 }
Exemple #5
0
<?php

use Tale\App;
include 'vendor/autoload.php';
//The first parameter to App can actually be omitted completely,
//if the app-directory is ./app from here!
//Even though, all sub paths (meaning, ALL sub-paths) will
//be relative then.
$app = new App(['path' => __DIR__ . '/app']);
$app->run();