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 testGetCaptureTo() { $viewModel = new ViewModel(); $viewModel->setCaptureTo('root::footer'); $viewModel->setOption('parent', 'some.parent'); $layout = new TestLayout($this->updaterMock, $this->blockPool); $this->assertEquals(['some.parent', 'footer'], $layout->getCaptureTo($viewModel)); }
public function testCanMergeChildModelsWithoutCaptureToValues() { $this->renderer->setMergeUnnamedChildren(true); $root = new ViewModel(array('foo' => 'bar')); $child1 = new ViewModel(array('foo' => 'baz')); $child2 = new ViewModel(array('foo' => 'bar')); $child1->setCaptureTo(false); $child2->setCaptureTo('child2'); $root->addChild($child1)->addChild($child2); $expected = array('foo' => 'baz', 'child2' => array('foo' => 'bar')); $test = $this->renderer->render($root); $this->assertEquals(json_encode($expected), $test); }
public function testCollect() { $event = new MvcEvent(); $layoutModel = new ViewModel(); $layoutModel->setTemplate('layout/2cols-left'); $event->setViewModel($layoutModel); $testBlock = new ViewModel(); $testBlock->setTemplate('test/block'); $testBlock->setCaptureTo('sidebarLeft'); $this->layout->addBlock('test.block', $testBlock); $this->collector->collect($event); $this->assertEquals('layout/2cols-left', $this->collector->getLayoutTemplate()); $this->assertInternalType('array', $this->collector->getHandles()); $this->assertContainsOnlyInstancesOf('ConLayout\\Handle\\HandleInterface', $this->collector->getHandles()); $this->assertInternalType('array', $this->collector->getBlocks()); $testBlockArray = current($this->collector->getBlocks()); $this->assertEquals('test/block', $testBlockArray['template']); $this->assertEquals('sidebarLeft', $testBlockArray['capture_to']); $this->assertEquals('Zend\\View\\Model\\ViewModel', $testBlockArray['class']); $this->assertInternalType('array', $this->collector->getLayoutStructure()); }
public function testRendersSequentialChildrenWithNestedChildren() { $this->parent->setTemplate('complex'); $child1 = $this->setupFirstChild(); $child1->setTemplate('layout'); $child1->setCaptureTo('content'); $child2 = $this->setupSecondChild(); $child2->setCaptureTo('sidebar'); $nested = new ViewModel(); $nested->setTemplate('child1'); $nested->setCaptureTo('content'); $child1->addChild($nested); $result = $this->renderer->render($this->parent); $this->assertRegExp('/Content:\s+Layout start\s+Content for layout\s+Layout end\s+Sidebar:\s+Second child/s', $result, $result); }
public function testCaptureToValueIsMutable() { $model = new ViewModel(); $model->setCaptureTo('foo'); $this->assertEquals('foo', $model->captureTo()); }
public function testUsesTreeRendererInterfaceToDetermineWhetherOrNotToPassOnlyRootViewModelToJsonRenderer() { $jsonRenderer = new Renderer\JsonRenderer(); $this->view->addRenderingStrategy(function ($e) use($jsonRenderer) { return $jsonRenderer; }); $result = new stdClass(); $this->view->addResponseStrategy(function ($e) use($result) { $result->content = $e->getResult(); }); $layout = new ViewModel(array('status' => 200)); $content = new ViewModel(array('foo' => 'bar')); $content->setCaptureTo('response'); $layout->addChild($content); $this->view->render($layout); $expected = json_encode(array('status' => 200, 'response' => array('foo' => 'bar'))); $this->assertEquals($expected, $result->content); }
/** * @param $config * @param array $data * @return array|object|ViewModel */ public function build($config, array $data = array()) { $allOptions = $this->config->getOptions(); $i = 0; $queue = new \SplQueue(); $queue->enqueue($config); while ($queue->count() > 0) { $options = $queue->dequeue(); $options = $this->config->applyInheritance($options); if (isset($options['viewModel'])) { $this->serviceLocator->setShared($options['viewModel'], false); $viewModel = $this->serviceLocator->get($options['viewModel']); } else { $viewModel = new ZendViewModel(); } if ($i == 0) { $rootViewModel = $viewModel; } if (isset($options['template'])) { $viewModel->setTemplate($options['template']); } if (isset($options['capture'])) { $viewModel->setCaptureTo($options['capture']); } elseif (isset($options['id'])) { $viewModel->setCaptureTo($options['id']); } if (isset($options['data']['static'])) { $viewModel->setVariables($options['data']['static']); } if (isset($options['data']['fromGlobal'])) { $globalVar = $options['data']['fromGlobal']; if (is_array($globalVar)) { foreach ($globalVar as $globalVarName => $viewVarName) { $globalVarValue = $this->getVarValue($globalVarName, $data); $viewModel->setVariable($viewVarName, $globalVarValue); } } else { $globalVarValue = $this->getVarValue($globalVar, $data); $viewModel->setVariable($globalVar, $globalVarValue); } } if (isset($options['parent'])) { /** @var ViewModel $parent */ $parent = $options['parent']; $parent->addChild($viewModel, $viewModel->captureTo(), true); if (isset($options['data']['fromParent'])) { $varFromParent = $options['data']['fromParent']; $parentVars = $parent->getVariables(); if (is_array($varFromParent)) { foreach ($varFromParent as $varFromParentName => $viewVarName) { $fromParentVal = $this->getVarValue($varFromParentName, $parentVars); if ($fromParentVal === null) { $fromParentVal = $parent->getVariable($varFromParentName); } if (is_array($viewVarName)) { $dataFromParent = []; foreach ($viewVarName as $varName) { $dataFromParent[$varName] = $fromParentVal; } } else { $dataFromParent = [$viewVarName => $fromParentVal]; } $viewModel->setVariables($dataFromParent); } } else { $viewVarName = $options['data']['fromParent']; $fromParentVal = $this->getVarValue($viewVarName, $parentVars); if ($fromParentVal === null) { $fromParentVal = $parent->getVariable($viewVarName); } $viewModel->setVariables([$viewVarName => $fromParentVal]); } } } if (!empty($options['children'])) { foreach ($options['children'] as $childId => $child) { if (is_string($child)) { $childId = $child; $child = $allOptions[$child]; } if (isset($options['childrenDynamicLists'][$childId])) { continue; } $child['id'] = $childId; $child['parent'] = $viewModel; $queue->enqueue($child); } } if (isset($options['childrenDynamicLists'])) { foreach ($options['childrenDynamicLists'] as $childName => $listName) { $list = $viewModel->getVariable($listName); if ($list === null) { throw new \UnexpectedValueException("Cannot build children list of '{$childName}' by '{$listName}' list . View does not contain variable '{$listName}'."); } if (!is_array($list) && !$list instanceof \Traversable) { throw new \UnexpectedValueException("Cannot build children list of '{$childName}' by '{$listName}' list . List '{$listName}' must be array " . gettype($list) . " given."); } if (array_key_exists($childName, $options['children'])) { $child = $options['children'][$childName]; } else { if (in_array($childName, $options['children'])) { $child = $allOptions[$childName]; } else { throw new \UnexpectedValueException("Cannot build children list of '{$childName}' by '{$listName}' list . Child '{$childName}' not found"); } } $child['id'] = $childName; $child['parent'] = $viewModel; if (isset($child['data']['fromParent'])) { $varFromParent = $child['data']['fromParent']; } foreach ($list as $entry) { if (isset($varFromParent)) { if (is_array($varFromParent)) { foreach ($varFromParent as $varFromParentName => $viewVarName) { $dataForChild = [$viewVarName => $entry]; } } else { $dataForChild = [$varFromParent => $entry]; } if (!isset($child['data']['static'])) { $child['data']['static'] = []; } $child['data']['static'] = array_merge($child['data']['static'], $dataForChild); unset($child['data']['fromParent']); } $queue->enqueue($child); } } } $i++; } return $rootViewModel; }