CellTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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->assertTrue(
  62. strpos($render, '<h2>Lorem ipsum</h2>') !== false &&
  63. strpos($render, '<h2>Usectetur adipiscing eli</h2>') !== false &&
  64. strpos($render, '<h2>Topis semper blandit eu non</h2>') !== false &&
  65. strpos($render, '<h2>Suspendisse gravida neque</h2>') !== false
  66. );
  67. }
  68. /**
  69. * Tests that we are able pass multiple arguments to cell methods.
  70. *
  71. * @return void
  72. */
  73. public function testCellWithArguments() {
  74. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  75. $render = "{$cell}";
  76. $this->assertTrue(strpos($render, 'dummy message') !== false);
  77. }
  78. /**
  79. * Tests that cell runs default action when none is provided.
  80. *
  81. * @return void
  82. */
  83. public function testDefaultCellAction() {
  84. $appCell = $this->View->cell('Articles');
  85. $this->assertTrue(strpos("{$appCell}", 'dummy') !== false);
  86. $pluginCell = $this->View->cell('TestPlugin.Dummy');
  87. $this->assertTrue(strpos("{$pluginCell}", 'dummy') !== false);
  88. }
  89. /**
  90. * Tests manual render() invocation.
  91. *
  92. * @return void
  93. */
  94. public function testCellManualRender() {
  95. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  96. $this->assertTrue(strpos($cell->render(), 'dummy message') !== false);
  97. $cell->teaserList();
  98. $this->assertTrue(strpos($cell->render('teaser_list'), '<h2>Lorem ipsum</h2>') !== false);
  99. }
  100. /**
  101. * Tests that using plugin's cells works.
  102. *
  103. * @return void
  104. */
  105. public function testPluginCell() {
  106. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  107. $this->assertTrue(strpos("{$cell}", 'hello world!') !== false);
  108. }
  109. /**
  110. * Tests that using an unexisting cell throws an exception.
  111. *
  112. * @expectedException \Cake\View\Error\MissingCellException
  113. * @return void
  114. */
  115. public function testUnexistingCell() {
  116. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  117. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  118. }
  119. }