MustacheExceptionTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * @expectedException MustacheException
  13. */
  14. public function testThrowsUnknownVariableException() {
  15. $this->pickyMustache->render('{{not_a_variable}}');
  16. }
  17. /**
  18. * @expectedException MustacheException
  19. */
  20. public function testThrowsUnclosedSectionException() {
  21. $this->pickyMustache->render('{{#unclosed}}');
  22. }
  23. /**
  24. * @expectedException MustacheException
  25. */
  26. public function testThrowsUnexpectedCloseSectionException() {
  27. $this->pickyMustache->render('{{/unopened}}');
  28. }
  29. /**
  30. * @expectedException MustacheException
  31. */
  32. public function testThrowsUnknownPartialException() {
  33. $this->pickyMustache->render('{{>impartial}}');
  34. }
  35. /**
  36. * @expectedException MustacheException
  37. */
  38. public function testThrowsUnknownPragmaException() {
  39. $this->pickyMustache->render('{{%SWEET-MUSTACHE-BRO}}');
  40. }
  41. public function testDoesntThrowUnclosedSectionException() {
  42. $this->assertEquals('', $this->slackerMustache->render('{{#unclosed}}'));
  43. }
  44. public function testDoesntThrowUnexpectedCloseSectionException() {
  45. $this->assertEquals('', $this->slackerMustache->render('{{/unopened}}'));
  46. }
  47. public function testDoesntThrowUnknownPartialException() {
  48. $this->assertEquals('', $this->slackerMustache->render('{{>impartial}}'));
  49. }
  50. /**
  51. * @expectedException MustacheException
  52. */
  53. public function testGetPragmaOptionsThrowsExceptionsIfItThinksYouHaveAPragmaButItTurnsOutYouDont() {
  54. $mustache = new TestableMustache();
  55. $mustache->testableGetPragmaOptions('PRAGMATIC');
  56. }
  57. }
  58. class PickyMustache extends Mustache {
  59. protected $_throwsExceptions = array(
  60. MustacheException::UNKNOWN_VARIABLE => true,
  61. MustacheException::UNCLOSED_SECTION => true,
  62. MustacheException::UNEXPECTED_CLOSE_SECTION => true,
  63. MustacheException::UNKNOWN_PARTIAL => true,
  64. MustacheException::UNKNOWN_PRAGMA => true,
  65. );
  66. }
  67. class SlackerMustache extends Mustache {
  68. protected $_throwsExceptions = array(
  69. MustacheException::UNKNOWN_VARIABLE => false,
  70. MustacheException::UNCLOSED_SECTION => false,
  71. MustacheException::UNEXPECTED_CLOSE_SECTION => false,
  72. MustacheException::UNKNOWN_PARTIAL => false,
  73. MustacheException::UNKNOWN_PRAGMA => false,
  74. );
  75. }
  76. class TestableMustache extends Mustache {
  77. public function testableGetPragmaOptions($pragma_name) {
  78. return $this->_getPragmaOptions($pragma_name);
  79. }
  80. }