File: ErrorMessagesTest.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 (176 lines) | stat: -rw-r--r-- 6,065 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php

declare(strict_types=1);

namespace DI\Test\IntegrationTest\ErrorMessages;

use DI\ContainerBuilder;
use DI\Definition\Exception\InvalidDefinition;
use DI\Test\IntegrationTest\BaseContainerTest;
use function DI\autowire;
use DI\DependencyException;

/**
 * Test error messages.
 */
class ErrorMessagesTest extends BaseContainerTest
{
    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_non_instantiable_class(ContainerBuilder $builder)
    {
        $word = $builder->isCompilationEnabled() ? 'compiled' : 'resolved';
        $message = <<<MESSAGE
Entry "DI\Test\IntegrationTest\ErrorMessages\InterfaceFixture" cannot be $word: the class is not instantiable
Full definition:
Object (
    class = #NOT INSTANTIABLE# DI\Test\IntegrationTest\ErrorMessages\InterfaceFixture
    lazy = false
)
MESSAGE;
        $this->expectException(InvalidDefinition::class);
        $this->expectExceptionMessage($message);
        $builder->addDefinitions([
            InterfaceFixture::class => autowire(),
        ]);

        $builder->build()->get(InterfaceFixture::class);
    }

    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_non_existent_class(ContainerBuilder $builder)
    {
        $word = $builder->isCompilationEnabled() ? 'compiled' : 'resolved';
        $message = <<<MESSAGE
Entry "Acme\Foo\Bar\Bar" cannot be $word: the class doesn't exist
Full definition:
Object (
    class = #UNKNOWN# Acme\Foo\Bar\Bar
    lazy = false
)
MESSAGE;
        $this->expectException(InvalidDefinition::class);
        $this->expectExceptionMessage($message);

        $builder->addDefinitions([
            'Acme\Foo\Bar\Bar' => \DI\create(),
        ]);

        $builder->build()->get('Acme\Foo\Bar\Bar');
    }

    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_undefined_constructor_parameter(ContainerBuilder $builder)
    {
        $word = $builder->isCompilationEnabled() ? 'compiled' : 'resolved';
        $message = <<<MESSAGE
Entry "DI\Test\IntegrationTest\ErrorMessages\Buggy1" cannot be $word: Parameter \$bar of __construct() has no value defined or guessable
Full definition:
Object (
    class = DI\Test\IntegrationTest\ErrorMessages\Buggy1
    lazy = false
    __construct(
        \$foo = 'some value'
        \$bar = #UNDEFINED#
        \$default = (default value) 123
    )
)
MESSAGE;
        $this->expectException(InvalidDefinition::class);
        $this->expectExceptionMessage($message);

        $builder->addDefinitions([
            Buggy1::class => autowire()->constructorParameter('foo', 'some value'),
        ]);
        $container = $builder->build();

        $container->get(Buggy1::class);
    }

    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_constructor_injection_of_non_existent_container_entry(ContainerBuilder $builder)
    {
        $this->expectException(DependencyException::class);
        $this->expectExceptionMessage('Error while injecting dependencies into DI\Test\IntegrationTest\ErrorMessages\Buggy2: No entry or class found for \'nonExistentEntry\'');
        $builder->useAttributes(true);
        $builder->build()->get(Buggy2::class);
    }

    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_property_injection_of_non_existent_container_entry(ContainerBuilder $builder)
    {
        $this->expectException(DependencyException::class);
        $this->expectExceptionMessage('Error while injecting in DI\Test\IntegrationTest\ErrorMessages\Buggy3::dependency. No entry or class found for \'namedDependency\'');
        $builder->useAttributes(true);
        $builder->build()->get(Buggy3::class);
    }

    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_setter_injection_of_non_existent_container_entry(ContainerBuilder $builder)
    {
        $this->expectExceptionMessage('Error while injecting dependencies into DI\Test\IntegrationTest\ErrorMessages\Buggy4: No entry or class found for \'nonExistentBean\'');
        $this->expectException(DependencyException::class);
        $builder->useAttributes(true);
        $builder->build()->get(Buggy4::class);
    }

    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_setter_injection_not_type_hinted(ContainerBuilder $builder)
    {
        $word = $builder->isCompilationEnabled() ? 'compiled' : 'resolved';
        $message = <<<MESSAGE
Entry "DI\Test\IntegrationTest\ErrorMessages\Buggy5" cannot be $word: Parameter \$dependency of setDependency() has no value defined or guessable
Full definition:
Object (
    class = DI\Test\IntegrationTest\ErrorMessages\Buggy5
    lazy = false
    setDependency(
        \$dependency = #UNDEFINED#
    )
)
MESSAGE;
        $this->expectException(InvalidDefinition::class);
        $this->expectExceptionMessage($message);

        $builder->useAttributes(true);
        $builder->addDefinitions([
            Buggy5::class => autowire(),
        ]);

        $builder->build()->get(Buggy5::class);
    }

    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_factory_not_callable(ContainerBuilder $builder)
    {
        $this->expectException(InvalidDefinition::class);
        $this->expectExceptionMessage('Entry "foo" cannot be resolved: factory \'bar\' is neither a callable nor a valid container entry');
        $builder->addDefinitions([
            'foo' => \DI\factory('bar'),
        ]);
        $builder->build()->get('foo');
    }
}