Skip to content

Commit a288cfb

Browse files
ChinaMoliyunwuxin
authored andcommitted
新增 单元测试
1 parent ab80b61 commit a288cfb

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

tests/ContainerTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace think\tests;
44

5+
use Closure;
6+
use Exception;
7+
use InvalidArgumentException;
58
use PHPUnit\Framework\TestCase;
69
use ReflectionMethod;
710
use stdClass;
@@ -55,6 +58,24 @@ public function __construct(Container $container)
5558
}
5659
}
5760

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+
5879
class ContainerTest extends TestCase
5980
{
6081
protected function tearDown(): void
@@ -316,12 +337,22 @@ public function testInvokeClassNotExists()
316337
$container->invokeClass('SomeClass');
317338
}
318339

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+
319351
protected function resolveContainer()
320352
{
321353
$container = new Container();
322354

323355
Container::setInstance($container);
324356
return $container;
325357
}
326-
327358
}

0 commit comments

Comments
 (0)