File: ProxyTest.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 (99 lines) | stat: -rw-r--r-- 2,782 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php

declare(strict_types=1);

namespace DI\Test\IntegrationTest;

use DI\ContainerBuilder;
use DI\Test\IntegrationTest\Fixtures\LazyDependency;
use DI\Test\IntegrationTest\Fixtures\ProxyTest\A;
use DI\Test\IntegrationTest\Fixtures\ProxyTest\B;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\Test;
use ProxyManager\Proxy\LazyLoadingInterface;

/**
 * Test lazy injections with proxies.
 */
class ProxyTest extends BaseContainerTest
{
    /**
     * @test
     * @dataProvider provideContainer
     */
    #[Test]
    #[DataProvider('provideContainer')]
    public function container_can_create_lazy_objects(ContainerBuilder $builder)
    {
        $builder->useAutowiring(false);
        $builder->addDefinitions([
            'foo' => \DI\create(LazyDependency::class)
                ->lazy(),
        ]);

        $proxy = $builder->build()->get('foo');

        $this->assertInstanceOf(LazyDependency::class, $proxy);

        if (PHP_VERSION_ID >= 80400) {
            $this->assertTrue(
                (new \ReflectionClass(LazyDependency::class))->isUninitializedLazyObject(
                    $proxy
                )
            );
        } else {
            $this->assertInstanceOf(LazyLoadingInterface::class, $proxy);
        }
    }

    /**
     * @test
     * @dataProvider provideContainer
     */
    #[Test]
    #[DataProvider('provideContainer')]
    public function lazy_services_resolve_to_the_same_instance(ContainerBuilder $builder)
    {
        $builder->useAutowiring(false);
        $builder->addDefinitions([
            'foo' => \DI\create(LazyDependency::class)
                ->lazy(),
        ]);
        $container = $builder->build();

        /** @var LazyDependency $proxy */
        $proxy = $container->get('foo');
        $this->assertSame($proxy, $container->get('foo'));

        // Resolve the proxy and check again
        /** @noinspection PhpExpressionResultUnusedInspection */
        $proxy->getValue();

        $this->assertSame($proxy, $container->get('foo'));
    }

    /**
     * @test
     * @dataProvider provideContainer
     */
    #[Test]
    #[DataProvider('provideContainer')]
    public function dependencies_of_proxies_are_resolved_once(ContainerBuilder $builder)
    {
        $builder->useAutowiring(false);
        $builder->addDefinitions([
            'A' => \DI\create(A::class)
                ->constructor(\DI\get('B'))
                ->lazy(),
            'B' => \DI\create(B::class),
        ]);
        $container = $builder->build();

        /** @var A $a1 */
        $a1 = $container->get('A');
        /** @var A $a2 */
        $a2 = $container->get('A');
        $this->assertSame($a1->getB(), $a2->getB());
    }
}