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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
<?php
declare(strict_types=1);
namespace DI\Test\UnitTest\Definition\Resolver;
use DI\Container;
use DI\Factory\RequestedEntry;
use DI\Invoker\FactoryParameterResolver;
use DI\Test\UnitTest\Definition\Resolver\Fixture\NoConstructor;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
/**
* @covers \DI\Invoker\FactoryParameterResolver
*/
#[\PHPUnit\Framework\Attributes\CoversClass(\DI\Invoker\FactoryParameterResolver::class)]
class FactoryParameterResolverTest extends TestCase
{
private FactoryParameterResolver $resolver;
private MockObject|ContainerInterface $container;
private MockObject|RequestedEntry $requestedEntry;
public function setUp(): void
{
$this->markTestSkipped('Requires mnapoli/phpunit-easymock');
$this->container = $this->easyMock(ContainerInterface::class);
$this->resolver = new FactoryParameterResolver($this->container);
$this->requestedEntry = $this->easyMock(RequestedEntry::class);
}
/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function should_resolve_psr11_container()
{
$callable = function (ContainerInterface $c) {
};
$reflection = new \ReflectionFunction($callable);
$parameters = $this->resolver->getParameters($reflection, [$this->container, $this->requestedEntry], []);
$this->assertCount(1, $parameters);
$this->assertSame($this->container, $parameters[0]);
}
/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function should_resolve_container_and_requested_entry()
{
$callable = function (ContainerInterface $c, RequestedEntry $entry) {
};
$reflection = new \ReflectionFunction($callable);
$parameters = $this->resolver->getParameters($reflection, [$this->container, $this->requestedEntry], []);
$this->assertCount(2, $parameters);
$this->assertSame($this->container, $parameters[0]);
$this->assertSame($this->requestedEntry, $parameters[1]);
}
/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function should_resolve_only_container()
{
$callable = function (ContainerInterface $c) {
};
$reflection = new \ReflectionFunction($callable);
$parameters = $this->resolver->getParameters($reflection, [$this->container, $this->requestedEntry], []);
$this->assertCount(1, $parameters);
$this->assertSame($this->container, $parameters[0]);
}
/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function should_resolve_only_requested_entry()
{
$callable = function (RequestedEntry $entry) {
};
$reflection = new \ReflectionFunction($callable);
$parameters = $this->resolver->getParameters($reflection, [$this->container, $this->requestedEntry], []);
$this->assertCount(1, $parameters);
$this->assertSame($this->requestedEntry, $parameters[0]);
}
/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function should_resolve_nothing()
{
$callable = function () {
};
$reflection = new \ReflectionFunction($callable);
$parameters = $this->resolver->getParameters($reflection, [$this->container, $this->requestedEntry], []);
$this->assertCount(0, $parameters);
}
/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function should_not_overwrite_resolved_with_container_or_entry()
{
$callable = function (ContainerInterface $container, RequestedEntry $entry, $other) {
};
$reflection = new \ReflectionFunction($callable);
$mockContainer = $this->easyMock(Container::class);
$mockEntry = $this->easyMock(RequestedEntry::class);
$resolvedParams = [$mockContainer, $mockEntry, 'Foo'];
$parameters = $this->resolver->getParameters($reflection, [$this->container, $this->requestedEntry], $resolvedParams);
$this->assertCount(3, $parameters);
$this->assertSame($parameters[0], $mockContainer);
$this->assertSame($parameters[1], $mockEntry);
$this->assertEquals('Foo', $parameters[2]);
}
/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function should_not_overwrite_resolved_from_container()
{
$callable = function (NoConstructor $nc) {
};
$reflection = new \ReflectionFunction($callable);
$ncMock = $this->easyMock(NoConstructor::class);
$ncReal = new NoConstructor();
$preparedContainer = clone $this->container;
$preparedContainer->expects($this->once())
->method('has')
->with(NoConstructor::class)
->willReturn(true);
$preparedContainer->expects($this->once())
->method('get')
->with(NoConstructor::class)
->willReturn($ncReal);
$resolvedParams = [$ncMock];
$parameters = $this->resolver->getParameters($reflection, [$this->container, $this->requestedEntry], $resolvedParams);
$this->assertCount(1, $parameters);
$this->assertSame($parameters[0], $ncMock);
}
}
|