Skip to content

Commit eea9a56

Browse files
committed
More extensive test
1 parent 7f8628e commit eea9a56

File tree

1 file changed

+82
-8
lines changed

1 file changed

+82
-8
lines changed

tests/Container/ContainerTest.php

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,20 +381,59 @@ public function testResolvingCallbacksAreCalledForType()
381381
public function testResolvingCallbacksAreCalledOnceForImplementations()
382382
{
383383
$container = new Container;
384-
$resolving_invocations = 0;
385-
$after_resolving_invocations = 0;
384+
$resolving_contract_invocations = 0;
385+
$after_resolving_contract_invocations = 0;
386+
$resolving_implementation_invocations = 0;
387+
$after_resolving_implementation_invocations = 0;
386388

387-
$container->resolving(IContainerContractStub::class, function () use (&$resolving_invocations) {
388-
$resolving_invocations++;
389+
$container->resolving(IContainerContractStub::class, function () use (&$resolving_contract_invocations) {
390+
$resolving_contract_invocations++;
389391
});
390-
$container->afterResolving(IContainerContractStub::class, function () use (&$after_resolving_invocations) {
391-
$after_resolving_invocations++;
392+
$container->afterResolving(IContainerContractStub::class, function () use (&$after_resolving_contract_invocations) {
393+
$after_resolving_contract_invocations++;
394+
});
395+
$container->resolving(ContainerImplementationStub::class, function () use (&$resolving_implementation_invocations) {
396+
$resolving_implementation_invocations++;
397+
});
398+
$container->afterResolving(ContainerImplementationStub::class, function () use (&$after_resolving_implementation_invocations) {
399+
$after_resolving_implementation_invocations++;
392400
});
393401
$container->bind(IContainerContractStub::class, ContainerImplementationStub::class);
394402
$container->make(IContainerContractStub::class);
395403

396-
$this->assertEquals(1, $resolving_invocations);
397-
$this->assertEquals(1, $after_resolving_invocations);
404+
$this->assertEquals(1, $resolving_contract_invocations);
405+
$this->assertEquals(1, $after_resolving_contract_invocations);
406+
$this->assertEquals(1, $resolving_implementation_invocations);
407+
$this->assertEquals(1, $after_resolving_implementation_invocations);
408+
}
409+
410+
public function testResolvingCallbacksAreCalledForNestedDependencies()
411+
{
412+
$container = new Container;
413+
$resolving_dependency_interface_invocations = 0;
414+
$resolving_dependency_implementation_invocations = 0;
415+
$resolving_dependent_invocations = 0;
416+
417+
$container->bind(IContainerContractStub::class, ContainerImplementationStub::class);
418+
419+
$container->resolving(IContainerContractStub::class, function () use (&$resolving_dependency_interface_invocations) {
420+
$resolving_dependency_interface_invocations++;
421+
});
422+
423+
$container->resolving(ContainerImplementationStub::class, function () use (&$resolving_dependency_implementation_invocations) {
424+
$resolving_dependency_implementation_invocations++;
425+
});
426+
427+
$container->resolving(ContainerNestedDependentStubTwo::class, function () use (&$resolving_dependent_invocations) {
428+
$resolving_dependent_invocations++;
429+
});
430+
431+
$container->make(ContainerNestedDependentStubTwo::class);
432+
$container->make(ContainerNestedDependentStubTwo::class);
433+
434+
$this->assertEquals(4, $resolving_dependency_interface_invocations);
435+
$this->assertEquals(4, $resolving_dependency_implementation_invocations);
436+
$this->assertEquals(2, $resolving_dependent_invocations);
398437
}
399438

400439
public function testUnsetRemoveBoundInstances()
@@ -937,6 +976,19 @@ public function testResolvingCallbacksShouldBeFiredWhenCalledWithAliases()
937976
$this->assertEquals('taylor', $instance->name);
938977
}
939978

979+
public function testInterfaceResolvingCallbacksShouldBeFiredWhenCalledWithAliases()
980+
{
981+
$container = new Container;
982+
$container->alias(IContainerContractStub::class, 'foo');
983+
$container->resolving(IContainerContractStub::class, function ($object) {
984+
return $object->name = 'taylor';
985+
});
986+
$container->bind('foo', ContainerImplementationStub::class);
987+
$instance = $container->make('foo');
988+
989+
$this->assertEquals('taylor', $instance->name);
990+
}
991+
940992
public function testMakeWithMethodIsAnAliasForMakeMethod()
941993
{
942994
$mock = $this->getMockBuilder(Container::class)
@@ -1089,6 +1141,18 @@ public function __construct(IContainerContractStub $impl)
10891141
}
10901142
}
10911143

1144+
class ContainerDependentStubTwo
1145+
{
1146+
public $implA;
1147+
public $implB;
1148+
1149+
public function __construct(IContainerContractStub $implA, IContainerContractStub $implB)
1150+
{
1151+
$this->implA = $implA;
1152+
$this->implB = $implB;
1153+
}
1154+
}
1155+
10921156
class ContainerNestedDependentStub
10931157
{
10941158
public $inner;
@@ -1099,6 +1163,16 @@ public function __construct(ContainerDependentStub $inner)
10991163
}
11001164
}
11011165

1166+
class ContainerNestedDependentStubTwo
1167+
{
1168+
public $inner;
1169+
1170+
public function __construct(ContainerDependentStubTwo $inner)
1171+
{
1172+
$this->inner = $inner;
1173+
}
1174+
}
1175+
11021176
class ContainerDefaultValueStub
11031177
{
11041178
public $stub;

0 commit comments

Comments
 (0)