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
|
<?php
declare(strict_types=1);
namespace DI\Test\UnitTest\Definition\Resolver;
use DI\Definition\Reference;
use DI\Definition\ArrayDefinition;
use DI\Definition\ObjectDefinition;
use DI\Definition\Resolver\ArrayResolver;
use DI\Definition\Resolver\DefinitionResolver;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use DI\DependencyException;
/**
* @covers \DI\Definition\Resolver\ArrayResolver
*/
#[\PHPUnit\Framework\Attributes\CoversClass(\DI\Definition\Resolver\ArrayResolver::class)]
class ArrayResolverTest extends TestCase
{
private MockObject|DefinitionResolver $parentResolver;
private ArrayResolver $resolver;
public function setUp(): void
{
$this->markTestSkipped('Requires mnapoli/phpunit-easymock');
$this->parentResolver = $this->easyMock(DefinitionResolver::class);
$this->resolver = new ArrayResolver($this->parentResolver);
}
/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function should_resolve_array_of_values()
{
$definition = new ArrayDefinition([
'bar',
42,
]);
$value = $this->resolver->resolve($definition);
$this->assertEquals(['bar', 42], $value);
}
/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function should_resolve_nested_definitions()
{
$this->parentResolver->expects($this->exactly(2))
->method('resolve')
->willReturnMap([
[$this->isInstanceOf(Reference::class)],
[$this->isInstanceOf(ObjectDefinition::class)],
])
->willReturnOnConsecutiveCalls(42, new \stdClass());
$definition = new ArrayDefinition([
'bar',
new Reference('bar'),
new ObjectDefinition('', 'bar'),
]);
$value = $this->resolver->resolve($definition);
$this->assertEquals(['bar', 42, new \stdClass()], $value);
}
/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function resolve_should_preserve_keys()
{
$definition = new ArrayDefinition([
'hello' => 'world',
]);
$value = $this->resolver->resolve($definition);
$this->assertEquals(['hello' => 'world'], $value);
}
/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function should_throw_with_a_nice_message()
{
$this->expectException(DependencyException::class);
$this->expectExceptionMessage('Error while resolving foo[0]. This is a message');
$this->parentResolver->expects($this->once())
->method('resolve')
->willThrowException(new \Exception('This is a message'));
$definition = new ArrayDefinition([
new Reference('bar'),
]);
$definition->setName('foo');
$this->resolver->resolve($definition);
}
}
|