1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
<?php
class One
{
public $arg1, $arg2;
public function __construct($arg1 = null, $arg2 = null)
{
$this->arg1 = $arg1;
$this->arg2 = $arg2;
}
}
class Swift_DependencyContainerTest extends \PHPUnit_Framework_TestCase
{
private $_container;
public function setUp()
{
$this->_container = new Swift_DependencyContainer();
}
public function testRegisterAndLookupValue()
{
$this->_container->register('foo')->asValue('bar');
$this->assertEquals('bar', $this->_container->lookup('foo'));
}
public function testHasReturnsTrueForRegisteredValue()
{
$this->_container->register('foo')->asValue('bar');
$this->assertTrue($this->_container->has('foo'));
}
public function testHasReturnsFalseForUnregisteredValue()
{
$this->assertFalse($this->_container->has('foo'));
}
public function testRegisterAndLookupNewInstance()
{
$this->_container->register('one')->asNewInstanceOf('One');
$this->assertInstanceof('One', $this->_container->lookup('one'));
}
public function testHasReturnsTrueForRegisteredInstance()
{
$this->_container->register('one')->asNewInstanceOf('One');
$this->assertTrue($this->_container->has('one'));
}
public function testNewInstanceIsAlwaysNew()
{
$this->_container->register('one')->asNewInstanceOf('One');
$a = $this->_container->lookup('one');
$b = $this->_container->lookup('one');
$this->assertEquals($a, $b);
}
public function testRegisterAndLookupSharedInstance()
{
$this->_container->register('one')->asSharedInstanceOf('One');
$this->assertInstanceof('One', $this->_container->lookup('one'));
}
public function testHasReturnsTrueForSharedInstance()
{
$this->_container->register('one')->asSharedInstanceOf('One');
$this->assertTrue($this->_container->has('one'));
}
public function testMultipleSharedInstancesAreSameInstance()
{
$this->_container->register('one')->asSharedInstanceOf('One');
$a = $this->_container->lookup('one');
$b = $this->_container->lookup('one');
$this->assertEquals($a, $b);
}
public function testNewInstanceWithDependencies()
{
$this->_container->register('foo')->asValue('FOO');
$this->_container->register('one')->asNewInstanceOf('One')
->withDependencies(array('foo'));
$obj = $this->_container->lookup('one');
$this->assertSame('FOO', $obj->arg1);
}
public function testNewInstanceWithMultipleDependencies()
{
$this->_container->register('foo')->asValue('FOO');
$this->_container->register('bar')->asValue(42);
$this->_container->register('one')->asNewInstanceOf('One')
->withDependencies(array('foo', 'bar'));
$obj = $this->_container->lookup('one');
$this->assertSame('FOO', $obj->arg1);
$this->assertSame(42, $obj->arg2);
}
public function testNewInstanceWithInjectedObjects()
{
$this->_container->register('foo')->asValue('FOO');
$this->_container->register('one')->asNewInstanceOf('One');
$this->_container->register('two')->asNewInstanceOf('One')
->withDependencies(array('one', 'foo'));
$obj = $this->_container->lookup('two');
$this->assertEquals($this->_container->lookup('one'), $obj->arg1);
$this->assertSame('FOO', $obj->arg2);
}
public function testNewInstanceWithAddConstructorValue()
{
$this->_container->register('one')->asNewInstanceOf('One')
->addConstructorValue('x')
->addConstructorValue(99);
$obj = $this->_container->lookup('one');
$this->assertSame('x', $obj->arg1);
$this->assertSame(99, $obj->arg2);
}
public function testNewInstanceWithAddConstructorLookup()
{
$this->_container->register('foo')->asValue('FOO');
$this->_container->register('bar')->asValue(42);
$this->_container->register('one')->asNewInstanceOf('One')
->addConstructorLookup('foo')
->addConstructorLookup('bar');
$obj = $this->_container->lookup('one');
$this->assertSame('FOO', $obj->arg1);
$this->assertSame(42, $obj->arg2);
}
public function testResolvedDependenciesCanBeLookedUp()
{
$this->_container->register('foo')->asValue('FOO');
$this->_container->register('one')->asNewInstanceOf('One');
$this->_container->register('two')->asNewInstanceOf('One')
->withDependencies(array('one', 'foo'));
$deps = $this->_container->createDependenciesFor('two');
$this->assertEquals(
array($this->_container->lookup('one'), 'FOO'), $deps
);
}
public function testArrayOfDependenciesCanBeSpecified()
{
$this->_container->register('foo')->asValue('FOO');
$this->_container->register('one')->asNewInstanceOf('One');
$this->_container->register('two')->asNewInstanceOf('One')
->withDependencies(array(array('one', 'foo'), 'foo'));
$obj = $this->_container->lookup('two');
$this->assertEquals(array($this->_container->lookup('one'), 'FOO'), $obj->arg1);
$this->assertSame('FOO', $obj->arg2);
}
public function testAliasCanBeSet()
{
$this->_container->register('foo')->asValue('FOO');
$this->_container->register('bar')->asAliasOf('foo');
$this->assertSame('FOO', $this->_container->lookup('bar'));
}
public function testAliasOfAliasCanBeSet()
{
$this->_container->register('foo')->asValue('FOO');
$this->_container->register('bar')->asAliasOf('foo');
$this->_container->register('zip')->asAliasOf('bar');
$this->_container->register('button')->asAliasOf('zip');
$this->assertSame('FOO', $this->_container->lookup('button'));
}
}
|