public function setup() { parent::setup(); $config = (include 'config/application.config.php'); $config['module_listener_options']['config_static_paths'] = array(getcwd() . '/config/test.config.php'); if (file_exists(__DIR__ . '/config/test.config.php')) { $moduleConfig = (include __DIR__ . '/config/test.config.php'); array_unshift($config['module_listener_options']['config_static_paths'], $moduleConfig); } $this->serviceManager = new ServiceManager(new ServiceManagerConfig(isset($config['service_manager']) ? $config['service_manager'] : array())); $this->serviceManager->setService('ApplicationConfig', $config); $this->serviceManager->setFactory('ServiceListener', 'Zend\\Mvc\\Service\\ServiceListenerFactory'); $moduleManager = $this->serviceManager->get('ModuleManager'); $moduleManager->loadModules(); $this->routes = array(); foreach ($moduleManager->getModules() as $m) { $moduleConfig = (include __DIR__ . '/../../../../' . ucfirst($m) . '/config/module.config.php'); if (isset($moduleConfig['router'])) { foreach ($moduleConfig['router']['routes'] as $key => $name) { $this->routes[$key] = $name; } } } $this->serviceManager->setAllowOverride(true); $this->application = $this->serviceManager->get('Application'); $this->event = new MvcEvent(); $this->event->setTarget($this->application); $this->event->setApplication($this->application)->setRequest($this->application->getRequest())->setResponse($this->application->getResponse())->setRouter($this->serviceManager->get('Router')); $this->createDatabase(); }
/** * @return Response */ public function __invoke() { $routeMatch = $this->application->getMvcEvent()->getRouteMatch(); $redirect = $this->getRedirect($routeMatch->getMatchedRouteName(), $this->getRedirectRouteFromRequest()); $response = $this->application->getResponse(); $response->getHeaders()->addHeaderLine('Location', $redirect); $response->setStatusCode(302); return $response; }
public function testResponseMayBeInjected() { $app = new Application(); $response = new Response(); $app->setResponse($response); $this->assertSame($response, $app->getResponse()); }
/** * @dataProvider eventPropagation */ public function testEventPropagationStatusIsClearedBetweenEventsDuringRun($events) { $event = new MvcEvent(); $event->setTarget($this->application); $event->setApplication($this->application)->setRequest($this->application->getRequest())->setResponse($this->application->getResponse())->setRouter($this->serviceManager->get('Router')); $event->stopPropagation(true); // Intentionally not calling bootstrap; setting mvc event $r = new ReflectionObject($this->application); $eventProp = $r->getProperty('event'); $eventProp->setAccessible(true); $eventProp->setValue($this->application, $event); // Setup listeners that stop propagation, but do nothing else $marker = array(); foreach ($events as $event) { $marker[$event] = true; } $marker = (object) $marker; $listener = function ($e) use($marker) { $marker->{$e->getName()} = $e->propagationIsStopped(); $e->stopPropagation(true); }; $this->application->getEventManager()->attach($events, $listener); $this->application->run(); foreach ($events as $event) { $this->assertFalse($marker->{$event}, sprintf('Assertion failed for event "%s"', $event)); } }
/** * Faz o setup dos testes. Executado antes de cada teste * @return void */ public function setup() { $env = getenv('ENV'); //o jenkins tem configurações especiais if (!$env || $env != 'jenkins') { putenv("ENV=testing"); $env = 'testing'; } putenv('PROJECT_ROOT=' . __DIR__ . '/../../../../../'); parent::setup(); //arquivo de configuração da aplicação $config = (include __DIR__ . '/../../../../../config/tests.config.php'); $config['module_listener_options']['config_static_paths'] = array(); //cria um novo ServiceManager $this->serviceManager = new ServiceManager(new ServiceManagerConfig(isset($config['service_manager']) ? $config['service_manager'] : array())); //configura os serviços básicos no ServiceManager $this->serviceManager->setService('ApplicationConfig', $config); $this->serviceManager->setFactory('ServiceListener', 'Zend\\Mvc\\Service\\ServiceListenerFactory'); //verifica se os módulos possuem rotas configuradas e carrega elas para serem usadas pelo ControllerTestCase $moduleManager = $this->serviceManager->get('ModuleManager'); $moduleManager->loadModules(); $this->routes = array(); $testConfig = false; //carrega as rotas dos módulos foreach ($moduleManager->getLoadedModules() as $m) { $moduleConfig = $m->getConfig(); $this->getModuleRoutes($moduleConfig); $moduleName = explode('\\', get_class($m)); $moduleName = $moduleName[0]; //verifica se existe um arquivo de configuração específico no módulo para testes if (file_exists(getcwd() . '/module/' . ucfirst($moduleName) . '/config/test.config.php')) { $testConfig = (include getcwd() . '/module/' . ucfirst($moduleName) . '/config/test.config.php'); array_unshift($config['module_listener_options']['config_static_paths'], $testConfig[$env]); } } if (!$testConfig) { $config['module_listener_options']['config_static_paths'] = array(getcwd() . '/config/tests.config.php'); } $this->config = (include $config['module_listener_options']['config_static_paths'][0]); $this->serviceManager->setAllowOverride(true); //instancia a aplicação e configura os eventos e rotas $this->application = $this->serviceManager->get('Application'); $this->event = new MvcEvent(); $this->event->setTarget($this->application); $this->event->setApplication($this->application)->setRequest($this->application->getRequest())->setResponse($this->application->getResponse())->setRouter($this->serviceManager->get('Router')); $this->entityManager = $this->getEntityManager(); $this->dropDatabase(); $this->createDatabase(); }
/** * @group 2981 */ public function testReturnsResponseFromListenerWhenDispatchEventShortCircuits() { $this->application->bootstrap(); $testResponse = new Response(); $response = $this->application->getResponse(); $events = $this->application->getEventManager(); $events->clearListeners(MvcEvent::EVENT_ROUTE); $events->attach(MvcEvent::EVENT_DISPATCH, function ($e) use($testResponse) { $testResponse->setContent('triggered'); return $testResponse; }, 100); $self = $this; $triggered = false; $events->attach(MvcEvent::EVENT_FINISH, function ($e) use($self, $testResponse, &$triggered) { $self->assertSame($testResponse, $e->getResponse()); $triggered = true; }); $this->application->run(); $this->assertTrue($triggered); }
/** * @group ZF2-171 */ public function testFinishShouldRunEvenIfDispatchEventReturnsResponse() { $app = new Application(); $response = $app->getResponse(); $events = $app->events(); $events->clearListeners('route'); $events->attach('dispatch', function ($e) use($response) { return $response; }, 100); $token = new stdClass(); $events->attach('finish', function ($e) use($token) { $token->foo = 'bar'; }); $app->run(); $this->assertTrue(isset($token->foo)); $this->assertEquals('bar', $token->foo); }
/** * Gets the response from the service manager and convert it to Symfony. * * @return \Symfony\Component\HttpFoundation\Response */ protected function getSymfonyResponse() { $zendResponse = $this->application->getResponse(); return self::createSymfonyResponse($zendResponse); }