TestData
2 years ago
BasicTest.php
5 years ago
CallTest.php
5 years ago
ChainTest.php
5 years ago
ConstructParamsTest.php
5 years ago
CreateArgsTest.php
5 years ago
DiceTest.php
5 years ago
NamedInstancesTest.php
5 years ago
NamespaceTest.php
5 years ago
ShareInstancesTest.php
2 years ago
SubstitutionsTest.php
5 years ago
bootstrap.php
5 years ago
NamespaceTest.php
67 lines
| 1 | <?php |
| 2 | /* @description Dice - A minimal Dependency Injection Container for PHP * |
| 3 | * @author Tom Butler tom@r.je * |
| 4 | * @copyright 2012-2018 Tom Butler <tom@r.je> | https:// r.je/dice.html * |
| 5 | * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * |
| 6 | * @version 3.0 */ |
| 7 | class NamespaceTest extends DiceTest { |
| 8 | public function testNamespaceBasic() { |
| 9 | $a = $this->dice->create('Foo\\A'); |
| 10 | $this->assertInstanceOf('Foo\\A', $a); |
| 11 | } |
| 12 | |
| 13 | |
| 14 | public function testNamespaceWithSlash() { |
| 15 | $a = $this->dice->create('\\Foo\\A'); |
| 16 | $this->assertInstanceOf('\\Foo\\A', $a); |
| 17 | } |
| 18 | |
| 19 | public function testNamespaceWithSlashrule() { |
| 20 | $rule = []; |
| 21 | $rule['substitutions']['Foo\\A'] = [\Dice\Dice::INSTANCE => 'Foo\\ExtendedA']; |
| 22 | $dice = $this->dice->addRule('\\Foo\\B', $rule); |
| 23 | |
| 24 | $b = $dice->create('\\Foo\\B'); |
| 25 | $this->assertInstanceOf('Foo\\ExtendedA', $b->a); |
| 26 | } |
| 27 | |
| 28 | public function testNamespaceWithSlashruleInstance() { |
| 29 | $rule = []; |
| 30 | $rule['substitutions']['Foo\\A'] = [\Dice\Dice::INSTANCE => 'Foo\\ExtendedA']; |
| 31 | $dice = $this->dice->addRule('\\Foo\\B', $rule); |
| 32 | |
| 33 | $b = $dice->create('\\Foo\\B'); |
| 34 | $this->assertInstanceOf('Foo\\ExtendedA', $b->a); |
| 35 | } |
| 36 | |
| 37 | public function testNamespaceTypeHint() { |
| 38 | $rule = []; |
| 39 | $rule['shared'] = true; |
| 40 | $dice = $this->dice->addRule('Bar\\A', $rule); |
| 41 | |
| 42 | $c = $dice->create('Foo\\C'); |
| 43 | $this->assertInstanceOf('Bar\\A', $c->a); |
| 44 | |
| 45 | $c2 = $dice->create('Foo\\C'); |
| 46 | $this->assertNotSame($c, $c2); |
| 47 | |
| 48 | //Check the rule has been correctly recognised for type hinted classes in a different namespace PrestoPlayer\$this->assertSame($c2->a, $c->a); |
| 49 | } |
| 50 | |
| 51 | public function testNamespaceInjection() { |
| 52 | $b = $this->dice->create('Foo\\B'); |
| 53 | $this->assertInstanceOf('Foo\\B', $b); |
| 54 | $this->assertInstanceOf('Foo\\A', $b->a); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | public function testNamespaceRuleSubstitution() { |
| 59 | $rule = []; |
| 60 | $rule['substitutions']['Foo\\A'] = [\Dice\Dice::INSTANCE => 'Foo\\ExtendedA']; |
| 61 | $dice = $this->dice->addRule('Foo\\B', $rule); |
| 62 | |
| 63 | $b = $dice->create('Foo\\B'); |
| 64 | $this->assertInstanceOf('Foo\\ExtendedA', $b->a); |
| 65 | } |
| 66 | |
| 67 | } |