testIteratorProxiesIteratorMethods()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NotificationBundle\Tests\Iterator;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\NotificationBundle\Iterator\IteratorProxyMessageIterator;
18
19
/**
20
 * @covers \Sonata\NotificationBundle\Iterator\IteratorProxyMessageIterator
21
 */
22
class IteratorProxyMessageIteratorTest extends TestCase
23
{
24
    public function testIteratorProxiesIteratorMethods(): void
25
    {
26
        $content = [
27
            'foo',
28
            'bar',
29
        ];
30
31
        $actualIterator = $this->createMock('Iterator');
32
        $this->expectIterator($actualIterator, $content, true);
33
34
        $proxy = new IteratorProxyMessageIterator($actualIterator);
35
        foreach ($proxy as $eachKey => $eachEntry) {
36
            $this->assertNotNull($eachKey);
37
            $this->assertNotEmpty($eachEntry);
38
        }
39
    }
40
41
    /**
42
     * @see https://gist.github.com/2852498
43
     */
44
    public function expectIterator($mock, array $content, $withKey = false, $counter = 0): int
45
    {
46
        $mock
47
            ->expects($this->at($counter))
48
            ->method('rewind')
49
        ;
50
51
        foreach ($content as $key => $value) {
52
            $mock
53
                ->expects($this->at(++$counter))
54
                ->method('valid')
55
                ->willReturn(true)
56
            ;
57
58
            $mock
59
                ->expects($this->at(++$counter))
60
                ->method('current')
61
                ->willReturn($value)
62
            ;
63
64
            if ($withKey) {
65
                $mock
66
                    ->expects($this->at(++$counter))
67
                    ->method('key')
68
                    ->willReturn($key)
69
                ;
70
            }
71
72
            $mock
73
                ->expects($this->at(++$counter))
74
                ->method('next')
75
            ;
76
        }
77
78
        $mock
79
            ->expects($this->at(++$counter))
80
            ->method('valid')
81
            ->willReturn(false)
82
        ;
83
84
        return ++$counter;
85
    }
86
}
87