File: InjectTest.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 (104 lines) | stat: -rw-r--r-- 3,411 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
<?php

declare(strict_types=1);

namespace DI\Test\UnitTest\Attributes;

use DI\Attribute\Inject;
use DI\Test\UnitTest\Attributes\Fixtures\InjectFixture;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use DI\Definition\Exception\InvalidAttribute;

/**
 * Inject annotation test class.
 *
 * @requires PHP >= 8
 *
 * @covers \DI\Attribute\Inject
 */
#[\PHPUnit\Framework\Attributes\CoversClass(\DI\Attribute\Inject::class)]
class InjectTest extends TestCase
{
    private ReflectionClass $reflectionClass;

    public function setUp(): void
    {
        $this->reflectionClass = new ReflectionClass(InjectFixture::class);
    }

    public function testProperty1()
    {
        $property = $this->reflectionClass->getProperty('property1');
        /** @var Inject $annotation */
        $annotation = $property->getAttributes(Inject::class)[0]->newInstance();

        $this->assertInstanceOf(Inject::class, $annotation);
        $this->assertEquals('foo', $annotation->getName());
    }

    public function testProperty2()
    {
        $property = $this->reflectionClass->getProperty('property2');
        /** @var Inject $annotation */
        $annotation = $property->getAttributes(Inject::class)[0]->newInstance();

        $this->assertInstanceOf(Inject::class, $annotation);
        $this->assertNull($annotation->getName());
    }

    public function testProperty3()
    {
        $property = $this->reflectionClass->getProperty('property3');
        /** @var Inject $annotation */
        $annotation = $property->getAttributes(Inject::class)[0]->newInstance();

        $this->assertInstanceOf(Inject::class, $annotation);
        $this->assertEquals('foo', $annotation->getName());
    }

    public function testMethod1()
    {
        $method = $this->reflectionClass->getMethod('method1');
        /** @var Inject $annotation */
        $annotation = $method->getAttributes(Inject::class)[0]->newInstance();

        $this->assertInstanceOf(Inject::class, $annotation);
        $this->assertEmpty($annotation->getParameters());
    }

    public function testMethod2()
    {
        $method = $this->reflectionClass->getMethod('method2');
        /** @var Inject $annotation */
        $annotation = $method->getAttributes(Inject::class)[0]->newInstance();
        $parameters = $annotation->getParameters();

        $this->assertInstanceOf(Inject::class, $annotation);
        $this->assertCount(2, $parameters);
        $this->assertEquals('foo', $parameters[0]);
        $this->assertEquals('bar', $parameters[1]);
    }

    public function testMethod3()
    {
        $method = $this->reflectionClass->getMethod('method3');
        /** @var Inject $annotation */
        $annotation = $method->getAttributes(Inject::class)[0]->newInstance();
        $parameters = $annotation->getParameters();

        $this->assertInstanceOf(Inject::class, $annotation);
        $this->assertCount(1, $parameters);

        $this->assertArrayHasKey('str1', $parameters);
        $this->assertEquals('foo', $parameters['str1']);
    }

    public function testInvalidAnnotation()
    {
        $this->expectException(InvalidAttribute::class);
        $this->expectExceptionMessage("#[Inject(['param' => 'value'])] expects \"value\" to be a string, [] given.");
        $method = $this->reflectionClass->getMethod('method4');
        $method->getAttributes(Inject::class)[0]->newInstance();
    }
}