CompilerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2010-2014 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\n"),
  99. array(
  100. Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
  101. Mustache_Tokenizer::NAME => 'name',
  102. ),
  103. array(
  104. Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
  105. Mustache_Tokenizer::NAME => '.',
  106. ),
  107. $this->createTextToken("'bar'"),
  108. ),
  109. 'Monkey',
  110. false,
  111. ENT_COMPAT,
  112. 'UTF-8',
  113. array(
  114. "\nclass Monkey extends Mustache_Template",
  115. "\$buffer .= \$indent . 'foo\n';",
  116. '$value = $this->resolveValue($context->find(\'name\'), $context, $indent);',
  117. '$buffer .= htmlspecialchars($value, '.ENT_COMPAT.', \'UTF-8\');',
  118. '$value = $this->resolveValue($context->last(), $context, $indent);',
  119. '$buffer .= \'\\\'bar\\\'\';',
  120. 'return $buffer;',
  121. )
  122. ),
  123. );
  124. }
  125. /**
  126. * @expectedException Mustache_Exception_SyntaxException
  127. */
  128. public function testCompilerThrowsSyntaxException()
  129. {
  130. $compiler = new Mustache_Compiler;
  131. $compiler->compile('', array(array(Mustache_Tokenizer::TYPE => 'invalid')), 'SomeClass');
  132. }
  133. /**
  134. * @param string $value
  135. */
  136. private function createTextToken($value)
  137. {
  138. return array(
  139. Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_TEXT,
  140. Mustache_Tokenizer::VALUE => $value,
  141. );
  142. }
  143. }