Failed Conditions
Branch master (01ba0d)
by Arnold
06:58 queued 01:45
created

IterableApplyTest::testIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 24
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Jasny\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use function Jasny\iterable_apply;
7
8
/**
9
 * @covers \Jasny\iterable_apply
10
 */
11
class IterableApplyTest extends TestCase
12
{
13
    use LazyExecutionIteratorTrait;
14
15
    public function test()
16
    {
17
        $objects = [
18
            'foo' => new \stdClass(),
19
            'bar' => new \stdClass(),
20
            'qux' => new \stdClass()
21
        ];
22
23
        $iterator = iterable_apply($objects, function($value, $key) {
24
            $value->key = $key;
25
            return 10; // Should be ignored
26
        });
27
28
        $this->assertObjectNotHasAttribute('key', $objects['foo']);
29
30
        $result = iterator_to_array($iterator);
31
32
        $this->assertSame($objects, $result);
33
34
        $this->assertAttributeEquals('foo', 'key', $objects['foo']);
35
        $this->assertAttributeEquals('bar', 'key', $objects['bar']);
36
        $this->assertAttributeEquals('qux', 'key', $objects['qux']);
37
    }
38
39
    public function testIterator()
40
    {
41
        $objects = [
42
            'foo' => new \stdClass(),
43
            'bar' => new \stdClass(),
44
            'qux' => new \stdClass()
45
        ];
46
47
        $inner = new \ArrayIterator($objects);
48
49
        $iterator = iterable_apply($inner, function($value, $key) {
50
            $value->key = $key;
51
            return 10; // Should be ignored
52
        });
53
54
        $this->assertObjectNotHasAttribute('key', $objects['foo']);
55
56
        $result = iterator_to_array($iterator);
57
58
        $this->assertSame($objects, $result);
59
60
        $this->assertAttributeEquals('foo', 'key', $objects['foo']);
61
        $this->assertAttributeEquals('bar', 'key', $objects['bar']);
62
        $this->assertAttributeEquals('qux', 'key', $objects['qux']);
63
    }
64
65
    public function testArrayObject()
66
    {
67
        $objects = [
68
            'foo' => new \stdClass(),
69
            'bar' => new \stdClass(),
70
            'qux' => new \stdClass()
71
        ];
72
73
        $inner = new \ArrayObject($objects);
74
75
        $iterator = iterable_apply($inner, function($value, $key) {
76
            $value->key = $key;
77
            return 10; // Should be ignored
78
        });
79
80
        $this->assertObjectNotHasAttribute('key', $objects['foo']);
81
82
        $result = iterator_to_array($iterator);
83
84
        $this->assertSame($objects, $result);
85
86
        $this->assertAttributeEquals('foo', 'key', $objects['foo']);
87
        $this->assertAttributeEquals('bar', 'key', $objects['bar']);
88
        $this->assertAttributeEquals('qux', 'key', $objects['qux']);
89
    }
90
91
    public function testEmpty()
92
    {
93
        $iterator = iterable_apply(new \EmptyIterator(), function() {});
94
95
        $result = iterator_to_array($iterator);
96
97
        $this->assertEquals([], $result);
98
    }
99
100
    /**
101
     * Test that nothing happens when not iterating
102
     */
103
    public function testLazyExecution()
104
    {
105
        $iterator = $this->createLazyExecutionIterator();
106
107
        iterable_apply($iterator, function() {});
108
109
        $this->assertTrue(true, "No warning");
110
    }
111
}
112