CellTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Controller\Controller;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\Event\EventManager;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\View\Cell;
  22. use Cake\View\CellTrait;
  23. use TestApp\View\CustomJsonView;
  24. /**
  25. * CellTest class.
  26. *
  27. * For testing both View\Cell & Utility\CellTrait
  28. */
  29. class CellTest extends TestCase {
  30. /**
  31. * setUp method
  32. *
  33. * @return void
  34. */
  35. public function setUp() {
  36. parent::setUp();
  37. Configure::write('App.namespace', 'TestApp');
  38. Plugin::load(['TestPlugin', 'TestTheme']);
  39. $request = $this->getMock('Cake\Network\Request');
  40. $response = $this->getMock('Cake\Network\Response');
  41. $this->View = new \Cake\View\View($request, $response);
  42. }
  43. /**
  44. * tearDown method
  45. *
  46. * @return void
  47. */
  48. public function tearDown() {
  49. parent::tearDown();
  50. Plugin::unload('TestPlugin');
  51. Plugin::unload('TestTheme');
  52. unset($this->View);
  53. }
  54. /**
  55. * Tests basic cell rendering.
  56. *
  57. * @return void
  58. */
  59. public function testCellRender() {
  60. $cell = $this->View->cell('Articles::teaserList');
  61. $render = "{$cell}";
  62. $this->assertEquals('teaser_list', $cell->template);
  63. $this->assertContains('<h2>Lorem ipsum</h2>', $render);
  64. $this->assertContains('<h2>Usectetur adipiscing eli</h2>', $render);
  65. $this->assertContains('<h2>Topis semper blandit eu non</h2>', $render);
  66. $this->assertContains('<h2>Suspendisse gravida neque</h2>', $render);
  67. }
  68. /**
  69. * Test __toString() hitting an error when rendering views.
  70. *
  71. * @return void
  72. */
  73. public function testCellImplictRenderWithError() {
  74. $capture = function ($errno, $msg) {
  75. restore_error_handler();
  76. $this->assertEquals(E_USER_WARNING, $errno);
  77. $this->assertContains('Could not render cell - Cell view file', $msg);
  78. };
  79. set_error_handler($capture);
  80. $cell = $this->View->cell('Articles::teaserList');
  81. $cell->template = 'nope';
  82. $result = "{$cell}";
  83. }
  84. /**
  85. * Tests that we are able pass multiple arguments to cell methods.
  86. *
  87. * This test sets its own error handler, as PHPUnit won't convert
  88. * errors into exceptions when the caller is a __toString() method.
  89. *
  90. * @return void
  91. */
  92. public function testCellWithArguments() {
  93. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  94. $render = "{$cell}";
  95. $this->assertContains('dummy message', $render);
  96. }
  97. /**
  98. * Tests that cell runs default action when none is provided.
  99. *
  100. * @return void
  101. */
  102. public function testDefaultCellAction() {
  103. $appCell = $this->View->cell('Articles');
  104. $this->assertEquals('display', $appCell->template);
  105. $this->assertContains('dummy', "{$appCell}");
  106. $pluginCell = $this->View->cell('TestPlugin.Dummy');
  107. $this->assertContains('dummy', "{$pluginCell}");
  108. $this->assertEquals('display', $pluginCell->template);
  109. }
  110. /**
  111. * Tests manual render() invocation.
  112. *
  113. * @return void
  114. */
  115. public function testCellManualRender() {
  116. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  117. $this->assertContains('dummy message', $cell->render());
  118. $cell->teaserList();
  119. $this->assertContains('<h2>Lorem ipsum</h2>', $cell->render('teaser_list'));
  120. }
  121. /**
  122. * Tests manual render() invocation with error
  123. *
  124. * @expectedException \Cake\View\Exception\MissingCellViewException
  125. * @return void
  126. */
  127. public function testCellManualRenderError() {
  128. $cell = $this->View->cell('Articles');
  129. $cell->render('derp');
  130. }
  131. /**
  132. * Test rendering a cell with a theme.
  133. *
  134. * @return void
  135. */
  136. public function testCellRenderThemed() {
  137. $this->View->theme = 'TestTheme';
  138. $cell = $this->View->cell('Articles', ['msg' => 'hello world!']);
  139. $this->assertEquals($this->View->theme, $cell->theme);
  140. $this->assertContains('Themed cell content.', $cell->render());
  141. $this->assertEquals($cell->View->theme, $cell->theme);
  142. }
  143. /**
  144. * Tests that using plugin's cells works.
  145. *
  146. * @return void
  147. */
  148. public function testPluginCell() {
  149. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  150. $this->assertContains('hello world!', "{$cell}");
  151. }
  152. /**
  153. * Test that plugin cells can render other view templates.
  154. *
  155. * @return void
  156. */
  157. public function testPluginCellAlternateTemplate() {
  158. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  159. $cell->template = '../../Element/translate';
  160. $this->assertContains('This is a translatable string', "{$cell}");
  161. }
  162. /**
  163. * Test that plugin cells can render other view templates.
  164. *
  165. * @return void
  166. */
  167. public function testPluginCellAlternateTemplateRenderParam() {
  168. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  169. $result = $cell->render('../../Element/translate');
  170. $this->assertContains('This is a translatable string', $result);
  171. }
  172. /**
  173. * Tests that using an unexisting cell throws an exception.
  174. *
  175. * @expectedException \Cake\View\Exception\MissingCellException
  176. * @return void
  177. */
  178. public function testUnexistingCell() {
  179. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  180. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  181. }
  182. /**
  183. * Tests missing method errors
  184. *
  185. * @expectedException \BadMethodCallException
  186. * @expectedExceptionMessage Class TestApp\View\Cell\ArticlesCell does not have a "nope" method.
  187. * @return void
  188. */
  189. public function testCellMissingMethod() {
  190. $this->View->cell('Articles::nope');
  191. }
  192. /**
  193. * Test that cell options are passed on.
  194. *
  195. * @return void
  196. */
  197. public function testCellOptions() {
  198. $cell = $this->View->cell('Articles', [], ['limit' => 10, 'nope' => 'nope']);
  199. $this->assertEquals(10, $cell->limit);
  200. $this->assertFalse(property_exists('nope', $cell), 'Not a valid option');
  201. }
  202. /**
  203. * Test that cells get the helper configuration from the view that created them.
  204. *
  205. * @return void
  206. */
  207. public function testCellInheritsHelperConfig() {
  208. $this->View->helpers = ['Url', 'Form', 'Banana'];
  209. $cell = $this->View->cell('Articles');
  210. $this->assertSame($this->View->helpers, $cell->helpers);
  211. }
  212. /**
  213. * Test that cells the view class name of a custom view passed on.
  214. *
  215. * @return void
  216. */
  217. public function testCellInheritsCustomViewClass() {
  218. $request = $this->getMock('Cake\Network\Request');
  219. $response = $this->getMock('Cake\Network\Response');
  220. $view = new CustomJsonView($request, $response);
  221. $cell = $view->cell('Articles');
  222. $this->assertSame('TestApp\View\CustomJsonView', $cell->viewClass);
  223. }
  224. }