| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- /*
- * This file is part of Mustache.php.
- *
- * (c) 2012 Justin Hileman
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Mustache\Test;
- use Mustache\HelperCollection;
- class HelperCollectionTest extends \PHPUnit_Framework_TestCase {
- public function testConstructor() {
- $foo = function() { echo 'foo'; };
- $bar = 'BAR';
- $helpers = new HelperCollection(array(
- 'foo' => $foo,
- 'bar' => $bar,
- ));
- $this->assertSame($foo, $helpers->get('foo'));
- $this->assertSame($bar, $helpers->get('bar'));
- }
- public function testAccessorsAndMutators() {
- $foo = function() { echo 'foo'; };
- $bar = 'BAR';
- $helpers = new HelperCollection;
- $this->assertTrue($helpers->isEmpty());
- $this->assertFalse($helpers->has('foo'));
- $this->assertFalse($helpers->has('bar'));
- $helpers->add('foo', $foo);
- $this->assertFalse($helpers->isEmpty());
- $this->assertTrue($helpers->has('foo'));
- $this->assertFalse($helpers->has('bar'));
- $helpers->add('bar', $bar);
- $this->assertFalse($helpers->isEmpty());
- $this->assertTrue($helpers->has('foo'));
- $this->assertTrue($helpers->has('bar'));
- $helpers->remove('foo');
- $this->assertFalse($helpers->isEmpty());
- $this->assertFalse($helpers->has('foo'));
- $this->assertTrue($helpers->has('bar'));
- }
- public function testMagicMethods() {
- $foo = function() { echo 'foo'; };
- $bar = 'BAR';
- $helpers = new HelperCollection;
- $this->assertTrue($helpers->isEmpty());
- $this->assertFalse($helpers->has('foo'));
- $this->assertFalse($helpers->has('bar'));
- $this->assertFalse(isset($helpers->foo));
- $this->assertFalse(isset($helpers->bar));
- $helpers->foo = $foo;
- $this->assertFalse($helpers->isEmpty());
- $this->assertTrue($helpers->has('foo'));
- $this->assertFalse($helpers->has('bar'));
- $this->assertTrue(isset($helpers->foo));
- $this->assertFalse(isset($helpers->bar));
- $helpers->bar = $bar;
- $this->assertFalse($helpers->isEmpty());
- $this->assertTrue($helpers->has('foo'));
- $this->assertTrue($helpers->has('bar'));
- $this->assertTrue(isset($helpers->foo));
- $this->assertTrue(isset($helpers->bar));
- unset($helpers->foo);
- $this->assertFalse($helpers->isEmpty());
- $this->assertFalse($helpers->has('foo'));
- $this->assertTrue($helpers->has('bar'));
- $this->assertFalse(isset($helpers->foo));
- $this->assertTrue(isset($helpers->bar));
- }
- /**
- * @dataProvider getInvalidHelperArguments
- */
- public function testHelperCollectionIsntAfraidToThrowExceptions($helpers = array(), $actions = array(), $exception = null) {
- if ($exception) {
- $this->setExpectedException($exception);
- }
- $helpers = new HelperCollection($helpers);
- foreach ($actions as $method => $args) {
- call_user_func_array(array($helpers, $method), $args);
- }
- }
- public function getInvalidHelperArguments() {
- return array(
- array(
- 'not helpers',
- array(),
- '\InvalidArgumentException',
- ),
- array(
- array(),
- array('get' => array('foo')),
- '\InvalidArgumentException',
- ),
- array(
- array('foo' => 'FOO'),
- array('get' => array('foo')),
- null,
- ),
- array(
- array('foo' => 'FOO'),
- array('get' => array('bar')),
- '\InvalidArgumentException',
- ),
- array(
- array('foo' => 'FOO'),
- array(
- 'add' => array('bar', 'BAR'),
- 'get' => array('bar'),
- ),
- null,
- ),
- array(
- array('foo' => 'FOO'),
- array(
- 'get' => array('foo'),
- 'remove' => array('foo'),
- ),
- null,
- ),
- array(
- array('foo' => 'FOO'),
- array(
- 'remove' => array('foo'),
- 'get' => array('foo'),
- ),
- '\InvalidArgumentException',
- ),
- array(
- array(),
- array('remove' => array('foo')),
- '\InvalidArgumentException',
- ),
- );
- }
- }
|