BakeViewTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Network\Request;
  17. use Cake\Network\Response;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\View\BakeView;
  20. /**
  21. * BakeViewTest class
  22. *
  23. */
  24. class BakeViewTest extends TestCase {
  25. /**
  26. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. $request = new Request();
  33. $response = new Response();
  34. $this->View = new BakeView($request, $response);
  35. }
  36. /**
  37. * tearDown method
  38. *
  39. * @return void
  40. */
  41. public function tearDown() {
  42. parent::tearDown();
  43. unset($this->View);
  44. }
  45. /**
  46. * test rendering a template file
  47. *
  48. * @return void
  49. */
  50. public function testRenderTemplate() {
  51. $this->View->set(['aVariable' => 123]);
  52. $result = $this->View->render('view_tests/simple');
  53. $expected = "The value of aVariable is: 123.\n";
  54. $this->assertSame($expected, $result, 'variables in erb-style tags should be evaluated');
  55. }
  56. /**
  57. * verify that php tags are ignored
  58. *
  59. * @return void
  60. */
  61. public function testRenderIgnorePhpTags() {
  62. $this->View->set(['aVariable' => 123]);
  63. $result = $this->View->render('view_tests/simple_php');
  64. $expected = "The value of aVariable is: 123. Not <?php echo \$aVariable ?>.\n";
  65. $this->assertSame($expected, $result, 'variables in php tags should be treated as strings');
  66. }
  67. /**
  68. * verify that short php tags are ignored
  69. *
  70. * @return void
  71. */
  72. public function testRenderIgnorePhpShortTags() {
  73. $this->View->set(['aVariable' => 123]);
  74. $result = $this->View->render('view_tests/simple_php_short_tags');
  75. $expected = "The value of aVariable is: 123. Not <?= \$aVariable ?>.\n";
  76. $this->assertSame($expected, $result, 'variables in php tags should be treated as strings');
  77. }
  78. /**
  79. * Newlines after template tags should act predictably
  80. *
  81. * @return void
  82. */
  83. public function testRenderNewlines() {
  84. $result = $this->View->render('view_tests/newlines');
  85. $expected = "There should be a newline about here: \n";
  86. $expected .= "And this should be on the next line.\n";
  87. $expected .= "\n";
  88. $expected .= "There should be no new line after this";
  89. $this->assertSame(
  90. $expected,
  91. $result,
  92. 'Tags at the end of a line should not swallow new lines when rendered'
  93. );
  94. }
  95. /**
  96. * Verify that template tags with leading whitespace don't leave a mess
  97. *
  98. * @return void
  99. */
  100. public function testSwallowLeadingWhitespace() {
  101. $result = $this->View->render('view_tests/leading_whitespace');
  102. $expected = $this->_getCompareTemplate('leading_whitespace');
  103. $this->assertSame(
  104. $expected,
  105. $result,
  106. 'Leading whitespace in bake templates should not result in leading/loose whitespace in rendered results'
  107. );
  108. }
  109. /**
  110. * _getCompareTemplate
  111. *
  112. * @param string $template
  113. * @return string
  114. */
  115. protected function _getCompareTemplate($template) {
  116. return file_get_contents(dirname(dirname(__DIR__)) . "/test_app/TestApp/Template/Bake/view_tests_compare/$template.ctp");
  117. }
  118. }