/** * When the found controller method wants to have the request injected, * this method will do it. * * @param ControllerWillRespond $event Event triggered before execution of controller. */ public function injectRequest(ControllerWillRespond $event) { $route = $this->router->getRoute($event->getControllerName()); // find the method's meta data $methods = $route->getMethods(); $i = ArrayUtils::search($methods, 'method', $event->getMethod()); if ($i === false) { return; } $method = $methods[$i]; $arguments = $event->getArguments(); foreach ($method['params'] as $i => $param) { if ($param['class'] && Debugger::isExtending($param['class'], Request::class, true) && !$arguments[$i] instanceof Request) { $arguments[$i] = $this->request; } } $event->setArguments($arguments); }
/** * @covers \Splot\Framework\Events\ControllerWillRespond::__construct * @covers \Splot\Framework\Events\ControllerWillRespond::getControllerName * @covers \Splot\Framework\Events\ControllerWillRespond::getController * @covers \Splot\Framework\Events\ControllerWillRespond::getMethod * @covers \Splot\Framework\Events\ControllerWillRespond::getArguments * @covers \Splot\Framework\Events\ControllerWillRespond::setArguments * @covers \Splot\Framework\Events\ControllerWillRespond::setMethod */ public function testControllerWillRespond() { $controller = new TestController(new Container()); $arguments = array('id' => 123); $event = new Events\ControllerWillRespond('SplotTestModule:TestController', $controller, 'index', $arguments); $this->assertEquals('SplotTestModule:TestController', $event->getControllerName()); $this->assertSame($controller, $event->getController()); $this->assertEquals('index', $event->getMethod()); $this->assertEquals($arguments, $event->getArguments()); $event->setMethod('customMethod'); $this->assertEquals('customMethod', $event->getMethod()); $newArguments = array('id' => 15); $event->setArguments($newArguments); $this->assertEquals($newArguments, $event->getArguments()); }
/** * @covers ::render * @covers ::renderController */ public function testRenderingControllers() { $routesModule = new SplotRoutesTestModule(); $this->application->addTestModule($routesModule); $controllerWillRespondCalled = false; $controllerDidRespondCalled = false; $this->application->getContainer()->get('event_manager')->subscribe(ControllerWillRespond::getName(), function () use(&$controllerWillRespondCalled) { $controllerWillRespondCalled = true; }); $this->application->getContainer()->get('event_manager')->subscribe(ControllerDidRespond::getName(), function () use(&$controllerDidRespondCalled) { $controllerDidRespondCalled = true; }); $response = $this->application->render('SplotRoutesTestModule:Item', array('id' => 123)); $this->assertTrue($response instanceof Response); $this->assertEquals('Received Item ID: 123', $response->getContent()); $this->assertTrue($controllerWillRespondCalled); $this->assertTrue($controllerDidRespondCalled); }