/tests/ZendTest/View/ViewTest.php

https://github.com/zucchi/zf2 · PHP · 293 lines · 225 code · 57 blank · 11 comment · 2 complexity · 9d8c89fc0311d35347c4d65781ce690f MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_View
  9. */
  10. namespace ZendTest\View;
  11. use ArrayObject;
  12. use PHPUnit_Framework_TestCase as TestCase;
  13. use stdClass;
  14. use Zend\Http\Request;
  15. use Zend\Http\Response;
  16. use Zend\View\Model\ViewModel;
  17. use Zend\View\Model\JsonModel;
  18. use Zend\View\Renderer\PhpRenderer;
  19. use Zend\View\Renderer;
  20. use Zend\View\Resolver;
  21. use Zend\View\Variables as ViewVariables;
  22. use Zend\View\View;
  23. class ViewTest extends TestCase
  24. {
  25. public function setUp()
  26. {
  27. $this->request = new Request;
  28. $this->response = new Response;
  29. $this->model = new ViewModel;
  30. $this->view = new View;
  31. $this->view->setRequest($this->request);
  32. $this->view->setResponse($this->response);
  33. }
  34. public function attachTestStrategies()
  35. {
  36. $this->view->addRenderingStrategy(function ($e) {
  37. return new TestAsset\Renderer\VarExportRenderer();
  38. });
  39. $this->result = $result = new stdClass;
  40. $this->view->addResponseStrategy(function ($e) use ($result) {
  41. $result->content = $e->getResult();
  42. });
  43. }
  44. public function testRendersViewModelWithNoChildren()
  45. {
  46. $this->attachTestStrategies();
  47. $variables = array(
  48. 'foo' => 'bar',
  49. 'bar' => 'baz',
  50. );
  51. $this->model->setVariables($variables);
  52. $this->view->render($this->model);
  53. foreach ($variables as $key => $value) {
  54. $expect = sprintf("'%s' => '%s',", $key, $value);
  55. $this->assertContains($expect, $this->result->content);
  56. }
  57. }
  58. public function testRendersViewModelWithChildren()
  59. {
  60. $this->attachTestStrategies();
  61. $child1 = new ViewModel(array('foo' => 'bar'));
  62. $child2 = new ViewModel(array('bar' => 'baz'));
  63. $this->model->setVariable('parent', 'node');
  64. $this->model->addChild($child1, 'child1');
  65. $this->model->addChild($child2, 'child2');
  66. $this->view->render($this->model);
  67. $expected = var_export(new ViewVariables(array(
  68. 'parent' => 'node',
  69. 'child1' => var_export(array('foo' => 'bar'), true),
  70. 'child2' => var_export(array('bar' => 'baz'), true),
  71. )), true);
  72. $this->assertEquals($expected, $this->result->content);
  73. }
  74. public function testRendersTreeOfModels()
  75. {
  76. $this->attachTestStrategies();
  77. $child1 = new ViewModel(array('foo' => 'bar'));
  78. $child1->setCaptureTo('child1');
  79. $child2 = new ViewModel(array('bar' => 'baz'));
  80. $child2->setCaptureTo('child2');
  81. $child1->addChild($child2);
  82. $this->model->setVariable('parent', 'node');
  83. $this->model->addChild($child1);
  84. $this->view->render($this->model);
  85. $expected = var_export(new ViewVariables(array(
  86. 'parent' => 'node',
  87. 'child1' => var_export(array(
  88. 'foo' => 'bar',
  89. 'child2' => var_export(array('bar' => 'baz'), true),
  90. ), true),
  91. )), true);
  92. $this->assertEquals($expected, $this->result->content);
  93. }
  94. public function testChildrenMayInvokeDifferentRenderingStrategiesThanParents()
  95. {
  96. $this->view->addRenderingStrategy(function ($e) {
  97. $model = $e->getModel();
  98. if (!$model instanceof ViewModel) {
  99. return;
  100. }
  101. return new TestAsset\Renderer\VarExportRenderer();
  102. });
  103. $this->view->addRenderingStrategy(function ($e) {
  104. $model = $e->getModel();
  105. if (!$model instanceof JsonModel) {
  106. return;
  107. }
  108. return new Renderer\JsonRenderer();
  109. }, 10); // higher priority, so it matches earlier
  110. $this->result = $result = new stdClass;
  111. $this->view->addResponseStrategy(function ($e) use ($result) {
  112. $result->content = $e->getResult();
  113. });
  114. $child1 = new ViewModel(array('foo' => 'bar'));
  115. $child1->setCaptureTo('child1');
  116. $child2 = new JsonModel(array('bar' => 'baz'));
  117. $child2->setCaptureTo('child2');
  118. $child2->setTerminal(false);
  119. $this->model->setVariable('parent', 'node');
  120. $this->model->addChild($child1);
  121. $this->model->addChild($child2);
  122. $this->view->render($this->model);
  123. $expected = var_export(new ViewVariables(array(
  124. 'parent' => 'node',
  125. 'child1' => var_export(array('foo' => 'bar'), true),
  126. 'child2' => json_encode(array('bar' => 'baz')),
  127. )), true);
  128. $this->assertEquals($expected, $this->result->content);
  129. }
  130. public function testTerminalChildRaisesException()
  131. {
  132. $this->attachTestStrategies();
  133. $child1 = new ViewModel(array('foo' => 'bar'));
  134. $child1->setCaptureTo('child1');
  135. $child1->setTerminal(true);
  136. $this->model->setVariable('parent', 'node');
  137. $this->model->addChild($child1);
  138. $this->setExpectedException('Zend\View\Exception\DomainException');
  139. $this->view->render($this->model);
  140. }
  141. public function testChildrenAreCapturedToParentVariables()
  142. {
  143. // I wish there were a "markTestRedundant()" method in PHPUnit
  144. $this->testRendersViewModelWithChildren();
  145. }
  146. public function testOmittingCaptureToValueInChildLeadsToOmissionInParent()
  147. {
  148. $this->attachTestStrategies();
  149. $child1 = new ViewModel(array('foo' => 'bar'));
  150. $child1->setCaptureTo('child1');
  151. // Deliberately disable the "capture to" declaration
  152. $child2 = new ViewModel(array('bar' => 'baz'));
  153. $child2->setCaptureTo(null);
  154. $this->model->setVariable('parent', 'node');
  155. $this->model->addChild($child1);
  156. $this->model->addChild($child2);
  157. $this->view->render($this->model);
  158. $expected = var_export(new ViewVariables(array(
  159. 'parent' => 'node',
  160. 'child1' => var_export(array('foo' => 'bar'), true),
  161. )), true);
  162. $this->assertEquals($expected, $this->result->content);
  163. }
  164. public function testResponseStrategyIsTriggeredForParentModel()
  165. {
  166. // I wish there were a "markTestRedundant()" method in PHPUnit
  167. $this->testRendersViewModelWithChildren();
  168. }
  169. public function testResponseStrategyIsNotTriggeredForChildModel()
  170. {
  171. $this->view->addRenderingStrategy(function ($e) {
  172. return new Renderer\JsonRenderer();
  173. });
  174. $result = new ArrayObject;
  175. $this->view->addResponseStrategy(function ($e) use ($result) {
  176. $result[] = $e->getResult();
  177. });
  178. $child1 = new ViewModel(array('foo' => 'bar'));
  179. $child1->setCaptureTo('child1');
  180. $child2 = new ViewModel(array('bar' => 'baz'));
  181. $child2->setCaptureTo('child2');
  182. $this->model->setVariable('parent', 'node');
  183. $this->model->addChild($child1);
  184. $this->model->addChild($child2);
  185. $this->view->render($this->model);
  186. $this->assertEquals(1, count($result));
  187. }
  188. public function testUsesTreeRendererInterfaceToDetermineWhetherOrNotToPassOnlyRootViewModelToPhpRenderer()
  189. {
  190. $resolver = new Resolver\TemplateMapResolver(array(
  191. 'layout' => __DIR__ . '/_templates/nested-view-model-layout.phtml',
  192. 'content' => __DIR__ . '/_templates/nested-view-model-content.phtml',
  193. ));
  194. $phpRenderer = new PhpRenderer();
  195. $phpRenderer->setCanRenderTrees(true);
  196. $phpRenderer->setResolver($resolver);
  197. $this->view->addRenderingStrategy(function ($e) use ($phpRenderer) {
  198. return $phpRenderer;
  199. });
  200. $result = new stdClass;
  201. $this->view->addResponseStrategy(function ($e) use ($result) {
  202. $result->content = $e->getResult();
  203. });
  204. $layout = new ViewModel();
  205. $layout->setTemplate('layout');
  206. $content = new ViewModel();
  207. $content->setTemplate('content');
  208. $content->setCaptureTo('content');
  209. $layout->addChild($content);
  210. $this->view->render($layout);
  211. $this->assertContains('Layout start', $result->content);
  212. $this->assertContains('Content for layout', $result->content, $result->content);
  213. $this->assertContains('Layout end', $result->content);
  214. }
  215. public function testUsesTreeRendererInterfaceToDetermineWhetherOrNotToPassOnlyRootViewModelToJsonRenderer()
  216. {
  217. $jsonRenderer = new Renderer\JsonRenderer();
  218. $this->view->addRenderingStrategy(function ($e) use ($jsonRenderer) {
  219. return $jsonRenderer;
  220. });
  221. $result = new stdClass;
  222. $this->view->addResponseStrategy(function ($e) use ($result) {
  223. $result->content = $e->getResult();
  224. });
  225. $layout = new ViewModel(array('status' => 200));
  226. $content = new ViewModel(array('foo' => 'bar'));
  227. $content->setCaptureTo('response');
  228. $layout->addChild($content);
  229. $this->view->render($layout);
  230. $expected = json_encode(array(
  231. 'status' => 200,
  232. 'response' => array('foo' => 'bar'),
  233. ));
  234. $this->assertEquals($expected, $result->content);
  235. }
  236. }