CompilerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\template\view;
  9. use lithium\core\Libraries;
  10. use lithium\template\view\Compiler;
  11. class CompilerTest extends \lithium\test\Unit {
  12. protected $_path;
  13. protected $_file = 'template.html.php';
  14. public function skip() {
  15. $path = realpath(Libraries::get(true, 'resources') . '/tmp/tests');
  16. $this->skipIf(!is_writable($path), "Path `{$path}` is not writable.");
  17. $path = realpath(Libraries::get(true, 'resources') . '/tmp/cache/templates');
  18. $this->skipIf(!is_writable($path), "Path `{$path}` is not writable.");
  19. }
  20. public function setUp() {
  21. $this->_path = realpath(
  22. str_replace('\\', '/', Libraries::get(true, 'resources')) . '/tmp/tests'
  23. );
  24. file_put_contents("{$this->_path}/{$this->_file}", "
  25. <?php echo 'this is unescaped content'; ?" . ">
  26. <?='this is escaped content'; ?" . ">
  27. <?=\$alsoEscaped; ?" . ">
  28. <?=\$this->escape('this is also escaped content'); ?" . ">
  29. <?=\$this->escape(
  30. 'this, too, is escaped content'
  31. ); ?" . ">
  32. <?='This is
  33. escaped content
  34. that breaks over
  35. several lines
  36. '; ?" . ">
  37. <?=\$h('This is pre-escaped content'); ?>
  38. ");
  39. }
  40. public function tearDown() {
  41. $path = realpath(Libraries::get(true, 'resources') . '/tmp/cache/templates');
  42. foreach (glob("{$path}/*.php") as $file) {
  43. unlink($file);
  44. }
  45. unlink("{$this->_path}/{$this->_file}");
  46. }
  47. public function testTemplateContentRewriting() {
  48. $template = Compiler::template("{$this->_path}/{$this->_file}");
  49. $this->assertTrue(file_exists($template));
  50. $expected = array(
  51. "<?php echo 'this is unescaped content'; ?" . ">",
  52. "<?php echo \$h('this is escaped content'); ?" . ">",
  53. "<?php echo \$h(\$alsoEscaped); ?" . ">",
  54. "<?php echo \$this->escape('this is also escaped content'); ?" . ">",
  55. '<?php echo $this->escape(',
  56. "'this, too, is escaped content'",
  57. '); ?>',
  58. "<?php echo \$h('This is",
  59. 'escaped content',
  60. 'that breaks over',
  61. 'several lines',
  62. "'); ?>",
  63. "<?php echo \$h('This is pre-escaped content'); ?>"
  64. );
  65. $result = array_map('trim', explode("\n", trim(file_get_contents($template))));
  66. $this->assertEqual($expected, $result);
  67. }
  68. public function testFallbackWithNonWritableDirectory() {
  69. $this->expectException('/failed to open stream/');
  70. $result = Compiler::template("{$this->_path}/{$this->_file}", array(
  71. 'path' => LITHIUM_APP_PATH . '/foo',
  72. 'fallback' => true
  73. ));
  74. $this->assertEqual("{$this->_path}/{$this->_file}", $result);
  75. $this->expectException('/Could not write compiled template/');
  76. $this->expectException('/failed to open stream/');
  77. $result = Compiler::template("{$this->_path}/{$this->_file}", array(
  78. 'path' => LITHIUM_APP_PATH . '/foo',
  79. 'fallback' => false
  80. ));
  81. }
  82. public function testTemplateCacheHit() {
  83. $path = Libraries::get(true, 'resources') . '/tmp/cache/templates';
  84. $original = Compiler::template("{$this->_path}/{$this->_file}", compact('path'));
  85. $cache = glob("{$path}/*");
  86. clearstatcache();
  87. $cached = Compiler::template("{$this->_path}/{$this->_file}", compact('path'));
  88. $this->assertEqual($original, $cached);
  89. $this->assertEqual($cache, glob("{$path}/*"));
  90. file_put_contents("{$this->_path}/{$this->_file}", "Updated");
  91. clearstatcache();
  92. $updated = Compiler::template("{$this->_path}/{$this->_file}", compact('path'));
  93. $newCache = glob("{$path}/*");
  94. $this->assertNotEqual($cache, $updated);
  95. $this->assertEqual(count($cache), count($newCache));
  96. $this->assertNotEqual($cache, $newCache);
  97. }
  98. }
  99. ?>