/** * @test */ public function requestContainsRoutingResultInRoutingResultAttributeAfterRouting() { $self = $this; $route = RouteBuilder::route()->get('/test')->to('testAction')->build(); $routingResult = RoutingResult::forSuccess($route); $this->router->expects($this->any())->method('match')->will($this->returnValue($routingResult)); $next = function ($request, $response) use($routingResult, $self) { $self->assertSame($routingResult, $request->getAttribute(RoutingResult::class)); return $response; }; $this->middleware->__invoke($this->request, $this->response, $next); }
/** * Creates a route using given params * * @param array $methods * @param string $name * @param string $path * @param mixed $target * @param array $matchers * @return Route */ protected function createRoute(array $methods, string $name, string $path, $target, array $matchers = []) : Route { $builder = RouteBuilder::route($this->defaultRouteClass); $builder->from($path)->to($target); foreach ($methods as $method) { $builder->accepting($method); } $builder->named($name); foreach ($matchers as $param => $paramMatchers) { foreach ($paramMatchers as $matcher) { $builder->ifMatches($param, $matcher); } } return $builder->build(); }
/** * @test * @expectedException \InvalidArgumentException */ public function willThrowAnExceptionWhenProvidingNotMatchingParams() { $paramValue = 'abc'; $matcher = $this->createMock(Matcher::class); $matcher->expects($this->once())->method('__invoke')->with($paramValue)->will($this->returnValue(false)); $this->router->addRoute(RouteBuilder::route()->get('/company/[:companyId]')->to('companyAction')->ifMatches('companyId', $matcher)->build()); $this->router->generateUri('companyAction', ['companyId' => $paramValue]); }
/** * @test */ public function outGetsCalledIfGivenAndErrorHandlerIsSetAndNoErrorOccurs() { $expected = ['outCalled']; $called = []; $resolver = $this->createMock(ActionResolver::class); $resolver->expects($this->once())->method('resolve')->will($this->returnValue(function (ServerRequestInterface $request, ResponseInterface $response) { return $response; })); $app = new Adrenaline([$resolver], [], null, $this->emitter); $app->addRoute(RouteBuilder::route()->get('/')->to(function (ServerRequestInterface $request, ResponseInterface $response) { return $response; })->named('home')->build()); $app->setErrorHandler(function (ServerRequestInterface $request, ResponseInterface $response, $err) use(&$called) { $called[] = 'errCalled'; return $response; }); $app($this->request, $this->response, function (ServerRequestInterface $request, ResponseInterface $response) use(&$called) { $called[] = 'outCalled'; return $response; }); $this->assertSame($expected, $called); }
/** * @Bean * @Parameters({ * @Parameter({"name" = "app.datadir"}) * }) * @return \bitExpert\Pathfinder\Psr7Router * @throws \InvalidArgumentException * @throws \RuntimeException */ protected function router($datadir = '') { $defaultRoute = RouteBuilder::route()->get('/')->to('handleDefaultRouteAction')->build(); $pageRoute = RouteBuilder::route()->get('/[:page]')->to('handlePageAction')->ifMatches('page', new PageExistsMatcher($datadir))->build(); return new Psr7Router([$defaultRoute, $pageRoute]); }
/** * Asserts that the static route creation function for given method works * * @param $method * @param string|null $path * @param string|null $target * @param array $matchers */ protected function assertRouteCreationShortcutFunction($method, $path = null, $target = null, $matchers = []) { /** @var Route $route */ $builder = RouteBuilder::route(); $builder = call_user_func([$builder, $method], $path); $builder->to($target); $route = $builder->build(); $this->assertInstanceOf(Route::class, $route); $this->assertTrue(in_array(strtoupper($method), $route->getMethods())); $this->assertEquals($path, $route->getPath()); $this->assertEquals($target, $route->getTarget()); $this->assertEquals($matchers, $route->getMatchers()); }