MustacheExceptionTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. }
  80. class PickyMustache extends Mustache {
  81. protected $_throwsExceptions = array(
  82. MustacheException::UNKNOWN_VARIABLE => true,
  83. MustacheException::UNCLOSED_SECTION => true,
  84. MustacheException::UNEXPECTED_CLOSE_SECTION => true,
  85. MustacheException::UNKNOWN_PARTIAL => true,
  86. MustacheException::UNKNOWN_PRAGMA => true,
  87. );
  88. }
  89. class SlackerMustache extends Mustache {
  90. protected $_throwsExceptions = array(
  91. MustacheException::UNKNOWN_VARIABLE => false,
  92. MustacheException::UNCLOSED_SECTION => false,
  93. MustacheException::UNEXPECTED_CLOSE_SECTION => false,
  94. MustacheException::UNKNOWN_PARTIAL => false,
  95. MustacheException::UNKNOWN_PRAGMA => false,
  96. );
  97. }
  98. class TestableMustache extends Mustache {
  99. public function testableGetPragmaOptions($pragma_name) {
  100. return $this->_getPragmaOptions($pragma_name);
  101. }
  102. }