File: TypeHintContainerResolverTest.php

package info (click to toggle)
php-di-invoker 2.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 324 kB
  • sloc: php: 1,161; makefile: 13; sh: 6
file content (82 lines) | stat: -rw-r--r-- 2,390 bytes parent folder | download
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
<?php declare(strict_types=1);

namespace Invoker\Test\ParameterResolver\Container;

use Invoker\ParameterResolver\Container\TypeHintContainerResolver;
use Invoker\Test\Mock\ArrayContainer;
use PHPUnit\Framework\TestCase;

class TypeHintContainerResolverTest extends TestCase
{
    private const FIXTURE = TypeHintContainerResolverFixture::class;

    /** @var TypeHintContainerResolver */
    private $resolver;

    /** @var ArrayContainer */
    private $container;

    public function setUp(): void
    {
        $this->container = new ArrayContainer;
        $this->resolver = new TypeHintContainerResolver($this->container);
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_resolve_parameter_with_typehint_and_container()
    {
        $callable = function (TypeHintContainerResolverFixture $foo, self $bar) {
        };
        $reflection = new \ReflectionFunction($callable);

        $fixture = new TypeHintContainerResolverFixture;
        $this->container->set(self::FIXTURE, $fixture);
        $this->container->set(self::class, $this);

        $parameters = $this->resolver->getParameters($reflection, [], []);

        $this->assertCount(2, $parameters);
        $this->assertSame($fixture, $parameters[0]);
        $this->assertSame($this, $parameters[1]);
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_skip_parameter_if_container_cannot_provide_typehint()
    {
        $callable = function (TypeHintContainerResolverFixture $foo) {
        };
        $reflection = new \ReflectionFunction($callable);

        $parameters = $this->resolver->getParameters($reflection, [], []);

        $this->assertCount(0, $parameters);
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_skip_parameter_if_already_resolved()
    {
        $callable = function (TypeHintContainerResolverFixture $foo) {
        };
        $reflection = new \ReflectionFunction($callable);

        $this->container->set(self::FIXTURE, new TypeHintContainerResolverFixture);

        $resolvedParameters = ['first param value'];
        $parameters = $this->resolver->getParameters($reflection, [], $resolvedParameters);

        $this->assertSame($resolvedParameters, $parameters);
    }
}

class TypeHintContainerResolverFixture
{
}