MustacheExceptionTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 testThrowsUnexpectedCloseSectionException() {
  30. $this->pickyMustache->render('{{/unopened}}');
  31. }
  32. /**
  33. * @group partials
  34. * @expectedException MustacheException
  35. */
  36. public function testThrowsUnknownPartialException() {
  37. $this->pickyMustache->render('{{>impartial}}');
  38. }
  39. /**
  40. * @group pragmas
  41. * @expectedException MustacheException
  42. */
  43. public function testThrowsUnknownPragmaException() {
  44. $this->pickyMustache->render('{{%SWEET-MUSTACHE-BRO}}');
  45. }
  46. /**
  47. * @group sections
  48. */
  49. public function testDoesntThrowUnclosedSectionException() {
  50. $this->assertEquals('', $this->slackerMustache->render('{{#unclosed}}'));
  51. }
  52. /**
  53. * @group sections
  54. */
  55. public function testDoesntThrowUnexpectedCloseSectionException() {
  56. $this->assertEquals('', $this->slackerMustache->render('{{/unopened}}'));
  57. }
  58. /**
  59. * @group partials
  60. */
  61. public function testDoesntThrowUnknownPartialException() {
  62. $this->assertEquals('', $this->slackerMustache->render('{{>impartial}}'));
  63. }
  64. /**
  65. * @group pragmas
  66. * @expectedException MustacheException
  67. */
  68. public function testGetPragmaOptionsThrowsExceptionsIfItThinksYouHaveAPragmaButItTurnsOutYouDont() {
  69. $mustache = new TestableMustache();
  70. $mustache->testableGetPragmaOptions('PRAGMATIC');
  71. }
  72. }
  73. class PickyMustache extends Mustache {
  74. protected $_throwsExceptions = array(
  75. MustacheException::UNKNOWN_VARIABLE => true,
  76. MustacheException::UNCLOSED_SECTION => true,
  77. MustacheException::UNEXPECTED_CLOSE_SECTION => true,
  78. MustacheException::UNKNOWN_PARTIAL => true,
  79. MustacheException::UNKNOWN_PRAGMA => true,
  80. );
  81. }
  82. class SlackerMustache extends Mustache {
  83. protected $_throwsExceptions = array(
  84. MustacheException::UNKNOWN_VARIABLE => false,
  85. MustacheException::UNCLOSED_SECTION => false,
  86. MustacheException::UNEXPECTED_CLOSE_SECTION => false,
  87. MustacheException::UNKNOWN_PARTIAL => false,
  88. MustacheException::UNKNOWN_PRAGMA => false,
  89. );
  90. }
  91. class TestableMustache extends Mustache {
  92. public function testableGetPragmaOptions($pragma_name) {
  93. return $this->_getPragmaOptions($pragma_name);
  94. }
  95. }