HelperCollectionTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\HelperCollection;
  12. class HelperCollectionTest extends \PHPUnit_Framework_TestCase {
  13. public function testConstructor() {
  14. $foo = function() { echo 'foo'; };
  15. $bar = 'BAR';
  16. $helpers = new HelperCollection(array(
  17. 'foo' => $foo,
  18. 'bar' => $bar,
  19. ));
  20. $this->assertSame($foo, $helpers->get('foo'));
  21. $this->assertSame($bar, $helpers->get('bar'));
  22. }
  23. public function testAccessorsAndMutators() {
  24. $foo = function() { echo 'foo'; };
  25. $bar = 'BAR';
  26. $helpers = new HelperCollection;
  27. $this->assertTrue($helpers->isEmpty());
  28. $this->assertFalse($helpers->has('foo'));
  29. $this->assertFalse($helpers->has('bar'));
  30. $helpers->add('foo', $foo);
  31. $this->assertFalse($helpers->isEmpty());
  32. $this->assertTrue($helpers->has('foo'));
  33. $this->assertFalse($helpers->has('bar'));
  34. $helpers->add('bar', $bar);
  35. $this->assertFalse($helpers->isEmpty());
  36. $this->assertTrue($helpers->has('foo'));
  37. $this->assertTrue($helpers->has('bar'));
  38. $helpers->remove('foo');
  39. $this->assertFalse($helpers->isEmpty());
  40. $this->assertFalse($helpers->has('foo'));
  41. $this->assertTrue($helpers->has('bar'));
  42. }
  43. public function testMagicMethods() {
  44. $foo = function() { echo 'foo'; };
  45. $bar = 'BAR';
  46. $helpers = new HelperCollection;
  47. $this->assertTrue($helpers->isEmpty());
  48. $this->assertFalse($helpers->has('foo'));
  49. $this->assertFalse($helpers->has('bar'));
  50. $this->assertFalse(isset($helpers->foo));
  51. $this->assertFalse(isset($helpers->bar));
  52. $helpers->foo = $foo;
  53. $this->assertFalse($helpers->isEmpty());
  54. $this->assertTrue($helpers->has('foo'));
  55. $this->assertFalse($helpers->has('bar'));
  56. $this->assertTrue(isset($helpers->foo));
  57. $this->assertFalse(isset($helpers->bar));
  58. $helpers->bar = $bar;
  59. $this->assertFalse($helpers->isEmpty());
  60. $this->assertTrue($helpers->has('foo'));
  61. $this->assertTrue($helpers->has('bar'));
  62. $this->assertTrue(isset($helpers->foo));
  63. $this->assertTrue(isset($helpers->bar));
  64. unset($helpers->foo);
  65. $this->assertFalse($helpers->isEmpty());
  66. $this->assertFalse($helpers->has('foo'));
  67. $this->assertTrue($helpers->has('bar'));
  68. $this->assertFalse(isset($helpers->foo));
  69. $this->assertTrue(isset($helpers->bar));
  70. }
  71. /**
  72. * @dataProvider getInvalidHelperArguments
  73. */
  74. public function testHelperCollectionIsntAfraidToThrowExceptions($helpers = array(), $actions = array(), $exception = null) {
  75. if ($exception) {
  76. $this->setExpectedException($exception);
  77. }
  78. $helpers = new HelperCollection($helpers);
  79. foreach ($actions as $method => $args) {
  80. call_user_func_array(array($helpers, $method), $args);
  81. }
  82. }
  83. public function getInvalidHelperArguments() {
  84. return array(
  85. array(
  86. 'not helpers',
  87. array(),
  88. '\InvalidArgumentException',
  89. ),
  90. array(
  91. array(),
  92. array('get' => array('foo')),
  93. '\InvalidArgumentException',
  94. ),
  95. array(
  96. array('foo' => 'FOO'),
  97. array('get' => array('foo')),
  98. null,
  99. ),
  100. array(
  101. array('foo' => 'FOO'),
  102. array('get' => array('bar')),
  103. '\InvalidArgumentException',
  104. ),
  105. array(
  106. array('foo' => 'FOO'),
  107. array(
  108. 'add' => array('bar', 'BAR'),
  109. 'get' => array('bar'),
  110. ),
  111. null,
  112. ),
  113. array(
  114. array('foo' => 'FOO'),
  115. array(
  116. 'get' => array('foo'),
  117. 'remove' => array('foo'),
  118. ),
  119. null,
  120. ),
  121. array(
  122. array('foo' => 'FOO'),
  123. array(
  124. 'remove' => array('foo'),
  125. 'get' => array('foo'),
  126. ),
  127. '\InvalidArgumentException',
  128. ),
  129. array(
  130. array(),
  131. array('remove' => array('foo')),
  132. '\InvalidArgumentException',
  133. ),
  134. );
  135. }
  136. }