Completed
Push — master ( 17598f...abee1f )
by Kirill
13s queued 11s
created

InjectableTest::testInjectorOuterBinding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Core;
13
14
use Mockery as m;
15
use PHPUnit\Framework\TestCase;
16
use ReflectionClass;
17
use ReflectionMethod;
18
use Spiral\Core\ConfigsInterface;
19
use Spiral\Core\Container;
20
use Spiral\Core\Exception\Container\AutowireException;
21
use Spiral\Core\Exception\Container\InjectionException;
22
use Spiral\Tests\Core\Fixtures\InvalidInjector;
23
use Spiral\Tests\Core\Fixtures\SampleClass;
24
use Spiral\Tests\Core\Fixtures\TestConfig;
25
26
class InjectableTest extends TestCase
27
{
28
    public function testMissingInjector(): void
29
    {
30
        $this->expectExceptionMessage("Undefined class or binding 'Spiral\Core\ConfigsInterface'");
31
        $this->expectException(AutowireException::class);
32
33
        $container = new Container();
34
        $container->get(TestConfig::class);
35
    }
36
37
    public function testInvalidInjector(): void
38
    {
39
        $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an " .
40
                    "instance of InjectorInterface for 'Spiral\Tests\Core\Fixtures\TestConfig'";
41
        $this->expectException(InjectionException::class);
42
        $this->expectExceptionMessage($excepted);
43
44
        $container = new Container();
45
46
        $container->bindInjector(TestConfig::class, InvalidInjector::class);
47
        $container->get(TestConfig::class);
48
    }
49
50
    public function testInvalidInjectorBinding(): void
51
    {
52
        $this->expectException(AutowireException::class);
53
        $this->expectExceptionMessage("Undefined class or binding 'invalid-injector'");
54
55
        $container = new Container();
56
57
        $container->bindInjector(TestConfig::class, 'invalid-injector');
58
        $container->get(TestConfig::class);
59
    }
60
61
    public function testInvalidRuntimeInjector(): void
62
    {
63
        $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an " .
64
            "instance of InjectorInterface for 'Spiral\Tests\Core\Fixtures\TestConfig'";
65
        $this->expectException(InjectionException::class);
66
        $this->expectExceptionMessage($excepted);
67
68
        $container = new Container();
69
70
        $container->bindInjector(TestConfig::class, 'invalid-injector');
71
        $container->bind('invalid-injector', new InvalidInjector());
0 ignored issues
show
Bug introduced by
new Spiral\Tests\Core\Fixtures\InvalidInjector() of type Spiral\Tests\Core\Fixtures\InvalidInjector is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        $container->bind('invalid-injector', /** @scrutinizer ignore-type */ new InvalidInjector());
Loading history...
72
73
        $container->get(TestConfig::class);
74
    }
75
76
    public function testGetInjectors(): void
77
    {
78
        $container = new Container();
79
80
        $container->bindInjector(TestConfig::class, 'invalid-injector');
81
82
        $injectors = $container->getInjectors();
83
84
        $this->assertNotEmpty($injectors);
85
        $this->assertArrayHasKey(TestConfig::class, $injectors);
86
        $this->assertSame('invalid-injector', $injectors[TestConfig::class]);
87
88
        $container->removeInjector(TestConfig::class);
89
        $injectors = $container->getInjectors();
90
91
        $this->assertEmpty($injectors);
92
    }
93
94
    public function testInjectorOuterBinding(): void
95
    {
96
        $this->expectException(AutowireException::class);
97
        $this->expectExceptionMessage("Undefined class or binding 'invalid-configurator'");
98
        $container = new Container();
99
        $container->bind(ConfigsInterface::class, 'invalid-configurator');
100
101
        $container->get(TestConfig::class);
102
    }
103
104
    public function testInvalidInjection(): void
105
    {
106
        $this->expectException(InjectionException::class);
107
        $this->expectExceptionMessage("Invalid injection response for 'Spiral\Tests\Core\Fixtures\TestConfig'");
108
109
        $container = new Container();
110
111
        $configurator = m::mock(ConfigsInterface::class);
112
        $container->bind(ConfigsInterface::class, $configurator);
0 ignored issues
show
Bug introduced by
$configurator of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
        $container->bind(ConfigsInterface::class, /** @scrutinizer ignore-type */ $configurator);
Loading history...
113
114
        $configurator->shouldReceive('createInjection')->andReturn(new SampleClass());
115
116
        $container->get(TestConfig::class);
117
    }
118
119
    public function testInjector(): void
120
    {
121
        $configurator = m::mock(ConfigsInterface::class);
122
        $expected = new TestConfig();
123
124
        $container = new Container();
125
        $container->bind(ConfigsInterface::class, $configurator);
0 ignored issues
show
Bug introduced by
$configurator of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
        $container->bind(ConfigsInterface::class, /** @scrutinizer ignore-type */ $configurator);
Loading history...
126
127
        $configurator->shouldReceive('createInjection')
128
            ->with(m::on(static function (ReflectionClass $r) {
129
                return $r->getName() === TestConfig::class;
130
            }), null)
131
            ->andReturn($expected)
132
        ;
133
134
        $this->assertSame($expected, $container->get(TestConfig::class));
135
    }
136
137
    public function testInjectorWithContext(): void
138
    {
139
        $configurator = m::mock(ConfigsInterface::class);
140
        $expected = new TestConfig();
141
142
        $container = new Container();
143
        $container->bind(ConfigsInterface::class, $configurator);
0 ignored issues
show
Bug introduced by
$configurator of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
        $container->bind(ConfigsInterface::class, /** @scrutinizer ignore-type */ $configurator);
Loading history...
144
145
        $configurator->shouldReceive('createInjection')
146
            ->with(m::on(static function (ReflectionClass $r) {
147
                return $r->getName() === TestConfig::class;
148
            }), 'context')
149
            ->andReturn($expected)
150
        ;
151
152
        $this->assertSame($expected, $container->get(TestConfig::class, 'context'));
153
    }
154
155
    public function testInjectorForMethod(): void
156
    {
157
        $configurator = m::mock(ConfigsInterface::class);
158
        $expected = new TestConfig();
159
160
        $container = new Container();
161
        $container->bind(ConfigsInterface::class, $configurator);
0 ignored issues
show
Bug introduced by
$configurator of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
        $container->bind(ConfigsInterface::class, /** @scrutinizer ignore-type */ $configurator);
Loading history...
162
163
        $configurator->shouldReceive('createInjection')
164
            ->with(m::on(static function (ReflectionClass $r) {
165
                return $r->getName() === TestConfig::class;
166
            }), 'contextArgument')
167
            ->andReturn($expected)
168
        ;
169
170
        $arguments = $container->resolveArguments(new ReflectionMethod($this, 'methodInjection'));
171
172
        $this->assertCount(1, $arguments);
173
        $this->assertSame($expected, $arguments[0]);
174
    }
175
176
    /**
177
     * @param TestConfig $contextArgument
178
     */
179
    private function methodInjection(TestConfig $contextArgument): void
0 ignored issues
show
Unused Code introduced by
The method methodInjection() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
Unused Code introduced by
The parameter $contextArgument is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

179
    private function methodInjection(/** @scrutinizer ignore-unused */ TestConfig $contextArgument): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
180
    {
181
    }
182
}
183