CellTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /**
  24. * CellTest class.
  25. *
  26. * For testing both View\Cell & Utility\CellTrait
  27. */
  28. class CellTest extends TestCase {
  29. /**
  30. * setUp method
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. Configure::write('App.namespace', 'TestApp');
  37. Configure::write('debug', 2);
  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 - 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. * Test rendering a cell with a theme.
  123. *
  124. * @return void
  125. */
  126. public function testCellRenderThemed() {
  127. $this->View->theme = 'TestTheme';
  128. $cell = $this->View->cell('Articles', ['msg' => 'hello world!']);
  129. $this->assertEquals($this->View->theme, $cell->theme);
  130. $this->assertContains('Themed cell content.', $cell->render());
  131. $this->assertEquals($cell->View->theme, $cell->theme);
  132. }
  133. /**
  134. * Tests that using plugin's cells works.
  135. *
  136. * @return void
  137. */
  138. public function testPluginCell() {
  139. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  140. $this->assertContains('hello world!', "{$cell}");
  141. }
  142. /**
  143. * Tests that using an unexisting cell throws an exception.
  144. *
  145. * @expectedException \Cake\View\Error\MissingCellException
  146. * @return void
  147. */
  148. public function testUnexistingCell() {
  149. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  150. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  151. }
  152. /**
  153. * Tests missing method errors
  154. *
  155. * @expectedException \BadMethodCallException
  156. * @expectedExceptionMessage Class TestApp\View\Cell\ArticlesCell does not have a "nope" method.
  157. * @return void
  158. */
  159. public function testCellMissingMethod() {
  160. $this->View->cell('Articles::nope');
  161. }
  162. }