File: ObjectDefinitionTest.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 (121 lines) | stat: -rw-r--r-- 4,553 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
<?php

declare(strict_types=1);

namespace DI\Test\UnitTest\Definition;

use DI\Definition\ObjectDefinition;
use DI\Definition\ObjectDefinition\MethodInjection;
use DI\Definition\ObjectDefinition\PropertyInjection;
use DI\Test\UnitTest\Definition\Fixture\NonInstantiableClass;
use PHPUnit\Framework\TestCase;

/**
 * @covers \DI\Definition\ObjectDefinition
 */
#[\PHPUnit\Framework\Attributes\CoversClass(\DI\Definition\ObjectDefinition::class)]
class ObjectDefinitionTest extends TestCase
{
    public function test_getters_setters()
    {
        $definition = new ObjectDefinition('foo', 'bar');
        $definition->setLazy(true);

        $this->assertEquals('foo', $definition->getName());
        $this->assertEquals('bar', $definition->getClassName());
        $this->assertTrue($definition->isLazy());

        $definition->setClassName('classname');
        $this->assertEquals('classname', $definition->getClassName());
    }

    public function test_defaults()
    {
        $definition = new ObjectDefinition('foo');

        $this->assertEquals('foo', $definition->getName());
        $this->assertEquals('foo', $definition->getClassName());
        $this->assertFalse($definition->isLazy());
        $this->assertNull($definition->getConstructorInjection());
        $this->assertEmpty($definition->getPropertyInjections());
        $this->assertEmpty($definition->getMethodInjections());
    }

    public function test_class_exists()
    {
        $definition = new ObjectDefinition('foo');
        self::assertFalse($definition->classExists());
        $definition = new ObjectDefinition(self::class);
        self::assertTrue($definition->classExists());
    }

    public function test_is_instantiable()
    {
        $definition = new ObjectDefinition('foo');
        self::assertFalse($definition->isInstantiable());
        $definition = new ObjectDefinition(NonInstantiableClass::class);
        self::assertFalse($definition->isInstantiable());
        $definition = new ObjectDefinition(\stdClass::class);
        self::assertTrue($definition->classExists());
    }

    public function test_property_injections()
    {
        $definition = new ObjectDefinition('foo');
        $definition->addPropertyInjection(new PropertyInjection('property1', 'Property1'));
        $definition->addPropertyInjection(new PropertyInjection('property2', 'Property2'));
        $this->assertEquals([
            'property1' => new PropertyInjection('property1', 'Property1'),
            'property2' => new PropertyInjection('property2', 'Property2'),
        ], $definition->getPropertyInjections());
    }

    public function test_method_injections()
    {
        $definition = new ObjectDefinition('foo');
        $definition->setConstructorInjection(MethodInjection::constructor());
        $definition->addMethodInjection(new MethodInjection('method1', ['foo']));
        $definition->addMethodInjection(new MethodInjection('method2'));

        $this->assertNotNull($definition->getConstructorInjection());
        $this->assertEquals([
            new MethodInjection('method1', ['foo']),
            new MethodInjection('method2'),
        ], $definition->getMethodInjections());
    }

    public function test_replace_wildcards()
    {
        $definition = new ObjectDefinition('class', 'Foo*\Bar*\Baz*');
        $definition->replaceWildcards(['1', '2', '3']);
        $this->assertEquals('Foo1\Bar2\Baz3', $definition->getClassName());
    }

    public function test_replace_wildcards_with_no_classname_defined()
    {
        $definition = new ObjectDefinition('Foo*\Bar*\Baz*');
        $definition->replaceWildcards(['1', '2', '3']);
        $this->assertEquals('Foo1\Bar2\Baz3', $definition->getClassName());
    }

    public function test_replace_wildcards_with_extra_replacements()
    {
        $definition = new ObjectDefinition('Foo*\Bar\Baz');
        $definition->replaceWildcards(['1', '2', '3']);
        $this->assertEquals('Foo1\Bar\Baz', $definition->getClassName());
    }

    public function test_replace_wildcards_with_missing_replacements()
    {
        $definition = new ObjectDefinition('Foo*\Bar*\Baz*');
        $definition->replaceWildcards(['1']);
        $this->assertEquals('Foo1\Bar*\Baz*', $definition->getClassName());
    }

    public function test_replace_wildcards_with_no_wildcards()
    {
        $definition = new ObjectDefinition('Foo\Bar\Baz');
        $definition->replaceWildcards(['1', '2', '3']);
        $this->assertEquals('Foo\Bar\Baz', $definition->getClassName());
    }
}