File: FactoryResolverTest.php

package info (click to toggle)
php-di 7.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,932 kB
  • sloc: php: 10,572; makefile: 42; xml: 17; sh: 10; pascal: 5
file content (150 lines) | stat: -rw-r--r-- 5,202 bytes parent folder | download | duplicates (2)
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
<?php

declare(strict_types=1);

namespace DI\Test\UnitTest\Definition\Resolver;

use DI\Definition\FactoryDefinition;
use DI\Definition\ObjectDefinition;
use DI\Definition\Resolver\DefinitionResolver;
use DI\Definition\Resolver\FactoryResolver;
use DI\NotFoundException;
use DI\Test\UnitTest\Definition\Resolver\Fixture\FixtureClass;
use DI\Test\UnitTest\Definition\Resolver\Fixture\NoConstructor;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use DI\Definition\Exception\InvalidDefinition;

/**
 * @covers \DI\Definition\Resolver\FactoryResolver
 */
#[\PHPUnit\Framework\Attributes\CoversClass(\DI\Definition\Resolver\FactoryResolver::class)]
class FactoryResolverTest extends TestCase
{

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_resolve_callables()
    {
        $this->markTestSkipped('Requires mnapoli/phpunit-easymock');
        $container = $this->easyMock(ContainerInterface::class);
        $resolver = new FactoryResolver($container, $this->easyMock(DefinitionResolver::class));

        $definition = new FactoryDefinition('foo', function () {
            return 'bar';
        });

        $value = $resolver->resolve($definition);

        $this->assertEquals('bar', $value);
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_inject_container()
    {
        $this->markTestSkipped('Requires mnapoli/phpunit-easymock');
        $container = $this->easyMock(ContainerInterface::class);
        $resolver = new FactoryResolver($container, $this->easyMock(DefinitionResolver::class));

        $definition = new FactoryDefinition('foo', function ($c) {
            return $c;
        });

        $value = $resolver->resolve($definition);

        $this->assertInstanceOf(ContainerInterface::class, $value);
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_throw_if_the_factory_is_not_callable()
    {
        $this->markTestSkipped('Requires mnapoli/phpunit-easymock');
        $this->expectException(InvalidDefinition::class);
        $this->expectExceptionMessage('Entry "foo" cannot be resolved: factory \'Hello world\' is neither a callable nor a valid container entry');
        $container = $this->easyMock(ContainerInterface::class);
        $resolver = new FactoryResolver($container, $this->easyMock(DefinitionResolver::class));

        $definition = new FactoryDefinition('foo', 'Hello world');

        $container->method('get')
            ->willThrowException(new NotFoundException);

        $resolver->resolve($definition);
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_throw_if_not_enough_parameters()
    {
        $this->markTestSkipped('Requires mnapoli/phpunit-easymock');
        $this->expectException(InvalidDefinition::class);
        $this->expectExceptionMessage('Entry "foo" cannot be resolved: Unable to invoke the callable because no value was given for parameter 3 ($c)');
        $container = $this->easyMock(ContainerInterface::class);
        $resolver = new FactoryResolver($container, $this->easyMock(DefinitionResolver::class));

        $definition = new FactoryDefinition('foo', function ($a, $b, $c) {
        });

        $resolver->resolve($definition);
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_inject_parameters()
    {
        $this->markTestSkipped('Requires mnapoli/phpunit-easymock');
        $container = $this->easyMock(ContainerInterface::class);
        $resolver = new FactoryResolver($container, $this->easyMock(DefinitionResolver::class));

        $testCase = $this;
        $definition = new FactoryDefinition('foo', function ($c, $par1, $par2, $baz) use ($testCase) {
            $testCase->assertEquals('Parameter 1', $par1);
            $testCase->assertEquals(2, $par2);
            $testCase->assertEquals('bar', $baz);

            return $c;
        }, ['par1' => 'Parameter 1', 'par2' => 2]);

        $value = $resolver->resolve($definition, ['baz' => 'bar']);

        $this->assertInstanceOf(ContainerInterface::class, $value);
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_resolve_nested_definition_in_parameters()
    {
        $this->markTestSkipped('Requires mnapoli/phpunit-easymock');
        $container = $this->easyMock(ContainerInterface::class);
        $parentResolver = $this->easyMock(DefinitionResolver::class);
        $resolver = new FactoryResolver($container, $parentResolver);

        $definition = new FactoryDefinition('foo', function ($par1) {
            return new FixtureClass($par1);
        }, ['par1' => new ObjectDefinition('', NoConstructor::class)]);

        $parentResolver->expects($this->once())
            ->method('resolve')
            ->with($this->isInstanceOf(ObjectDefinition::class))
            ->willReturn('bar');

        $object = $resolver->resolve($definition);

        $this->assertInstanceOf(FixtureClass::class, $object);
        $this->assertEquals('bar', $object->constructorParam1);
    }
}