File tree Expand file tree Collapse file tree 1 file changed +32
-1
lines changed Expand file tree Collapse file tree 1 file changed +32
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace think \tests ;
4
4
5
+ use Closure ;
6
+ use Exception ;
7
+ use InvalidArgumentException ;
5
8
use PHPUnit \Framework \TestCase ;
6
9
use ReflectionMethod ;
7
10
use stdClass ;
@@ -55,6 +58,24 @@ public function __construct(Container $container)
55
58
}
56
59
}
57
60
61
+ class WithDefaultValues
62
+ {
63
+ public $ container ;
64
+
65
+ public function __construct (?Container $ container = null )
66
+ {
67
+ $ this ->container = $ container ;
68
+ }
69
+
70
+ /**
71
+ * 有默认值且类存在,但无法注入
72
+ */
73
+ public static function classExistsButCannotBeInjected (?Closure $ closure = null )
74
+ {
75
+ return $ closure ;
76
+ }
77
+ }
78
+
58
79
class ContainerTest extends TestCase
59
80
{
60
81
protected function tearDown (): void
@@ -316,12 +337,22 @@ public function testInvokeClassNotExists()
316
337
$ container ->invokeClass ('SomeClass ' );
317
338
}
318
339
340
+ public function testInvokeWithDefaultValues ()
341
+ {
342
+ $ container = $ this ->resolveContainer ();
343
+ $ container ->bind (Container::class, $ container );
344
+
345
+ $ class = $ container ->make (WithDefaultValues::class);
346
+ $ this ->assertSame ($ container , $ class ->container );
347
+
348
+ $ this ->assertSame (null , $ container ->invokeMethod (WithDefaultValues::class . '::classExistsButCannotBeInjected ' ));
349
+ }
350
+
319
351
protected function resolveContainer ()
320
352
{
321
353
$ container = new Container ();
322
354
323
355
Container::setInstance ($ container );
324
356
return $ container ;
325
357
}
326
-
327
358
}
You can’t perform that action at this time.
0 commit comments