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

EntryLoaderTest::testIteratorToArrayRewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jasny\Container\Tests\Loader;
4
5
use Jasny\Container\Loader\EntryLoader;
6
use org\bovigo\vfs\vfsStream;
7
use PHPUnit\Framework\TestCase;
8
9
/**
10
 * @covers \Jasny\Container\Loader\EntryLoader
11
 * @covers \Jasny\Container\Loader\AbstractLoader
12
 */
13
class EntryLoaderTest extends TestCase
14
{
15
    protected $root;
16
17
    public function setUp()
18
    {
19
        $this->root = vfsStream::setup();
20
        vfsStream::create([
21
            'r.php' => "<?php return ['red' => function() { return 22; }];",
22
            'g.php' => "<?php return ['green' => function() { return 125; }];",
23
            'b.php' => "<?php return ['blue' => function() { return 48; }];",
24
            'multi.php' => "<?php return ['one' => function() { return 'I'; }, 'two' => function() { return 'II'; }];",
25
            'empty.php' => "<?php return [];",
26
            'blank.php' => "<?php "
27
        ]);
28
    }
29
30
    public function testIterate()
31
    {
32
        $iterator = new \ArrayIterator(['vfs://root/r.php', 'vfs://root/g.php', 'vfs://root/b.php']);
33
        $entries = new EntryLoader($iterator);
34
35
        $result = [];
36
37
        foreach ($entries as $key => $callback) {
38
            $this->assertInternalType('string', $key);
39
            $this->assertInstanceOf(\Closure::class, $callback);
40
41
            $result[$key] = $callback();
42
        }
43
44
        $this->assertEquals(['red' => 22, 'green' => 125, 'blue' => 48], $result);
45
    }
46
47
    public function testIterateMulti()
48
    {
49
        $iterator = new \ArrayIterator(['vfs://root/r.php', 'vfs://root/multi.php', 'vfs://root/g.php']);
50
        $entries = new EntryLoader($iterator);
51
52
        $result = [];
53
54
        foreach ($entries as $key => $callback) {
55
            $this->assertInternalType('string', $key);
56
            $this->assertInstanceOf(\Closure::class, $callback);
57
58
            $result[$key] = $callback();
59
        }
60
61
        $this->assertEquals(['red' => 22, 'one' => 'I', 'two' => 'II', 'green' => 125], $result);
62
    }
63
64
    public function testIterateEmpty()
65
    {
66
        $iterator = new \ArrayIterator(['vfs://root/r.php', 'vfs://root/empty.php', 'vfs://root/g.php']);
67
        $entries = new EntryLoader($iterator);
68
69
        $result = [];
70
71
        foreach ($entries as $key => $callback) {
72
            $this->assertInternalType('string', $key);
73
            $this->assertInstanceOf(\Closure::class, $callback);
74
75
            $result[$key] = $callback();
76
        }
77
78
        $this->assertEquals(['red' => 22, 'green' => 125], $result);
79
    }
80
81
    public function testIteratorToArray()
82
    {
83
        $iterator = new \ArrayIterator(['vfs://root/r.php', 'vfs://root/g.php', 'vfs://root/b.php']);
84
        $entries = new EntryLoader($iterator);
85
86
        $result = iterator_to_array($entries);
87
88
        $this->assertEquals(['red', 'green', 'blue'], array_keys($result));
89
        $this->assertContainsOnlyInstancesOf(\Closure::class, $result);
90
    }
91
92
    public function testIteratorToArrayEmpty()
93
    {
94
        $iterator = new \ArrayIterator([]);
95
        $entries = new EntryLoader($iterator);
96
97
        $result = iterator_to_array($entries);
98
99
        $this->assertEquals([], $result);
100
    }
101
102
    public function testIteratorToArrayRewind()
103
    {
104
        $iterator = new \ArrayIterator(['vfs://root/r.php', 'vfs://root/g.php', 'vfs://root/b.php']);
105
        $entries = new EntryLoader($iterator);
106
107
        $first = iterator_to_array($entries);
108
        $this->assertEquals(['red', 'green', 'blue'], array_keys($first));
109
110
        $second = iterator_to_array($entries);
111
        $this->assertEquals(['red', 'green', 'blue'], array_keys($second));
112
    }
113
114
    /**
115
     * @expectedException \UnexpectedValueException
116
     * @expectedExceptionMessage Failed to load container entries from 'vfs://root/blank.php': Invalid or no return value
117
     */
118
    public function testIterateUnexpectedValue()
119
    {
120
        $iterator = new \ArrayIterator(['vfs://root/r.php', 'vfs://root/blank.php', 'vfs://root/g.php']);
121
        $entries = new EntryLoader($iterator);
122
123
        foreach ($entries as $key => $callback) {
124
            $callback();
125
        }
126
    }
127
128
    public function testGetInnerIterator()
129
    {
130
        $iterator = new \ArrayIterator();
131
        $entries = new EntryLoader($iterator);
132
133
        $this->assertSame($iterator, $entries->getInnerIterator());
134
    }
135
}
136