MustacheExceptionTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. require_once '../Mustache.php';
  3. class MustacheExceptionTest extends PHPUnit_Framework_TestCase {
  4. const TEST_CLASS = 'Mustache';
  5. protected $pickyMustache;
  6. protected $slackerMustache;
  7. public function setUp() {
  8. $this->pickyMustache = new PickyMustache();
  9. $this->slackerMustache = new SlackerMustache();
  10. }
  11. /**
  12. * @group interpolation
  13. * @expectedException MustacheException
  14. */
  15. public function testThrowsUnknownVariableException() {
  16. $this->pickyMustache->render('{{not_a_variable}}');
  17. }
  18. /**
  19. * @group sections
  20. * @expectedException MustacheException
  21. */
  22. public function testThrowsUnclosedSectionException() {
  23. $this->pickyMustache->render('{{#unclosed}}');
  24. }
  25. /**
  26. * @group sections
  27. * @expectedException MustacheException
  28. */
  29. public function testThrowsUnclosedInvertedSectionException() {
  30. $this->pickyMustache->render('{{^unclosed}}');
  31. }
  32. /**
  33. * @group sections
  34. * @expectedException MustacheException
  35. */
  36. public function testThrowsUnexpectedCloseSectionException() {
  37. $this->pickyMustache->render('{{/unopened}}');
  38. }
  39. /**
  40. * @group partials
  41. * @expectedException MustacheException
  42. */
  43. public function testThrowsUnknownPartialException() {
  44. $this->pickyMustache->render('{{>impartial}}');
  45. }
  46. /**
  47. * @group pragmas
  48. * @expectedException MustacheException
  49. */
  50. public function testThrowsUnknownPragmaException() {
  51. $this->pickyMustache->render('{{%SWEET-MUSTACHE-BRO}}');
  52. }
  53. /**
  54. * @group sections
  55. */
  56. public function testDoesntThrowUnclosedSectionException() {
  57. $this->assertEquals('', $this->slackerMustache->render('{{#unclosed}}'));
  58. }
  59. /**
  60. * @group sections
  61. */
  62. public function testDoesntThrowUnexpectedCloseSectionException() {
  63. $this->assertEquals('', $this->slackerMustache->render('{{/unopened}}'));
  64. }
  65. /**
  66. * @group partials
  67. */
  68. public function testDoesntThrowUnknownPartialException() {
  69. $this->assertEquals('', $this->slackerMustache->render('{{>impartial}}'));
  70. }
  71. /**
  72. * @group pragmas
  73. * @expectedException MustacheException
  74. */
  75. public function testGetPragmaOptionsThrowsExceptionsIfItThinksYouHaveAPragmaButItTurnsOutYouDont() {
  76. $mustache = new TestableMustache();
  77. $mustache->testableGetPragmaOptions('PRAGMATIC');
  78. }
  79. public function testOverrideThrownExceptionsViaConstructorOptions() {
  80. $exceptions = array(
  81. MustacheException::UNKNOWN_VARIABLE,
  82. MustacheException::UNCLOSED_SECTION,
  83. MustacheException::UNEXPECTED_CLOSE_SECTION,
  84. MustacheException::UNKNOWN_PARTIAL,
  85. MustacheException::UNKNOWN_PRAGMA,
  86. );
  87. $one = new TestableMustache(null, null, null, array(
  88. 'throws_exceptions' => array_fill_keys($exceptions, true)
  89. ));
  90. $thrownExceptions = $one->getThrownExceptions();
  91. foreach ($exceptions as $exception) {
  92. $this->assertTrue($thrownExceptions[$exception]);
  93. }
  94. $two = new TestableMustache(null, null, null, array(
  95. 'throws_exceptions' => array_fill_keys($exceptions, false)
  96. ));
  97. $thrownExceptions = $two->getThrownExceptions();
  98. foreach ($exceptions as $exception) {
  99. $this->assertFalse($thrownExceptions[$exception]);
  100. }
  101. }
  102. }
  103. class PickyMustache extends Mustache {
  104. protected $_throwsExceptions = array(
  105. MustacheException::UNKNOWN_VARIABLE => true,
  106. MustacheException::UNCLOSED_SECTION => true,
  107. MustacheException::UNEXPECTED_CLOSE_SECTION => true,
  108. MustacheException::UNKNOWN_PARTIAL => true,
  109. MustacheException::UNKNOWN_PRAGMA => true,
  110. );
  111. }
  112. class SlackerMustache extends Mustache {
  113. protected $_throwsExceptions = array(
  114. MustacheException::UNKNOWN_VARIABLE => false,
  115. MustacheException::UNCLOSED_SECTION => false,
  116. MustacheException::UNEXPECTED_CLOSE_SECTION => false,
  117. MustacheException::UNKNOWN_PARTIAL => false,
  118. MustacheException::UNKNOWN_PRAGMA => false,
  119. );
  120. }
  121. class TestableMustache extends Mustache {
  122. public function testableGetPragmaOptions($pragma_name) {
  123. return $this->_getPragmaOptions($pragma_name);
  124. }
  125. public function getThrownExceptions() {
  126. return $this->_throwsExceptions;
  127. }
  128. }