HelperCollectionTest.php 4.7 KB

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