CellTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. $cell = $this->View->cell('Articles::teaserList');
  75. $cell->template = 'nope';
  76. $output = "{$cell}";
  77. $this->assertStringStartsWith("Error: Could not render cell - View file", $output);
  78. }
  79. /**
  80. * Tests that we are able pass multiple arguments to cell methods.
  81. *
  82. * @return void
  83. */
  84. public function testCellWithArguments() {
  85. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  86. $render = "{$cell}";
  87. $this->assertContains('dummy message', $render);
  88. }
  89. /**
  90. * Tests that cell runs default action when none is provided.
  91. *
  92. * @return void
  93. */
  94. public function testDefaultCellAction() {
  95. $appCell = $this->View->cell('Articles');
  96. $this->assertEquals('display', $appCell->template);
  97. $this->assertContains('dummy', "{$appCell}");
  98. $pluginCell = $this->View->cell('TestPlugin.Dummy');
  99. $this->assertContains('dummy', "{$pluginCell}");
  100. $this->assertEquals('display', $pluginCell->template);
  101. }
  102. /**
  103. * Tests manual render() invocation.
  104. *
  105. * @return void
  106. */
  107. public function testCellManualRender() {
  108. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  109. $this->assertContains('dummy message', $cell->render());
  110. $cell->teaserList();
  111. $this->assertContains('<h2>Lorem ipsum</h2>', $cell->render('teaser_list'));
  112. }
  113. /**
  114. * Test rendering a cell with a theme.
  115. *
  116. * @return void
  117. */
  118. public function testCellRenderThemed() {
  119. $this->View->theme = 'TestTheme';
  120. $cell = $this->View->cell('Articles', ['msg' => 'hello world!']);
  121. $this->assertEquals($this->View->theme, $cell->theme);
  122. $this->assertContains('Themed cell content.', $cell->render());
  123. $this->assertEquals($cell->View->theme, $cell->theme);
  124. }
  125. /**
  126. * Tests that using plugin's cells works.
  127. *
  128. * @return void
  129. */
  130. public function testPluginCell() {
  131. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  132. $this->assertContains('hello world!', "{$cell}");
  133. }
  134. /**
  135. * Tests that using an unexisting cell throws an exception.
  136. *
  137. * @expectedException \Cake\View\Error\MissingCellException
  138. * @return void
  139. */
  140. public function testUnexistingCell() {
  141. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  142. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  143. }
  144. /**
  145. * Tests missing method errors
  146. *
  147. * @expectedException \BadMethodCallException
  148. * @expectedExceptionMessage Class TestApp\View\Cell\ArticlesCell does not have a "nope" method.
  149. * @return void
  150. */
  151. public function testCellMissingMethod() {
  152. $this->View->cell('Articles::nope');
  153. }
  154. }