예제 #1
0
 public function bootstrap()
 {
     $event = new MvcEvent();
     $event->setApplication($this);
     $event->setTarget($this);
     $this->getEventManager()->trigger('bootstrap', $event);
 }
예제 #2
0
 public function testOnDispatch()
 {
     // Create MvcEvent
     $e = new MvcEvent();
     $e->setViewModel(new ViewModel());
     $rm = new RouteMatch([]);
     $rm->setParam('controller', 'Application\\Controller\\Download');
     $e->setRouteMatch($rm);
     $e->setTarget(new DownloadController([]));
     // Create EntityManager and EntityRepository
     $moduleDetail = new ModuleList();
     $moduleDetail->setModuleDesc('Pretty description');
     $repo = $this->prophesize('Doctrine\\ORM\\EntityRepository');
     $repo->findOneBy(['moduleName' => 'Application'])->willReturn($moduleDetail);
     $em = $this->prophesize('Doctrine\\ORM\\EntityManager');
     $em->getRepository('Application\\Entity\\ModuleList')->willReturn($repo);
     $this->sm->setService('Doctrine\\ORM\\EntityManager', $em->reveal());
     // Create ViewHelperManager
     $headTitle = new HeadTitle();
     $vhm = new HelperPluginManager();
     $vhm->setService('headTitle', $headTitle);
     $this->sm->setService('ViewHelperManager', $vhm);
     $this->module->onDispatch($e);
     $fbMeta = $e->getViewModel()->getVariable('fbMeta');
     $this->assertEquals(sprintf('%s-Real Live Learn ZF2', $moduleDetail->getModuleDesc()), $fbMeta['title']);
     $this->assertEquals(sprintf('%s-', $moduleDetail->getModuleDesc()), $fbMeta['description']);
 }
예제 #3
0
 public function bootstrap()
 {
     $event = new MvcEvent();
     $event->setApplication($this);
     $event->setTarget($this);
     $this->getEventManager()->trigger(MvcEvent::EVENT_BOOTSTRAP, $event);
 }
예제 #4
0
 public function setUpMvcEvent($app, $request, $response)
 {
     $event = new MvcEvent();
     $event->setTarget($app);
     $event->setApplication($app)->setRequest($request)->setResponse($response);
     $r = new ReflectionProperty($app, 'event');
     $r->setAccessible(true);
     $r->setValue($app, $event);
     return $app;
 }
예제 #5
0
 public function testNotRestoreWithoutSessionSteps()
 {
     $listener = new StepCollectionListener();
     $event = new MvcEvent();
     $stepMock = $this->getMock('Wizard\\Step\\StepInterface');
     $stepMock->method('getWizard')->will($this->returnValue($this->getWizard()));
     $stepMock->expects($this->never())->method('setFromArray');
     $event->setTarget($stepMock);
     $listener->restore($event);
 }
예제 #6
0
 public function testRouteListenerModifiesHalPluginToRenderCollectionsIfControllerIsRelevant()
 {
     $this->setupServiceChain();
     $this->hal->setRenderCollections(false);
     $matches = new RouteMatch(['controller' => 'ZF\\Apigility\\Admin\\Foo\\Controller']);
     $event = new MvcEvent();
     $event->setRouteMatch($matches);
     $event->setTarget($this->application);
     $this->assertNull($this->module->onRoute($event));
     $this->assertTrue($this->hal->getRenderCollections());
 }
 public function testInjectTemplate()
 {
     require_once __DIR__ . '/_files/TestController.php';
     $event = new MvcEvent();
     $routeMatch = new RouteMatch(array('action' => 'test'));
     $event->setRouteMatch($routeMatch);
     $model = $this->getMock('Zend\\View\\Model\\ViewModel', array('setTemplate'));
     $model->expects($this->once())->method('setTemplate')->with($this->equalTo('AzmTest/TestSubNamespace/test.phtml'));
     $event->setResult($model);
     $event->setTarget(new TestController());
     $this->listener->injectTemplate($event);
 }
예제 #8
0
 public function testResolve()
 {
     $event = new MvcEvent();
     $controller = new AbcController();
     $event->setTarget($controller);
     $routeMatch = new RouteMatch(['action' => 'def']);
     $event->setRouteMatch($routeMatch);
     list($module, $controller, $action) = $this->testedObject->resolve($event);
     $this->assertSame('applicationcoreacltest\\model\\provider', $module);
     $this->assertSame('abc', $controller);
     $this->assertSame('def', $action);
 }
예제 #9
0
 public function __Construct()
 {
     $env = getenv("DB");
     switch ($env) {
         case $env == "sqlite":
             $dbfilename = "schema.sqlite.sql";
             $configFileName = "sqlite.config.php";
             break;
         case $env == "mysql":
             $dbfilename = "schema.sql";
             $configFileName = "mysql.config.php";
             break;
         case $env == "pgsql":
             $dbfilename = "schema.pgsql.sql";
             $configFileName = "pgsql.config.php";
             break;
     }
     $configuration = (include __DIR__ . '/TestConfiguration.php');
     if (isset($configuration['output_buffering']) && $configuration['output_buffering']) {
         ob_start();
         // required to test sessions
     }
     $this->serviceManager = new ServiceManager(new ServiceManagerConfig(isset($configuration['service_manager']) ? $configuration['service_manager'] : array()));
     $this->serviceManager->setService('ApplicationConfig', $configuration);
     $this->serviceManager->setFactory('ServiceListener', 'Zend\\Mvc\\Service\\ServiceListenerFactory');
     /** @var $moduleManager \Zend\ModuleManager\ModuleManager */
     $moduleManager = $this->serviceManager->get('ModuleManager');
     $moduleManager->loadModules();
     $this->serviceManager->setAllowOverride(true);
     $application = $this->serviceManager->get('Application');
     $event = new MvcEvent();
     $event->setTarget($application);
     $event->setApplication($application)->setRequest($application->getRequest())->setResponse($application->getResponse())->setRouter($this->serviceManager->get('Router'));
     /// lets create user's table
     $em = $this->serviceManager->get("doctrine.entitymanager.orm_default");
     $conn = $em->getConnection();
     if (file_exists(__DIR__ . "/../vendor/zf-commons/zfc-user/data/{$dbfilename}")) {
         $sql = file_get_contents(__DIR__ . "/../vendor/zf-commons/zfc-user/data/{$dbfilename}");
     } elseif (file_exists(__DIR__ . "/../../../../vendor/zf-commons/zfc-user/data/{$dbfilename}")) {
         $sql = file_get_contents(__DIR__ . "/../../../../vendor/zf-commons/zfc-user/data/{$dbfilename}");
     } else {
         throw new \Exception("please check the zfc user sql file", 500);
     }
     $stmt = $conn->prepare($sql);
     try {
         $stmt->execute();
     } catch (Exception $exc) {
     }
 }
 /**
  * Inject a template into the view model, if none present
  *
  * Template is derived from the controller found in the route match, and,
  * optionally, the action, if present.
  *
  * @param  MvcEvent $e
  * @return void
  */
 public function injectTemplate(MvcEvent $e)
 {
     /**
      * This condition fix the template name derived from the controller
      * We have to use the service name insteadof the controller class because
      * proxy manager generate a random name
      */
     $controller = $e->getTarget();
     $routeMatch = $e->getRouteMatch();
     if ($controller instanceof ProxyInterface) {
         $e->setTarget($routeMatch->getParam('controller', ''));
     }
     parent::injectTemplate($e);
     if ($controller instanceof ProxyInterface) {
         $e->setTarget($controller);
     }
 }
예제 #11
0
 /**
  * @depends testModuleDefinesServiceForContentTypeListener
  */
 public function testOnBootstrapMethodRegistersListenersWithEventManager($factory)
 {
     $serviceConfig = $this->module->getServiceConfig();
     $this->services->setFactory('ZF\\Versioning\\ContentTypeListener', $serviceConfig['factories']['ZF\\Versioning\\ContentTypeListener']);
     $this->services->setFactory('ZF\\Versioning\\AcceptListener', $serviceConfig['factories']['ZF\\Versioning\\AcceptListener']);
     $this->services->setInvokableClass('ZF\\Versioning\\VersionListener', 'ZF\\Versioning\\VersionListener');
     $event = new MvcEvent();
     $event->setTarget($this->app);
     $this->module->onBootstrap($event);
     $listeners = $this->events->getListeners(MvcEvent::EVENT_ROUTE);
     $this->assertEquals(3, count($listeners));
     $this->assertTrue($listeners->hasPriority(-40));
     $test = [];
     foreach ($listeners as $listener) {
         $callback = $listener->getCallback();
         $test[] = array_shift($callback);
     }
     $expected = ['ZF\\Versioning\\ContentTypeListener', 'ZF\\Versioning\\AcceptListener', 'ZF\\Versioning\\VersionListener'];
     foreach ($expected as $class) {
         $listener = $this->services->get($class);
         $this->assertContains($listener, $test);
     }
 }
예제 #12
0
 /**
  * Complete the request
  *
  * Triggers "render" and "finish" events, and returns response from
  * event object.
  *
  * @param  MvcEvent $event
  * @return Application
  */
 protected function completeRequest(MvcEvent $event)
 {
     $events = $this->getEventManager();
     $event->setTarget($this);
     $events->trigger(MvcEvent::EVENT_RENDER, $event);
     $events->trigger(MvcEvent::EVENT_FINISH, $event);
     return $this;
 }
예제 #13
0
 /**
  * Complete the request
  *
  * Triggers "render" and "finish" events, and returns response from
  * event object.
  *
  * @param  MvcEvent $event
  * @return Response
  */
 protected function completeRequest(MvcEvent $event)
 {
     $events = $this->events();
     $event->setTarget($this);
     $events->trigger('render', $event);
     $events->trigger('finish', $event);
     return $event->getResponse();
 }
예제 #14
0
 private function prepareEvent($forApi = false)
 {
     if ($forApi) {
         $controllerMock = $this->getMock(AbstractApiActionController::class);
     } else {
         $controllerMock = $this->getMock(AbstractActionController::class);
     }
     $routerFactory = new RouterFactory();
     $router = $routerFactory->createService(Bootstrap::getServiceManager());
     $event = new MvcEvent();
     $event->setTarget($controllerMock);
     $event->setRouter($router);
     $event->setResponse(new Response());
     return $event;
 }
예제 #15
0
 /**
  * Get the MVC event instance
  * 
  * @return MvcEvent
  */
 public function getMvcEvent()
 {
     if ($this->event) {
         return $this->event;
     }
     $this->event = $event = new MvcEvent();
     $event->setTarget($this);
     $event->setRequest($this->getRequest())->setResponse($this->getResponse())->setRouter($this->getRouter());
     return $event;
 }
 public function testDecodingMultipartFormDataWithFileRegistersFileCleanupEventListener()
 {
     $request = new Request();
     $request->setMethod('PATCH');
     $request->getHeaders()->addHeaderLine('Content-Type', 'multipart/form-data; boundary=6603ddd555b044dc9a022f3ad9281c20');
     $request->setContent(file_get_contents(__DIR__ . '/TestAsset/multipart-form-data.txt'));
     $target = new TestAsset\EventTarget();
     $events = $this->getMock('Zend\\EventManager\\EventManagerInterface');
     $events->expects($this->once())->method('attach')->with($this->equalTo('finish'), $this->equalTo(array($this->listener, 'onFinish')), $this->equalTo(1000));
     $target->events = $events;
     $event = new MvcEvent();
     $event->setTarget($target);
     $event->setRequest($request);
     $event->setRouteMatch(new RouteMatch(array()));
     $listener = $this->listener;
     $result = $listener($event);
 }
예제 #17
0
 /**
  * @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));
     }
 }
 /**
  * @author Fabian Köstring
  */
 public function testOnBootstrapEmptyConfig()
 {
     $applicationEventManager = new EventManager();
     $serviceManager = $this->getMock('Zend\\ServiceManager\\ServiceLocatorInterface');
     $serviceManager->expects($this->any())->method('get')->will($this->returnValue(['zf2-doctrine-elasticsearch-sync' => []]));
     $application = $this->getMock('Zend\\Mvc\\ApplicationInterface');
     $application->expects($this->any())->method('getEventManager')->will($this->returnValue($applicationEventManager));
     $application->expects($this->once())->method('getServiceManager')->will($this->returnValue($serviceManager));
     $event = new MvcEvent();
     $event->setTarget($application);
     $event->setApplication($application);
     $module = new Module();
     $module->onBootstrap($event);
     $this->assertEmpty($applicationEventManager->getListeners(MvcEvent::EVENT_DISPATCH));
     $this->assertEmpty($applicationEventManager->getListeners(ORM\Events::onFlush));
 }
예제 #19
0
 public function initRoutes(MvcEvent $e)
 {
     $event = clone $e;
     $e->setTarget($this);
     $this->events->trigger('route.init', $event);
 }
예제 #20
0
 /**
  * Run the application
  * 
  * @triggers route(MvcEvent)
  *           Routes the request, and sets the RouteMatch object in the event.
  * @triggers dispatch(MvcEvent)
  *           Dispatches a request, using the discovered RouteMatch and 
  *           provided request.
  * @triggers dispatch.error(MvcEvent)
  *           On errors (controller not found, action not supported, etc.), 
  *           populates the event with information about the error type, 
  *           discovered controller, and controller class (if known). 
  *           Typically, a handler should return a populated Response object
  *           that can be returned immediately.
  * @return SendableResponse
  */
 public function run()
 {
     $events = $this->events();
     $event = new MvcEvent();
     $event->setTarget($this);
     $event->setRequest($this->getRequest())->setRouter($this->getRouter());
     $result = $events->trigger('route', $event, function ($r) {
         return $r instanceof Response;
     });
     if ($result->stopped()) {
         $response = $result->last();
         return $response;
     }
     $result = $events->trigger('dispatch', $event, function ($r) {
         return $r instanceof Response;
     });
     $response = $result->last();
     if (!$response instanceof Response) {
         $response = $this->getResponse();
         $event->setResponse($response);
     }
     $events->trigger('finish', $event);
     $response = $response;
     return $response;
 }
예제 #21
0
 /**
  * Complete the request
  *
  * Triggers "render" and "finish" events, and returns response from
  * event object.
  *
  * @param  MvcEvent $event
  * @return Application
  */
 protected function completeRequest(MvcEvent $event)
 {
     $events = $this->events;
     $event->setTarget($this);
     $event->setName(MvcEvent::EVENT_RENDER);
     $event->stopPropagation(false);
     // Clear before triggering
     $events->triggerEvent($event);
     $event->setName(MvcEvent::EVENT_FINISH);
     $event->stopPropagation(false);
     // Clear before triggering
     $events->triggerEvent($event);
     return $this;
 }