PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/lithium/0.9.9/libraries/lithium/tests/cases/template/view/CompilerTest.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 130 lines | 113 code | 11 blank | 6 comment | 0 complexity | 7391b69b4dae8b1a7bb0e6350925080a MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, 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\template\view\Compiler;
  10. class CompilerTest extends \lithium\test\Unit {
  11. protected $_path;
  12. protected $_file = 'resources/tmp/tests/template.html.php';
  13. public function skip() {
  14. $path = LITHIUM_APP_PATH . '/resources/tmp/tests';
  15. $this->skipIf(!is_writable($path), "{$path} is not writable.");
  16. }
  17. public function setUp() {
  18. $this->_path = str_replace('\\', '/', LITHIUM_APP_PATH);
  19. file_put_contents("{$this->_path}/{$this->_file}", "
  20. <?php echo 'this is unescaped content'; ?" . ">
  21. <?='this is escaped content'; ?" . ">
  22. <?=\$alsoEscaped; ?" . ">
  23. <?=\$this->escape('this is also escaped content'); ?" . ">
  24. <?=\$this->escape(
  25. 'this, too, is escaped content'
  26. ); ?" . ">
  27. <?='This is
  28. escaped content
  29. that breaks over
  30. several lines
  31. '; ?" . ">
  32. ");
  33. }
  34. public function tearDown() {
  35. foreach (glob("{$this->_path}/resources/tmp/cache/templates/*.php") as $file) {
  36. unlink($file);
  37. }
  38. unlink("{$this->_path}/{$this->_file}");
  39. }
  40. public function testTemplateContentRewriting() {
  41. $template = Compiler::template("{$this->_path}/{$this->_file}");
  42. $this->assertTrue(file_exists($template));
  43. $result = array_map('trim', explode("\n", trim(file_get_contents($template))));
  44. $expected = "<?php echo 'this is unescaped content'; ?" . ">";
  45. $this->assertEqual($expected, $result[0]);
  46. $expected = "<?php echo \$h('this is escaped content'); ?" . ">";
  47. $this->assertEqual($expected, $result[1]);
  48. $expected = "<?php echo \$h(\$alsoEscaped); ?" . ">";
  49. $this->assertEqual($expected, $result[2]);
  50. $expected = "<?php echo \$this->escape('this is also escaped content'); ?" . ">";
  51. $this->assertEqual($expected, $result[3]);
  52. $expected = '<?php echo $this->escape(';
  53. $this->assertEqual($expected, $result[4]);
  54. $expected = "'this, too, is escaped content'";
  55. $this->assertEqual($expected, $result[5]);
  56. $expected = '); ?>';
  57. $this->assertEqual($expected, $result[6]);
  58. $expected = "<?php echo \$h('This is";
  59. $this->assertEqual($expected, $result[7]);
  60. $expected = 'escaped content';
  61. $this->assertEqual($expected, $result[8]);
  62. $expected = 'that breaks over';
  63. $this->assertEqual($expected, $result[9]);
  64. $expected = 'several lines';
  65. $this->assertEqual($expected, $result[10]);
  66. $expected = "'); ?>";
  67. $this->assertEqual($expected, $result[11]);
  68. }
  69. public function testFallbackWithNonWritableDirectory() {
  70. $this->expectException('/failed to open stream/');
  71. $result = Compiler::template("{$this->_path}/{$this->_file}", array(
  72. 'path' => LITHIUM_APP_PATH . '/foo',
  73. 'fallback' => true
  74. ));
  75. $this->assertEqual("{$this->_path}/{$this->_file}", $result);
  76. $this->expectException('/Could not write compiled template/');
  77. $this->expectException('/failed to open stream/');
  78. $result = Compiler::template("{$this->_path}/{$this->_file}", array(
  79. 'path' => LITHIUM_APP_PATH . '/foo',
  80. 'fallback' => false
  81. ));
  82. }
  83. public function testTemplateCacheHit() {
  84. $path = LITHIUM_APP_PATH . '/resources/tmp/cache/templates';
  85. $original = Compiler::template("{$this->_path}/{$this->_file}", compact('path'));
  86. $cache = glob("{$path}/*");
  87. clearstatcache();
  88. $cached = Compiler::template("{$this->_path}/{$this->_file}", compact('path'));
  89. $this->assertEqual($original, $cached);
  90. $this->assertEqual($cache, glob("{$path}/*"));
  91. file_put_contents("{$this->_path}/{$this->_file}", "Updated");
  92. clearstatcache();
  93. $updated = Compiler::template("{$this->_path}/{$this->_file}", compact('path'));
  94. $newCache = glob("{$path}/*");
  95. $this->assertNotEqual($cache, $updated);
  96. $this->assertEqual(count($cache), count($newCache));
  97. $this->assertNotEqual($cache, $newCache);
  98. }
  99. }
  100. ?>