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
BasicTest.php
242 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 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | class BasicTest extends DiceTest { |
| 12 | |
| 13 | public function testCreate() { |
| 14 | $this->getMockBuilder('TestCreate')->getMock(); |
| 15 | $myobj = $this->dice->create('TestCreate'); |
| 16 | $this->assertInstanceOf('TestCreate', $myobj); |
| 17 | } |
| 18 | |
| 19 | |
| 20 | |
| 21 | public function testCreateInvalid() { |
| 22 | //"can't expect default exception". Not sure why. |
| 23 | $this->expectException('ErrorException'); |
| 24 | try { |
| 25 | $this->dice->create('SomeClassThatDoesNotExist'); |
| 26 | } |
| 27 | catch (Exception $e) { |
| 28 | throw new ErrorException('Error occurred'); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public function testNoConstructor() { |
| 33 | $a = $this->dice->create('NoConstructor'); |
| 34 | $this->assertInstanceOf('NoConstructor', $a); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | public function testSetDefaultRule() { |
| 39 | $defaultBehaviour = []; |
| 40 | $defaultBehaviour['shared'] = true; |
| 41 | $dice = $this->dice->addRule('*', $defaultBehaviour); |
| 42 | |
| 43 | $rule = $dice->getRule('*'); |
| 44 | foreach ($defaultBehaviour as $name => $value) { |
| 45 | $this->assertEquals($rule[$name], $defaultBehaviour[$name]); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | |
| 50 | public function testDefaultRuleWorks() { |
| 51 | $defaultBehaviour = []; |
| 52 | $defaultBehaviour['shared'] = true; |
| 53 | |
| 54 | $dice = $this->dice->addRule('*', $defaultBehaviour); |
| 55 | |
| 56 | $rule = $dice->getRule('A'); |
| 57 | |
| 58 | $this->assertTrue($rule['shared']); |
| 59 | |
| 60 | $a1 = $dice->create('A'); |
| 61 | $a2 = $dice->create('A'); |
| 62 | |
| 63 | $this->assertSame($a1, $a2); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /* |
| 68 | * Object graph creation cannot be tested with mocks because the constructor need to be tested. |
| 69 | * You can't set 'expects' on the objects which are created making them redundant for that as well |
| 70 | * Need real classes to test with unfortunately. |
| 71 | */ |
| 72 | public function testObjectGraphCreation() { |
| 73 | $a = $this->dice->create('A'); |
| 74 | $this->assertInstanceOf('B', $a->b); |
| 75 | $this->assertInstanceOf('c', $a->b->c); |
| 76 | $this->assertInstanceOf('D', $a->b->c->d); |
| 77 | $this->assertInstanceOf('E', $a->b->c->e); |
| 78 | $this->assertInstanceOf('F', $a->b->c->e->f); |
| 79 | } |
| 80 | |
| 81 | public function testSharedNamed() { |
| 82 | $rule = []; |
| 83 | $rule['shared'] = true; |
| 84 | $rule['instanceOf'] = 'A'; |
| 85 | |
| 86 | $dice = $this->dice->addRule('[A]', $rule); |
| 87 | |
| 88 | $a1 = $dice->create('[A]'); |
| 89 | $a2 = $dice->create('[A]'); |
| 90 | $this->assertSame($a1, $a2); |
| 91 | } |
| 92 | |
| 93 | public function testSharedRule() { |
| 94 | $shared = []; |
| 95 | $shared['shared'] = true; |
| 96 | |
| 97 | $dice = $this->dice->addRule('MyObj', $shared); |
| 98 | |
| 99 | $obj = $dice->create('MyObj'); |
| 100 | $this->assertInstanceOf('MyObj', $obj); |
| 101 | |
| 102 | $obj2 = $dice->create('MyObj'); |
| 103 | $this->assertInstanceOf('MyObj', $obj2); |
| 104 | |
| 105 | $this->assertSame($obj, $obj2); |
| 106 | |
| 107 | |
| 108 | //This check isn't strictly needed but it's nice to have that safety measure! |
| 109 | $obj->setFoo('bar'); |
| 110 | $this->assertEquals($obj->getFoo(), $obj2->getFoo()); |
| 111 | $this->assertEquals($obj->getFoo(), 'bar'); |
| 112 | $this->assertEquals($obj2->getFoo(), 'bar'); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | public function testInterfaceRule() { |
| 117 | $rule = []; |
| 118 | |
| 119 | $rule['shared'] = true; |
| 120 | $dice = $this->dice->addRule('interfaceTest', $rule); |
| 121 | |
| 122 | $one = $dice->create('InterfaceTestClass'); |
| 123 | $two = $dice->create('InterfaceTestClass'); |
| 124 | |
| 125 | $this->assertSame($one, $two); |
| 126 | } |
| 127 | |
| 128 | public function testCyclicReferences() { |
| 129 | $rule = []; |
| 130 | $rule['shared'] = true; |
| 131 | |
| 132 | $dice = $this->dice->addRule('CyclicB', $rule); |
| 133 | |
| 134 | $a = $dice->create('CyclicA'); |
| 135 | |
| 136 | $this->assertInstanceOf('CyclicB', $a->b); |
| 137 | $this->assertInstanceOf('CyclicA', $a->b->a); |
| 138 | |
| 139 | $this->assertSame($a->b, $a->b->a->b); |
| 140 | } |
| 141 | |
| 142 | public function testInherit() { |
| 143 | $rule = ['shared' => true, 'inherit' => false]; |
| 144 | |
| 145 | $dice = $this->dice->addRule('ParentClass', $rule); |
| 146 | $obj1 = $dice->create('Child'); |
| 147 | $obj2 = $dice->create('Child'); |
| 148 | $this->assertNotSame($obj1, $obj2); |
| 149 | } |
| 150 | |
| 151 | public function testSharedOverride() { |
| 152 | |
| 153 | //Set everything to shared by default |
| 154 | $dice = $this->dice->addRule('*', ['shared' => true]); |
| 155 | |
| 156 | $dice = $dice->addRule('A', ['shared' => false]); |
| 157 | |
| 158 | $a1 = $dice->create('A'); |
| 159 | $a2 = $dice->create('A'); |
| 160 | |
| 161 | $this->assertNotSame($a1, $a2); |
| 162 | |
| 163 | } |
| 164 | |
| 165 | public function testOptionalInterface() { |
| 166 | |
| 167 | $optionalInterface = $this->dice->create('OptionalInterface'); |
| 168 | |
| 169 | $this->assertEquals(null, $optionalInterface->obj); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | public function testScalarTypeHintWithShareInstances() { |
| 174 | |
| 175 | $dice = $this->dice->addRule('ScalarTypeHint', ['shareInstances' => ['A']]); |
| 176 | |
| 177 | $obj = $dice->create('ScalarTypeHint'); |
| 178 | |
| 179 | $this->assertInstanceOf('ScalarTypeHint', $obj); |
| 180 | } |
| 181 | |
| 182 | public function testPassGlobals() { |
| 183 | //write to the global $_GET variable |
| 184 | $_GET['foo'] = 'bar'; |
| 185 | |
| 186 | $dice = $this->dice->addRule('CheckConstructorArgs', |
| 187 | [ |
| 188 | 'constructParams' => [ |
| 189 | [\Dice\Dice::GLOBAL => '_GET'] |
| 190 | ] |
| 191 | ]); |
| 192 | |
| 193 | $obj = $dice->create('CheckConstructorArgs'); |
| 194 | |
| 195 | $this->assertEquals($_GET, $obj->arg1); |
| 196 | } |
| 197 | |
| 198 | public function testPassConstantString() { |
| 199 | $dice = $this->dice->addRule('CheckConstructorArgs', |
| 200 | [ |
| 201 | 'constructParams' => [ |
| 202 | [\Dice\Dice::CONSTANT => '\PDO::FETCH_ASSOC'] |
| 203 | ] |
| 204 | ]); |
| 205 | |
| 206 | $obj = $dice->create('CheckConstructorArgs'); |
| 207 | |
| 208 | $this->assertEquals(\PDO::FETCH_ASSOC, $obj->arg1); |
| 209 | } |
| 210 | |
| 211 | public function testImmutability() { |
| 212 | $this->assertEquals([], $this->dice->getRule('Foo')); |
| 213 | |
| 214 | $dice = $this->dice->addRule('Foo', ['shared' => true]); |
| 215 | |
| 216 | $this->assertEquals([], $this->dice->getRule('Foo')); |
| 217 | } |
| 218 | |
| 219 | public function testPassSelf() { |
| 220 | $dice = $this->dice->addRule('CheckConstructorArgs', |
| 221 | [ |
| 222 | 'constructParams' => [ |
| 223 | [\Dice\Dice::INSTANCE => \Dice\Dice::SELF] |
| 224 | ] |
| 225 | ]); |
| 226 | |
| 227 | $obj = $dice->create('CheckConstructorArgs'); |
| 228 | |
| 229 | $this->assertEquals($dice, $obj->arg1); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | // Issue 180 |
| 234 | public function testSlashNoSlash() { |
| 235 | $dice = $this->dice->addRule('\someclass', ['shared' => true]); |
| 236 | |
| 237 | $b = $dice->create('\someotherclass'); |
| 238 | $a = $dice->create('\someclass'); |
| 239 | |
| 240 | $this->assertSame($a, $b->obj); |
| 241 | } |
| 242 | } |