CompilerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2012 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * @group unit
  12. */
  13. class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @dataProvider getCompileValues
  17. */
  18. public function testCompile($source, array $tree, $name, $customEscaper, $entityFlags, $charset, $expected)
  19. {
  20. $compiler = new Mustache_Compiler;
  21. $compiled = $compiler->compile($source, $tree, $name, $customEscaper, $charset, false, $entityFlags);
  22. foreach ($expected as $contains) {
  23. $this->assertContains($contains, $compiled);
  24. }
  25. }
  26. public function getCompileValues()
  27. {
  28. return array(
  29. array('', array(), 'Banana', false, ENT_COMPAT, 'ISO-8859-1', array(
  30. "\nclass Banana extends Mustache_Template",
  31. 'return $buffer;',
  32. )),
  33. array('', array($this->createTextToken('TEXT')), 'Monkey', false, ENT_COMPAT, 'UTF-8', array(
  34. "\nclass Monkey extends Mustache_Template",
  35. '$buffer .= $indent . \'TEXT\';',
  36. 'return $buffer;',
  37. )),
  38. array(
  39. '',
  40. array(
  41. array(
  42. Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
  43. Mustache_Tokenizer::NAME => 'name',
  44. )
  45. ),
  46. 'Monkey',
  47. true,
  48. ENT_COMPAT,
  49. 'ISO-8859-1',
  50. array(
  51. "\nclass Monkey extends Mustache_Template",
  52. '$value = $this->resolveValue($context->find(\'name\'), $context, $indent);',
  53. '$buffer .= $indent . call_user_func($this->mustache->getEscape(), $value);',
  54. 'return $buffer;',
  55. )
  56. ),
  57. array(
  58. '',
  59. array(
  60. array(
  61. Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
  62. Mustache_Tokenizer::NAME => 'name',
  63. )
  64. ),
  65. 'Monkey',
  66. false,
  67. ENT_COMPAT,
  68. 'ISO-8859-1',
  69. array(
  70. "\nclass Monkey extends Mustache_Template",
  71. '$value = $this->resolveValue($context->find(\'name\'), $context, $indent);',
  72. '$buffer .= $indent . htmlspecialchars($value, '.ENT_COMPAT.', \'ISO-8859-1\');',
  73. 'return $buffer;',
  74. )
  75. ),
  76. array(
  77. '',
  78. array(
  79. array(
  80. Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
  81. Mustache_Tokenizer::NAME => 'name',
  82. )
  83. ),
  84. 'Monkey',
  85. false,
  86. ENT_QUOTES,
  87. 'ISO-8859-1',
  88. array(
  89. "\nclass Monkey extends Mustache_Template",
  90. '$value = $this->resolveValue($context->find(\'name\'), $context, $indent);',
  91. '$buffer .= $indent . htmlspecialchars($value, '.ENT_QUOTES.', \'ISO-8859-1\');',
  92. 'return $buffer;',
  93. )
  94. ),
  95. array(
  96. '',
  97. array(
  98. $this->createTextToken('foo'),
  99. $this->createTextToken("\n"),
  100. array(
  101. Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
  102. Mustache_Tokenizer::NAME => 'name',
  103. ),
  104. array(
  105. Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
  106. Mustache_Tokenizer::NAME => '.',
  107. ),
  108. $this->createTextToken("'bar'"),
  109. ),
  110. 'Monkey',
  111. false,
  112. ENT_COMPAT,
  113. 'UTF-8',
  114. array(
  115. "\nclass Monkey extends Mustache_Template",
  116. '$buffer .= $indent . \'foo\'',
  117. '$buffer .= "\n"',
  118. '$value = $this->resolveValue($context->find(\'name\'), $context, $indent);',
  119. '$buffer .= htmlspecialchars($value, '.ENT_COMPAT.', \'UTF-8\');',
  120. '$value = $this->resolveValue($context->last(), $context, $indent);',
  121. '$buffer .= \'\\\'bar\\\'\';',
  122. 'return $buffer;',
  123. )
  124. ),
  125. );
  126. }
  127. /**
  128. * @expectedException Mustache_Exception_SyntaxException
  129. */
  130. public function testCompilerThrowsSyntaxException()
  131. {
  132. $compiler = new Mustache_Compiler;
  133. $compiler->compile('', array(array(Mustache_Tokenizer::TYPE => 'invalid')), 'SomeClass');
  134. }
  135. private function createTextToken($value)
  136. {
  137. return array(
  138. Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_TEXT,
  139. Mustache_Tokenizer::VALUE => $value,
  140. );
  141. }
  142. }