CellTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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\Cache\Cache;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Event\EventManager;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\View\Cell;
  23. use Cake\View\CellTrait;
  24. use TestApp\View\CustomJsonView;
  25. /**
  26. * CellTest class.
  27. *
  28. * For testing both View\Cell & Utility\CellTrait
  29. */
  30. class CellTest extends TestCase
  31. {
  32. /**
  33. * setUp method
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. parent::setUp();
  40. Configure::write('App.namespace', 'TestApp');
  41. Plugin::load(['TestPlugin', 'TestTheme']);
  42. $request = $this->getMock('Cake\Network\Request');
  43. $response = $this->getMock('Cake\Network\Response');
  44. $this->View = new \Cake\View\View($request, $response);
  45. }
  46. /**
  47. * tearDown method
  48. *
  49. * @return void
  50. */
  51. public function tearDown()
  52. {
  53. parent::tearDown();
  54. Plugin::unload('TestPlugin');
  55. Plugin::unload('TestTheme');
  56. unset($this->View);
  57. }
  58. /**
  59. * Tests basic cell rendering.
  60. *
  61. * @return void
  62. */
  63. public function testCellRender()
  64. {
  65. $cell = $this->View->cell('Articles::teaserList');
  66. $render = "{$cell}";
  67. $this->assertEquals('teaser_list', $cell->template);
  68. $this->assertContains('<h2>Lorem ipsum</h2>', $render);
  69. $this->assertContains('<h2>Usectetur adipiscing eli</h2>', $render);
  70. $this->assertContains('<h2>Topis semper blandit eu non</h2>', $render);
  71. $this->assertContains('<h2>Suspendisse gravida neque</h2>', $render);
  72. }
  73. /**
  74. * Test __toString() hitting an error when rendering views.
  75. *
  76. * @return void
  77. */
  78. public function testCellImplictRenderWithError()
  79. {
  80. $capture = function ($errno, $msg) {
  81. restore_error_handler();
  82. $this->assertEquals(E_USER_WARNING, $errno);
  83. $this->assertContains('Could not render cell - Cell view file', $msg);
  84. };
  85. set_error_handler($capture);
  86. $cell = $this->View->cell('Articles::teaserList');
  87. $cell->template = 'nope';
  88. $result = "{$cell}";
  89. }
  90. /**
  91. * Tests that we are able pass multiple arguments to cell methods.
  92. *
  93. * This test sets its own error handler, as PHPUnit won't convert
  94. * errors into exceptions when the caller is a __toString() method.
  95. *
  96. * @return void
  97. */
  98. public function testCellWithArguments()
  99. {
  100. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  101. $render = "{$cell}";
  102. $this->assertContains('dummy message', $render);
  103. }
  104. /**
  105. * Tests that cell runs default action when none is provided.
  106. *
  107. * @return void
  108. */
  109. public function testDefaultCellAction()
  110. {
  111. $appCell = $this->View->cell('Articles');
  112. $this->assertEquals('display', $appCell->template);
  113. $this->assertContains('dummy', "{$appCell}");
  114. $pluginCell = $this->View->cell('TestPlugin.Dummy');
  115. $this->assertContains('dummy', "{$pluginCell}");
  116. $this->assertEquals('display', $pluginCell->template);
  117. }
  118. /**
  119. * Tests manual render() invocation.
  120. *
  121. * @return void
  122. */
  123. public function testCellManualRender()
  124. {
  125. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  126. $this->assertContains('dummy message', $cell->render());
  127. $cell->teaserList();
  128. $this->assertContains('<h2>Lorem ipsum</h2>', $cell->render('teaser_list'));
  129. }
  130. /**
  131. * Tests manual render() invocation with error
  132. *
  133. * @expectedException \Cake\View\Exception\MissingCellViewException
  134. * @return void
  135. */
  136. public function testCellManualRenderError()
  137. {
  138. $cell = $this->View->cell('Articles');
  139. $cell->render('derp');
  140. }
  141. /**
  142. * Test rendering a cell with a theme.
  143. *
  144. * @return void
  145. */
  146. public function testCellRenderThemed()
  147. {
  148. $this->View->theme = 'TestTheme';
  149. $cell = $this->View->cell('Articles', ['msg' => 'hello world!']);
  150. $this->assertEquals($this->View->theme, $cell->theme);
  151. $this->assertContains('Themed cell content.', $cell->render());
  152. $this->assertEquals($cell->View->theme, $cell->theme);
  153. }
  154. /**
  155. * Tests that using plugin's cells works.
  156. *
  157. * @return void
  158. */
  159. public function testPluginCell()
  160. {
  161. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  162. $this->assertContains('hello world!', "{$cell}");
  163. }
  164. /**
  165. * Test that plugin cells can render other view templates.
  166. *
  167. * @return void
  168. */
  169. public function testPluginCellAlternateTemplate()
  170. {
  171. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  172. $cell->template = '../../Element/translate';
  173. $this->assertContains('This is a translatable string', "{$cell}");
  174. }
  175. /**
  176. * Test that plugin cells can render other view templates.
  177. *
  178. * @return void
  179. */
  180. public function testPluginCellAlternateTemplateRenderParam()
  181. {
  182. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  183. $result = $cell->render('../../Element/translate');
  184. $this->assertContains('This is a translatable string', $result);
  185. }
  186. /**
  187. * Tests that using an unexisting cell throws an exception.
  188. *
  189. * @expectedException \Cake\View\Exception\MissingCellException
  190. * @return void
  191. */
  192. public function testUnexistingCell()
  193. {
  194. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  195. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  196. }
  197. /**
  198. * Tests missing method errors
  199. *
  200. * @expectedException \BadMethodCallException
  201. * @expectedExceptionMessage Class TestApp\View\Cell\ArticlesCell does not have a "nope" method.
  202. * @return void
  203. */
  204. public function testCellMissingMethod()
  205. {
  206. $this->View->cell('Articles::nope');
  207. }
  208. /**
  209. * Test that cell options are passed on.
  210. *
  211. * @return void
  212. */
  213. public function testCellOptions()
  214. {
  215. $cell = $this->View->cell('Articles', [], ['limit' => 10, 'nope' => 'nope']);
  216. $this->assertEquals(10, $cell->limit);
  217. $this->assertFalse(property_exists('nope', $cell), 'Not a valid option');
  218. }
  219. /**
  220. * Test that cells get the helper configuration from the view that created them.
  221. *
  222. * @return void
  223. */
  224. public function testCellInheritsHelperConfig()
  225. {
  226. $this->View->helpers = ['Url', 'Form', 'Banana'];
  227. $cell = $this->View->cell('Articles');
  228. $this->assertSame($this->View->helpers, $cell->helpers);
  229. }
  230. /**
  231. * Test that cells the view class name of a custom view passed on.
  232. *
  233. * @return void
  234. */
  235. public function testCellInheritsCustomViewClass()
  236. {
  237. $request = $this->getMock('Cake\Network\Request');
  238. $response = $this->getMock('Cake\Network\Response');
  239. $view = new CustomJsonView($request, $response);
  240. $cell = $view->cell('Articles');
  241. $this->assertSame('TestApp\View\CustomJsonView', $cell->viewClass);
  242. }
  243. /**
  244. * Test cached render.
  245. *
  246. * @return void
  247. */
  248. public function testCachedRenderSimple()
  249. {
  250. $mock = $this->getMock('Cake\Cache\CacheEngine');
  251. $mock->method('init')
  252. ->will($this->returnValue(true));
  253. $mock->method('read')
  254. ->will($this->returnValue(false));
  255. $mock->expects($this->once())
  256. ->method('write')
  257. ->with('cell_test_app_view_cell_articles_cell_display', "dummy\n");
  258. Cache::config('default', $mock);
  259. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  260. $result = $cell->render();
  261. $this->assertEquals("dummy\n", $result);
  262. Cache::drop('default');
  263. }
  264. /**
  265. * Test cached render array config
  266. *
  267. * @return void
  268. */
  269. public function testCachedRenderArrayConfig()
  270. {
  271. $mock = $this->getMock('Cake\Cache\CacheEngine');
  272. $mock->method('init')
  273. ->will($this->returnValue(true));
  274. $mock->method('read')
  275. ->will($this->returnValue(false));
  276. $mock->expects($this->once())
  277. ->method('write')
  278. ->with('my_key', "dummy\n");
  279. Cache::config('cell', $mock);
  280. $cell = $this->View->cell('Articles', [], [
  281. 'cache' => ['key' => 'my_key', 'config' => 'cell']
  282. ]);
  283. $result = $cell->render();
  284. $this->assertEquals("dummy\n", $result);
  285. Cache::drop('cell');
  286. }
  287. }