1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
<?php
namespace Illuminate\Tests\Container;
use Illuminate\Container\Container;
use PHPUnit\Framework\TestCase;
class ContainerTaggingTest extends TestCase
{
public function testContainerTags()
{
$container = new Container;
$container->tag(ContainerImplementationTaggedStub::class, 'foo', 'bar');
$container->tag(ContainerImplementationTaggedStubTwo::class, ['foo']);
$this->assertCount(1, $container->tagged('bar'));
$this->assertCount(2, $container->tagged('foo'));
$fooResults = [];
foreach ($container->tagged('foo') as $foo) {
$fooResults[] = $foo;
}
$barResults = [];
foreach ($container->tagged('bar') as $bar) {
$barResults[] = $bar;
}
$this->assertInstanceOf(ContainerImplementationTaggedStub::class, $fooResults[0]);
$this->assertInstanceOf(ContainerImplementationTaggedStub::class, $barResults[0]);
$this->assertInstanceOf(ContainerImplementationTaggedStubTwo::class, $fooResults[1]);
$container = new Container;
$container->tag([ContainerImplementationTaggedStub::class, ContainerImplementationTaggedStubTwo::class], ['foo']);
$this->assertCount(2, $container->tagged('foo'));
$fooResults = [];
foreach ($container->tagged('foo') as $foo) {
$fooResults[] = $foo;
}
$this->assertInstanceOf(ContainerImplementationTaggedStub::class, $fooResults[0]);
$this->assertInstanceOf(ContainerImplementationTaggedStubTwo::class, $fooResults[1]);
$this->assertCount(0, $container->tagged('this_tag_does_not_exist'));
}
public function testTaggedServicesAreLazyLoaded()
{
$container = $this->createPartialMock(Container::class, ['make']);
$container->expects($this->once())->method('make')->willReturn(new ContainerImplementationTaggedStub());
$container->tag(ContainerImplementationTaggedStub::class, ['foo']);
$container->tag(ContainerImplementationTaggedStubTwo::class, ['foo']);
$fooResults = [];
foreach ($container->tagged('foo') as $foo) {
$fooResults[] = $foo;
break;
}
$this->assertCount(2, $container->tagged('foo'));
$this->assertInstanceOf(ContainerImplementationTaggedStub::class, $fooResults[0]);
}
public function testLazyLoadedTaggedServicesCanBeLoopedOverMultipleTimes()
{
$container = new Container;
$container->tag(ContainerImplementationTaggedStub::class, 'foo');
$container->tag(ContainerImplementationTaggedStubTwo::class, ['foo']);
$services = $container->tagged('foo');
$fooResults = [];
foreach ($services as $foo) {
$fooResults[] = $foo;
}
$this->assertInstanceOf(ContainerImplementationTaggedStub::class, $fooResults[0]);
$this->assertInstanceOf(ContainerImplementationTaggedStubTwo::class, $fooResults[1]);
$fooResults = [];
foreach ($services as $foo) {
$fooResults[] = $foo;
}
$this->assertInstanceOf(ContainerImplementationTaggedStub::class, $fooResults[0]);
$this->assertInstanceOf(ContainerImplementationTaggedStubTwo::class, $fooResults[1]);
}
}
interface IContainerTaggedContractStub
{
//
}
class ContainerImplementationTaggedStub implements IContainerTaggedContractStub
{
//
}
class ContainerImplementationTaggedStubTwo implements IContainerTaggedContractStub
{
//
}
|