Passed
Pull Request — master (#193)
by Pierre
02:59
created

IteratorSpec::it_provides_a_set_of_values()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace spec\Knp\DictionaryBundle\Dictionary;
6
7
use Exception;
8
use Knp\DictionaryBundle\Dictionary\Iterator;
9
use PhpSpec\ObjectBehavior;
10
use Webmozart\Assert\Assert;
11
12
final class IteratorSpec extends ObjectBehavior
13
{
14
    use DictionaryBehavior;
15
16
    /**
17
     * @var bool
18
     */
19
    private $executed;
20
21
    function let()
22
    {
23
        $this->executed = false;
24
25
        $this->beConstructedWith('foo', $this->execution());
26
    }
27
28
    function it_is_initializable()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
29
    {
30
        $this->shouldHaveType(Iterator::class);
31
    }
32
33
    function it_supports_some_array_access_functions()
34
    {
35
        $dictionary = $this->getWrappedObject();
36
37
        $this['foo']->shouldBe(0);
38
        $this->shouldHaveKey('foo');
39
40
        $this['foo'] = 'test';
41
        $this['foo']->shouldBe('test');
42
43
        unset($dictionary['foo']);
44
        $this->shouldNotHaveKey('foo');
45
    }
46
47
    function it_is_hydrated_just_once()
48
    {
49
        Assert::false($this->executed);
50
51
        $this->shouldYieldLike([
52
            'foo' => 0,
53
            'bar' => 1,
54
            'baz' => 2,
55
        ]);
56
57
        Assert::true($this->executed);
58
59
        $this->shouldYieldLike([
60
            'foo' => 0,
61
            'bar' => 1,
62
            'baz' => 2,
63
        ]);
64
65
        Assert::true($this->executed);
66
    }
67
68
    protected function getExpectedResult(): array
69
    {
70
        return [
71
            'foo' => 0,
72
            'bar' => 1,
73
            'baz' => 2,
74
        ];
75
    }
76
77
    protected function getExpectedName(): string
78
    {
79
        return 'foo';
80
    }
81
82
    private function execution(): iterable
83
    {
84
        if (!$this->executed) {
85
            $this->executed = true;
86
87
            yield 'foo' => 0;
88
89
            yield 'bar' => 1;
90
91
            yield 'baz' => 2;
92
93
            return;
94
        }
95
96
        throw new Exception('Executed twice.');
97
    }
98
}
99