CompilerTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. namespace Mustache\Test;
  11. use Mustache\Compiler;
  12. use Mustache\Tokenizer;
  13. /**
  14. * @group unit
  15. */
  16. class CompilerTest extends \PHPUnit_Framework_TestCase {
  17. /**
  18. * @dataProvider getCompileValues
  19. */
  20. public function testCompile($source, array $tree, $name, $expected) {
  21. $compiler = new Compiler;
  22. $compiled = $compiler->compile($source, $tree, $name);
  23. foreach ($expected as $contains) {
  24. $this->assertContains($contains, $compiled);
  25. }
  26. }
  27. public function getCompileValues() {
  28. return array(
  29. array('', array(), 'Banana', array(
  30. "\nclass Banana extends \Mustache\Template",
  31. '$buffer->flush();',
  32. )),
  33. array('', array('TEXT'), 'Monkey', array(
  34. "\nclass Monkey extends \Mustache\Template",
  35. '$buffer->writeText(\'TEXT\');',
  36. '$buffer->flush();',
  37. )),
  38. array(
  39. '',
  40. array(
  41. 'foo',
  42. "\n",
  43. array(
  44. Tokenizer::TAG => '_v',
  45. Tokenizer::NAME => 'name',
  46. ),
  47. array(
  48. Tokenizer::TAG => '_v',
  49. Tokenizer::NAME => '.',
  50. ),
  51. "'bar'",
  52. ),
  53. 'Monkey',
  54. array(
  55. "\nclass Monkey extends \Mustache\Template",
  56. '$buffer->writeText(\'foo\');',
  57. '$buffer->writeLine();',
  58. '$value = $context->find(\'name\');',
  59. '$buffer->writeText($value, true);',
  60. '$value = $context->last();',
  61. '$buffer->writeText(\'\\\'bar\\\'\');',
  62. '$buffer->flush();',
  63. )
  64. ),
  65. );
  66. }
  67. /**
  68. * @expectedException InvalidArgumentException
  69. */
  70. public function testCompilerThrowsUnknownNodeTypeException() {
  71. $compiler = new Compiler;
  72. $compiler->compile('', array(array(Tokenizer::TAG => 'invalid')), 'SomeClass');
  73. }
  74. }