5
5
use Parable \Di \Container ;
6
6
use Parable \Di \Exceptions \ContainerException ;
7
7
use Parable \Di \Exceptions \NotFoundException ;
8
- use Parable \Di \Tests \Classes \BadDependency ;
8
+ use Parable \Di \Tests \Classes \ScalarDependency ;
9
9
use Parable \Di \Tests \Classes \CyclicalDependencyFirst ;
10
10
use Parable \Di \Tests \Classes \CyclicalDependencySecond ;
11
11
use Parable \Di \Tests \Classes \Dependencies ;
14
14
use Parable \Di \Tests \Classes \FakeWithInterface ;
15
15
use Parable \Di \Tests \Classes \FakeWithInterfaceDependency ;
16
16
use Parable \Di \Tests \Classes \NoDependencies ;
17
+ use Parable \Di \Tests \Classes \ScalarDependencyWithDefault ;
18
+ use Parable \Di \Tests \Classes \ScalarDependencyWithDefaultAndNonScalar ;
19
+ use Parable \Di \Tests \Classes \ScalarDependencyWithDefaultAndNonScalarReverse ;
17
20
18
21
class DiTest extends \PHPUnit \Framework \TestCase
19
22
{
@@ -156,9 +159,37 @@ public function testGetDependenciesForThrowsOnBadId()
156
159
public function testGetDependenciesForThrowsOnStringConstructorParameter ()
157
160
{
158
161
self ::expectException (ContainerException::class);
159
- self ::expectExceptionMessage ('Cannot inject value for constructor parameter `$nope`. ' );
162
+ self ::expectExceptionMessage ('Cannot inject value for non-optional constructor parameter `$nope` without a default value . ' );
160
163
161
- $ this ->container ->getDependenciesFor (BadDependency::class);
164
+ $ this ->container ->getDependenciesFor (ScalarDependency::class);
165
+ }
166
+
167
+ public function testGetDependenciesForScalarWithDefaultSetsDefaultValueAppropriately ()
168
+ {
169
+ $ dependencies = $ this ->container ->getDependenciesFor (ScalarDependencyWithDefault::class);
170
+
171
+ self ::assertSame (
172
+ ['hello ' ],
173
+ $ dependencies
174
+ );
175
+ }
176
+
177
+ public function testGetDependenciesForWillUseDefaultValueForScalarIfMixedWithActualDependency ()
178
+ {
179
+ $ dependencies = $ this ->container ->getDependenciesFor (ScalarDependencyWithDefaultAndNonScalar::class);
180
+
181
+ self ::assertInstanceOf (NoDependencies::class, $ dependencies [0 ]);
182
+ self ::assertSame ('hello ' , $ dependencies [1 ]);
183
+ }
184
+
185
+ public function testOptionalBeforeRequiredBreaksGetDependenciesFor ()
186
+ {
187
+ self ::expectException (ContainerException::class);
188
+ self ::expectExceptionMessage (
189
+ 'Cannot inject value for non-optional constructor parameter `$nope` without a default value. '
190
+ );
191
+
192
+ $ this ->container ->getDependenciesFor (ScalarDependencyWithDefaultAndNonScalarReverse::class);
162
193
}
163
194
164
195
public function testGetDependenciesForWithNewDependenciesWorks ()
@@ -177,6 +208,14 @@ public function testGetDependenciesForWithNewDependenciesWorks()
177
208
self ::assertSame ('new ' , $ instance ->fakeObject ->value );
178
209
}
179
210
211
+ public function testGetDependenciesForDoesntLikeInvalidValuePassed ()
212
+ {
213
+ $ this ->expectException (ContainerException::class);
214
+ $ this ->expectExceptionMessage ('Invalid dependency type value passed: `666` ' );
215
+
216
+ $ this ->container ->getDependenciesFor (Dependencies::class, 666 );
217
+ }
218
+
180
219
public function testIdsAreNormalized ()
181
220
{
182
221
$ this ->container ->get (NoDependencies::class);
0 commit comments