CellTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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');
  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. unset($this->View);
  52. }
  53. /**
  54. * Tests basic cell rendering.
  55. *
  56. * @return void
  57. */
  58. public function testCellRender() {
  59. $cell = $this->View->cell('Articles::teaserList');
  60. $render = "{$cell}";
  61. $this->assertEquals('teaser_list', $cell->template);
  62. $this->assertContains('<h2>Lorem ipsum</h2>', $render);
  63. $this->assertContains('<h2>Usectetur adipiscing eli</h2>', $render);
  64. $this->assertContains('<h2>Topis semper blandit eu non</h2>', $render);
  65. $this->assertContains('<h2>Suspendisse gravida neque</h2>', $render);
  66. }
  67. /**
  68. * Tests that we are able pass multiple arguments to cell methods.
  69. *
  70. * @return void
  71. */
  72. public function testCellWithArguments() {
  73. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  74. $render = "{$cell}";
  75. $this->assertContains('dummy message', $render);
  76. }
  77. /**
  78. * Tests that cell runs default action when none is provided.
  79. *
  80. * @return void
  81. */
  82. public function testDefaultCellAction() {
  83. $appCell = $this->View->cell('Articles');
  84. $this->assertEquals('display', $appCell->template);
  85. $this->assertContains('dummy', "{$appCell}");
  86. $pluginCell = $this->View->cell('TestPlugin.Dummy');
  87. $this->assertContains('dummy', "{$pluginCell}");
  88. $this->assertEquals('display', $pluginCell->template);
  89. }
  90. /**
  91. * Tests manual render() invocation.
  92. *
  93. * @return void
  94. */
  95. public function testCellManualRender() {
  96. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  97. $this->assertContains('dummy message', $cell->render());
  98. $cell->teaserList();
  99. $this->assertContains('<h2>Lorem ipsum</h2>', $cell->render('teaser_list'));
  100. }
  101. /**
  102. * Test rendering a cell with a theme.
  103. *
  104. * @return void
  105. */
  106. public function testCellRenderThemed() {
  107. $this->View->theme = 'TestTheme';
  108. $cell = $this->View->cell('Articles', ['msg' => 'hello world!']);
  109. $this->assertEquals($this->View->theme, $cell->theme);
  110. $this->assertContains('Themed cell content.', $cell->render());
  111. $this->assertEquals($cell->View->theme, $cell->theme);
  112. }
  113. /**
  114. * Tests that using plugin's cells works.
  115. *
  116. * @return void
  117. */
  118. public function testPluginCell() {
  119. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  120. $this->assertContains('hello world!', "{$cell}");
  121. }
  122. /**
  123. * Tests that using an unexisting cell throws an exception.
  124. *
  125. * @expectedException \Cake\View\Error\MissingCellException
  126. * @return void
  127. */
  128. public function testUnexistingCell() {
  129. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  130. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  131. }
  132. }