Failed Conditions
Push — master ( 8fb951...fb1394 )
by Arnold
03:15
created

ClassLoaderTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 148
rs 10
c 0
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetInnerIterator() 0 6 1
A testIteratorToArrayEmpty() 0 8 1
A testIteratorToArray() 0 15 1
A testIterateSkip() 0 25 3
A testIterateCallback() 0 21 2
A testIterate() 0 25 2
A testIterateUnexpectedValue() 0 11 2
A testIteratorToArrayRewind() 0 16 1
1
<?php
2
3
namespace Jasny\Container\Tests\Loader;
4
5
use Jasny\Autowire\AutowireInterface;
6
use Jasny\Container\Loader\ClassLoader;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Container\ContainerInterface;
9
10
/**
11
 * @covers \Jasny\Container\Loader\ClassLoader
12
 * @covers \Jasny\Container\Loader\AbstractLoader
13
 */
14
class ClassLoaderTest extends TestCase
15
{
16
    public function testIterate()
17
    {
18
        $entries = new ClassLoader(new \ArrayIterator(['Foo', 'Bar', 'App\Qux']));
19
20
        $foo = new \stdClass();
21
        $bar = new \stdClass();
22
        $qux = new \stdClass();
23
24
        $autowire = $this->createMock(AutowireInterface::class);
25
        $autowire->expects($this->exactly(3))->method('instantiate')
26
            ->withConsecutive(['Foo'], ['Bar'], ['App\Qux'])->willReturnOnConsecutiveCalls($foo, $bar, $qux);
27
28
        $container = $this->createMock(ContainerInterface::class);
29
        $container->method('get')->with('Jasny\Autowire\AutowireInterface')->willReturn($autowire);
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

29
        $container->/** @scrutinizer ignore-call */ 
30
                    method('get')->with('Jasny\Autowire\AutowireInterface')->willReturn($autowire);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
31
        $result = [];
32
33
        foreach ($entries as $class => $callback) {
34
            $this->assertInternalType('string', $class);
35
            $this->assertInstanceOf(\Closure::class, $callback);
36
37
            $result[$class] = $callback($container);
38
        }
39
40
        $this->assertSame(['Foo' => $foo, 'Bar' => $bar, 'App\Qux' => $qux], $result);
41
    }
42
43
    public function testIterateCallback()
44
    {
45
        $apply = function ($class) {
46
            return [
47
                "$class.one" => function () use ($class) { return substr($class, 0, 1) . "1"; },
48
                "$class.two" => function () use ($class) { return substr($class, 0, 1) . "2"; }
49
            ];
50
        };
51
52
        $entries = new ClassLoader(new \ArrayIterator(['Foo', 'Bar']), $apply);
53
54
        $result = [];
55
56
        foreach ($entries as $class => $callback) {
57
            $this->assertInternalType('string', $class);
58
            $this->assertInstanceOf(\Closure::class, $callback);
59
60
            $result[$class] = $callback();
61
        }
62
63
        $this->assertSame(['Foo.one' => "F1", "Foo.two" => "F2", 'Bar.one' => "B1", 'Bar.two' => "B2"], $result);
64
    }
65
66
    public function testIterateSkip()
67
    {
68
        $apply = function ($class) {
69
            if ($class === 'Nop') {
70
                return [];
71
            }
72
73
            return [
74
                "$class.one" => function () use ($class) { return substr($class, 0, 1) . "1"; },
75
                "$class.two" => function () use ($class) { return substr($class, 0, 1) . "2"; }
76
            ];
77
        };
78
79
        $entries = new ClassLoader(new \ArrayIterator(['Foo', 'Nop', 'Bar']), $apply);
80
81
        $result = [];
82
83
        foreach ($entries as $class => $callback) {
84
            $this->assertInternalType('string', $class);
85
            $this->assertInstanceOf(\Closure::class, $callback);
86
87
            $result[$class] = $callback();
88
        }
89
90
        $this->assertSame(['Foo.one' => "F1", "Foo.two" => "F2", 'Bar.one' => "B1", 'Bar.two' => "B2"], $result);
91
    }
92
93
    public function testIteratorToArray()
94
    {
95
        $apply = function ($class) {
96
            return [
97
                "$class.one" => function () use ($class) { return substr($class, 0, 1) . "1"; },
98
                "$class.two" => function () use ($class) { return substr($class, 0, 1) . "2"; }
99
            ];
100
        };
101
102
        $entries = new ClassLoader(new \ArrayIterator(['Foo', 'Bar']), $apply);
103
104
        $result = iterator_to_array($entries);
105
106
        $this->assertEquals(['Foo.one', 'Foo.two', 'Bar.one', 'Bar.two'], array_keys($result));
107
        $this->assertContainsOnlyInstancesOf(\Closure::class, $result);
108
    }
109
110
    public function testIteratorToArrayEmpty()
111
    {
112
        $iterator = new \ArrayIterator([]);
113
        $entries = new ClassLoader($iterator);
114
115
        $result = iterator_to_array($entries);
116
117
        $this->assertEquals([], $result);
118
    }
119
120
    public function testIteratorToArrayRewind()
121
    {
122
        $apply = function ($class) {
123
            return [
124
                "$class.one" => function () use ($class) { return substr($class, 0, 1) . "1"; },
125
                "$class.two" => function () use ($class) { return substr($class, 0, 1) . "2"; }
126
            ];
127
        };
128
129
        $entries = new ClassLoader(new \ArrayIterator(['Foo', 'Bar']), $apply);
130
131
        $first = iterator_to_array($entries);
132
        $this->assertEquals(['Foo.one', 'Foo.two', 'Bar.one', 'Bar.two'], array_keys($first));
133
134
        $second = iterator_to_array($entries);
135
        $this->assertEquals(['Foo.one', 'Foo.two', 'Bar.one', 'Bar.two'], array_keys($second));
136
    }
137
138
    /**
139
     * @expectedException \UnexpectedValueException
140
     * @expectedExceptionMessage Failed to load container entries for 'Foo': Expected array, callback returned Closure object
141
     */
142
    public function testIterateUnexpectedValue()
143
    {
144
        $apply = function () {
145
            return function() {};
146
        };
147
148
        $iterator = new \ArrayIterator(['Foo']);
149
        $entries = new ClassLoader($iterator, $apply);
150
151
        foreach ($entries as $key => $callback) {
152
            $callback();
153
        }
154
    }
155
156
    public function testGetInnerIterator()
157
    {
158
        $iterator = new \ArrayIterator([]);
159
        $entries = new ClassLoader($iterator);
160
161
        $this->assertSame($iterator, $entries->getInnerIterator());
162
    }
163
}
164