PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/templateTest.php

http://github.com/crodas/Haanga
PHP | 95 lines | 48 code | 8 blank | 39 comment | 2 complexity | cd45de9e4f9238a93a39991137d8d7b3 MD5 | raw file
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class Foo_Bar {
  4. static $Bar = 'haanga';
  5. static $Arr = array('foo', 'Bar' => 'Foo');
  6. protected $foo = 'foo';
  7. public $bar = 'bar';
  8. static function something()
  9. {
  10. return 'something';
  11. }
  12. function method() {
  13. return $this->foo;
  14. }
  15. function bar() {
  16. return "something else";
  17. }
  18. }
  19. /**
  20. * @runTestsInSeparateProcess
  21. */
  22. class templateTest extends TestCase
  23. {
  24. public function init($test_file, &$expected)
  25. {
  26. Haanga_Compiler::setOption('allow_exec', true);
  27. if ($test_file === '/assert_templates/strip_whitespace.tpl') {
  28. Haanga_Compiler::setOption('strip_whitespace', TRUE);
  29. $expected = rtrim($expected). ' '; /* weird output */
  30. } else {
  31. Haanga_Compiler::setOption('strip_whitespace', FALSE);
  32. }
  33. }
  34. /**
  35. * @dataProvider tplProvider
  36. */
  37. public function testRuntime($test_file, $data, $expected)
  38. {
  39. $this->init($test_file, $expected);
  40. $output = Haanga::Load($test_file, $data, TRUE);
  41. $this->assertEquals($output, $expected);
  42. $this->assertTrue(filemtime(__DIR__ . $test_file) <= filemtime(__DIR__ . '/tmp/assert_templates/' . basename($test_file) . '.php'));
  43. }
  44. /**
  45. * @dataProvider tplProvider
  46. */
  47. public function testIsCached($test_file, $data, $expected)
  48. {
  49. /* same as above, but we ensure that the file wasn't compiled */
  50. $this->init($test_file, $expected);
  51. $output = Haanga::Load($test_file, $data, TRUE);
  52. $this->assertEquals($output, $expected);
  53. $this->assertFalse(Haanga::$has_compiled);
  54. }
  55. public static function tplProvider()
  56. {
  57. $datas = array();
  58. foreach (glob(__DIR__ . "/assert_templates/*.tpl") as $test_file) {
  59. $data = array();
  60. $data_file = substr($test_file, 0, -3)."php";
  61. $expected = substr($test_file, 0, -3)."html";
  62. $test_file = substr($test_file, strlen(__DIR__));
  63. if (!is_file($expected)) {
  64. if (!is_file($expected.".php")) {
  65. continue;
  66. }
  67. $expected .= ".php";
  68. ob_start();
  69. require $expected;
  70. $expected = ob_get_clean();
  71. } else {
  72. $expected = file_get_contents($expected);
  73. }
  74. if (is_file($data_file)) {
  75. try {
  76. include $data_file;
  77. } Catch (Exception $e) {
  78. continue;
  79. }
  80. }
  81. $datas[] = array($test_file, $data, $expected);
  82. }
  83. return $datas;
  84. }
  85. }