public function testInjectTagsHeader() { $tag = InjectTagsHeaderListener::OPTION_CACHE_TAGS; $event = new MvcEvent(); $response = new Response(); $event->setResponse($response); $layout = new ViewModel(); $child1 = new ViewModel(); $child1->setOption($tag, ['tag1', 'tag2']); $layout->addChild($child1); $child2 = new ViewModel(); $child21 = new ViewModel(); $child21->setOption($tag, ['tag3', null]); $child2->addChild($child21); $layout->addChild($child2); $child3 = new ViewModel(); $child3->setOption('esi', ['ttl' => 120]); $child3->setOption($tag, 'tag4'); $layout->addChild($child3); $event->setViewModel($layout); $this->listener->injectTagsHeader($event); $this->assertSame(['tag1', 'tag2', 'tag3'], $this->listener->getCacheTags()); $headers = $response->getHeaders(); $this->assertEquals('tag1,tag2,tag3', $headers->get(VarnishService::VARNISH_HEADER_TAGS)->getFieldValue()); }
public function testCollect() { $event = new MvcEvent(); $layoutModel = new ViewModel(); $layoutModel->setTemplate('layout/2cols-left'); $event->setViewModel($layoutModel); $testBlock = new ViewModel(); $testBlock->setTemplate('widget1'); $testBlock->setCaptureTo('sidebarLeft'); $testBlock2 = new ViewModel(); $testBlock2->setOption('parent', 'test.block'); $testBlock2->setTemplate('widget1'); $this->blockPool->add('test.block', $testBlock); $this->blockPool->add('test.block2', $testBlock2); $this->collector->collect($event); $this->assertEquals('layout/2cols-left', $this->collector->getLayoutTemplate()); $this->assertInternalType('array', $this->collector->getHandles()); $this->assertContainsOnlyInstancesOf(HandleInterface::class, $this->collector->getHandles()); $this->assertInternalType('array', $this->collector->getBlocks()); $blocks = $this->collector->getBlocks(); $testBlockArray = current($blocks); $testBlock2Array = array_pop($blocks); $this->assertEquals('test.block::content', $testBlock2Array['capture_to']); $this->assertContains('_files/view/widget1.phtml', $testBlockArray['template']); $this->assertEquals('sidebarLeft', $testBlockArray['capture_to']); $this->assertEquals(ViewModel::class, $testBlockArray['class']); $this->assertEquals(LayoutUpdaterInterface::AREA_DEFAULT, $this->collector->getCurrentArea()); $this->assertInternalType('array', $this->collector->getLayoutStructure()); }
public function renderView($page, array $data) { $model = new ViewModel(); $model->setTemplate("mailer/{$page}.phtml"); $model->setOption('has_parent', true); $model->setVariables($data); return $this->view->render($model); }
public function testCastsViewModelToFeedModelUsingFeedTypeOptionProvided() { $model = new ViewModel($this->getFeedData('atom')); $model->setOption('feed_type', 'atom'); $xml = $this->renderer->render($model); $this->assertContains('<' . '?xml', $xml); $this->assertContains('atom', $xml); }
public function __invoke($revisionEntity) { $view = $this->getServiceLocator()->getServiceLocator()->get('View'); $model = new ViewModel(); $model->setTemplate('soliant-entity-audit/helper/revision-entity-link.phtml'); $model->setVariable('revisionEntity', $revisionEntity); $model->setOption('has_parent', true); return $view->render($model); }
public function testMarkupContainsCommentsWithDebugInfo() { $block = new ViewModel(); $block->setOption('block_id', 'the.block'); $viewModelHelper = new ModelHelper(); $viewModelHelper->setCurrent($block); $filter = new DebugFilter($viewModelHelper); $html = '<div></div>'; $filteredHtml = $filter->filter($html); $this->assertContains('<!--[the.block]', $filteredHtml); }
public function testParentBlockIsAddedAsOption() { $parent = new ViewModel(); $child = new ViewModel(); $child->setOption('block_id', 'child'); $child2 = new ViewModel(); $child2->setOption('block_id', 'child2'); $child->addChild($child2); $parent->addChild($child); $this->layout->setRoot($parent); $this->layout->load(); $childBlock = $this->layout->getBlock('child'); $this->assertSame($parent, $childBlock->getOption('parent_block')); $child2Block = $this->layout->getBlock('child2'); $this->assertSame($childBlock, $child2Block->getOption('parent_block')); $this->assertSame($parent, $child2Block->getOption('parent_block')->getOption('parent_block')); }
public function testCanOverwriteOptionsSingly() { $model = new ViewModel(array(), array('foo' => 'bar')); $model->setOption('foo', 'baz'); $this->assertEquals(array('foo' => 'baz'), $model->getOptions()); }
public function testBypassesRenderingIfResultIsAResponse() { $renderer = new TestAsset\DumbStrategy(); $this->view->addRenderingStrategy(function ($e) use($renderer) { return $renderer; }, 100); $model = new ViewModel(array('foo' => 'bar')); $model->setOption('template', 'content'); $this->event->setViewModel($model); $this->event->setResult($this->response); $result = $this->strategy->render($this->event); $this->assertSame($this->response, $result); }
/** * Render an action from a controller and render it's associated template * @param string $expr * @param array $attributes * @param array $options * @return string */ public function renderAction($expr, array $attributes, array $options) { ArgValidator::assert($expr, 'string'); $serviceManager = Module::getServiceManager(); $application = $serviceManager->get('Application'); //parse the name of the controller, action and template directory that should be used if (strpos($expr, '/') > 0) { list($controllerName, $actionName) = explode('/', $expr); $templateDir = $controllerName . '/'; } else { list($moduleName, $controllerName, $actionName) = explode(':', $expr); $actionName = lcfirst($actionName); $actionName = strtolower(preg_replace('/([A-Z])/', '-$1', $actionName)); $templateDir = lcfirst($moduleName) . '/' . lcfirst($controllerName) . '/'; $controllerName = $moduleName . '\\Controller\\' . $controllerName . 'Controller'; } //instantiate the controller based on the given name $controller = $serviceManager->get('ControllerLoader')->get($controllerName); //clone the MvcEvent and route and update them with the provided parameters $event = $application->getMvcEvent(); $routeMatch = clone $event->getRouteMatch(); $event = clone $event; foreach ($attributes as $key => $value) { $routeMatch->setParam($key, $value); } $event->setRouteMatch($routeMatch); //inject the new event into the controller if ($controller instanceof InjectApplicationEventInterface) { $controller->setEvent($event); } //test if the action exists in the controller and change it to not-found if missing $method = AbstractActionController::getMethodFromAction($actionName); if (!method_exists($controller, $method)) { $method = 'notFoundAction'; $actionName = 'not-found'; } //call the method on the controller $response = $controller->{$method}(); //if the result is an instance of the Response class return it if ($response instanceof Response) { return $response->getBody(); } //if the response is an instance of ViewModel then render that one if ($response instanceof ModelInterface) { $viewModel = $response; } elseif ($response === null || is_array($response) || $response instanceof \ArrayAccess || $response instanceof \Traversable) { $viewModel = new ViewModel($response); $viewModel->setTemplate($templateDir . $actionName); } else { return ''; } $viewModel->terminate(); $viewModel->setOption('has_parent', true); $view = $serviceManager->get('Zend\\View\\View'); $output = $view->render($viewModel); return $output; }
public function testGetCaptureToDefault() { $viewModel = new ViewModel(); $viewModel->setOption('parent', 'some.parent'); $layout = new TestLayout($this->blockFactory, $this->updaterMock); $this->assertEquals(['some.parent', 'content'], $layout->getCaptureTo($viewModel)); }
public function testInjectEsi() { $block = new ViewModel(); $block->setOption('esi', ['ttl' => 120]); $block->setTemplate('default'); $event = new Event(); $event->setParam('block', $block); $this->listener->determineEsiProcessing($this->mvcEvent); $this->listener->injectEsi($event); $this->assertEquals('default', $block->getTemplate()); $this->listener->setCanUseEsi(true); $this->listener->injectEsi($event); $this->assertEquals(InjectCacheHeaderListener::ESI_TEMPLATE, $block->getTemplate()); }