File: AttributesTest.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 (148 lines) | stat: -rw-r--r-- 4,859 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
<?php

declare(strict_types=1);

namespace DI\Test\IntegrationTest\Attributes;

use DI\ContainerBuilder;
use DI\Test\IntegrationTest\BaseContainerTest;
use DI\DependencyException;

/**
 * Test using PHP 8 attributes.
 *
 * @requires PHP >= 8
 */
#[\PHPUnit\Framework\Attributes\RequiresPhp('>= 8')]
class AttributesTest extends BaseContainerTest
{
    /**
     * @test
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\Test]
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function inject_in_properties(ContainerBuilder $builder)
    {
        $builder->useAttributes(true);

        /** @var B $object */
        $object = $builder->build()->get(B::class);

        $this->assertInstanceOf(A::class, $object->public);
        $this->assertInstanceOf(A::class, $object->getProtected());
        $this->assertInstanceOf(A::class, $object->getPrivate());
    }

    /**
     * Inject in parent properties (public, protected and private).
     *
     * @test
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\Test]
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function inject_in_parent_properties(ContainerBuilder $builder)
    {
        $builder->useAttributes(true);
        $container = $builder->build();

        /** @var C $object */
        $object = $container->get(C::class);
        $this->assertInstanceOf(A::class, $object->public);
        $this->assertInstanceOf(A::class, $object->getProtected());
        $this->assertInstanceOf(A::class, $object->getPrivate());

        /** @var D $object */
        $object = $container->get(D::class);
        $this->assertInstanceOf(A::class, $object->public);
        $this->assertInstanceOf(A::class, $object->getProtected());
        $this->assertInstanceOf(A::class, $object->getPrivate());
    }

    /**
     * Inject in private parent properties even if they have the same name of child properties.
     *
     * @test
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\Test]
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function inject_in_private_parent_properties_with_same_name(ContainerBuilder $builder)
    {
        $builder->useAttributes(true);
        $container = $builder->build();

        /** @var Child $object */
        $object = $container->get(Child::class);
        $this->assertInstanceOf(A::class, $object->public);
        $this->assertInstanceOf(A::class, $object->getProtected());
        $this->assertInstanceOf(A::class, $object->getPrivate());
        $this->assertInstanceOf(A::class, $object->getChildPrivate());
    }

    /**
     * @test
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\Test]
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function inject_by_name(ContainerBuilder $builder)
    {
        $builder->useAttributes(true);

        $dependency = new \stdClass();

        $builder->addDefinitions([
            'namedDependency'  => $dependency,
        ]);
        $container = $builder->build();

        /** @var NamedInjection $object */
        $object = $container->get(NamedInjection::class);
        $this->assertSame($dependency, $object->dependency1);
        $this->assertSame($dependency, $object->dependency2);
    }

    /**
     * @test
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\Test]
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function errors_if_dependency_by_name_not_found(ContainerBuilder $builder)
    {
        $this->expectException(DependencyException::class);
        $builder->useAttributes(true);
        $builder->build()->get(NamedInjection::class);
    }

    /**
     * @test
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\Test]
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function inject_promoted_property(ContainerBuilder $builder)
    {
        $builder->useAttributes(true);
        $object = $builder->build()->get(PromotedProperty::class);
        $this->assertInstanceOf(A::class, $object->promotedProperty);
    }

    /**
     * @test
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\Test]
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function inject_promoted_readonly_property(ContainerBuilder $builder)
    {
        if (PHP_VERSION_ID < 80100) {
            $this->markTestSkipped("PHP 8.1 required for readonly properties");
        }
        $builder->useAttributes(true);
        $object = $builder->build()->get(PromotedReadonlyProperty::class);
        $this->assertInstanceOf(A::class, $object->promotedProperty);
    }
}