/** * Get a list of arguments resolved against the container * * @param $responder * @return array */ protected function getResolvedArguments($responder) { $reflection = new \ReflectionFunction($responder); $parameters = $reflection->getParameters(); $args = []; foreach ($parameters as $parameter) { $type = $parameter->getClass()->getName(); $args[] = $this->container->make($type); } return $args; }
$injected = null; $route = new Route('slug', function (DependencyInterface $rad) use(&$injected) { $injected = $rad; }); $route->bind($this->container); $route->resolve(); expect($injected)->to->be->instanceof('Rad\\DependencyImpl'); }); it('should be able to resolve dependencies of a class responder', function () { $route = new Route('slug', 'Rad\\Responder'); $route->bind($this->container); $dep = $route->resolve(); expect($dep)->to->be->an->instanceof('Rad\\DependencyImpl'); }); it('should be able to resolve dependencies registered as a factory', function () { $container = new Container(); $container->bind('Rad\\DependencyInterface', function () { return new \Rad\DependencyImpl(); }); $route = new Route('slug', 'Rad\\Responder'); $route->bind($container); $dep = $route->resolve(); expect($dep)->to->be->an->instanceof('Rad\\DependencyImpl'); }); it('should be able to resolve a route if container not bound', function () { $executed = false; $route = new Route('slug', function (DependencyInterface $ignoreMe) use(&$executed) { $executed = true; }); $route->resolve(); expect($executed)->to->be->true;
$this->router->get('/custom-endpoint', function () use(&$executed) { $executed = true; }); $this->router->dispatch('GET', ['blah' => '/custom-endpoint']); expect($executed)->to->be->false; }); it('should use the $_REQUEST super global to match when dispatching a route', function () { $_REQUEST = ['scoped_action' => '/custom-endpoint']; $this->router->get('/custom-endpoint', function () use(&$executed) { $executed = true; }); $this->router->dispatch('GET'); expect($executed)->to->be->true; }); it('should forward a bound container to its routes', function () { $container = new Container(); $container->bind('Rad\\DependencyInterface', 'Rad\\DependencyImpl'); $this->router->bind($container); $dep = null; $this->router->get('/custom-endpoint', function (DependencyInterface $rad) use(&$dep) { $dep = $rad; }); $this->router->dispatch('GET', ['scoped_action' => '/custom-endpoint']); expect($dep)->to->be->instanceof('Rad\\DependencyImpl'); }); it('should use $_SERVER[REQUEST_METHOD] to determine parameters if no arguments provided', function () { $_REQUEST = ['scoped_action' => '/custom-endpoint']; $_SERVER = ['REQUEST_METHOD' => 'GET']; $this->router->get('/custom-endpoint', function () use(&$executed) { $executed = true; });