InvokableSpec::it_provides_a_set_of_values()   A
last analyzed

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 Knp\DictionaryBundle\Dictionary;
8
use Knp\DictionaryBundle\Dictionary\Invokable;
9
use PhpSpec\ObjectBehavior;
10
11
final class InvokableSpec extends ObjectBehavior
12
{
13
    /**
14
     * @var bool
15
     */
16
    private $executed;
17
18
    function let()
19
    {
20
        $this->executed = false;
21
22
        $context = $this;
23
24
        $this->beConstructedWith('foo', fn () => $context->execution());
25
    }
26
27
    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...
28
    {
29
        $this->shouldHaveType(Invokable::class);
30
    }
31
32
    function it_is_a_dictionary()
33
    {
34
        $this->shouldImplement(Dictionary::class);
35
    }
36
37
    function it_supports_callable_arguments()
38
    {
39
        $this->beConstructedWith(
40
            'baz',
41
            fn () => $this->execution(...\func_get_args()),
42
            [['foo' => 2, 'bar' => 1, 'baz' => 0]]
43
        );
44
45
        $this->getValues()->shouldReturn(['foo' => 2, 'bar' => 1, 'baz' => 0]);
0 ignored issues
show
Bug introduced by
The method getValues() does not exist on spec\Knp\DictionaryBundle\Dictionary\InvokableSpec. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

45
        $this->/** @scrutinizer ignore-call */ 
46
               getValues()->shouldReturn(['foo' => 2, 'bar' => 1, 'baz' => 0]);
Loading history...
46
    }
47
48
    function it_supports_some_array_access_functions()
49
    {
50
        $dictionary = $this->getWrappedObject();
51
52
        $this['foo']->shouldBe(0);
53
        $this->shouldHaveKey('foo');
54
55
        $this['foo'] = 'test';
56
        $this['foo']->shouldBe('test');
57
58
        unset($dictionary['foo']);
59
        $this->shouldNotHaveKey('foo');
60
    }
61
62
    function it_provides_a_set_of_values()
63
    {
64
        $this->getValues()->shouldReturn([
65
            'foo' => 0,
66
            'bar' => 1,
67
            'baz' => 2,
68
        ]);
69
    }
70
71
    function it_provides_a_set_of_keys()
72
    {
73
        $this->getKeys()->shouldReturn([
0 ignored issues
show
Bug introduced by
The method getKeys() does not exist on spec\Knp\DictionaryBundle\Dictionary\InvokableSpec. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

73
        $this->/** @scrutinizer ignore-call */ 
74
               getKeys()->shouldReturn([
Loading history...
74
            'foo',
75
            'bar',
76
            'baz',
77
        ]);
78
    }
79
80
    function it_is_hydrated_just_once()
81
    {
82
        $this->getValues()->shouldReturn([
83
            'foo' => 0,
84
            'bar' => 1,
85
            'baz' => 2,
86
        ]);
87
88
        $this->getValues()->shouldReturn([
89
            'foo' => 0,
90
            'bar' => 1,
91
            'baz' => 2,
92
        ]);
93
    }
94
95
    function it_access_to_value_like_an_array()
96
    {
97
        $this['foo']->shouldReturn(0);
98
        $this['bar']->shouldReturn(1);
99
        $this['baz']->shouldReturn(2);
100
    }
101
102
    function it_throws_an_exception_if_callable_returns_somthing_else_than_an_array_or_an_array_access($nothing)
0 ignored issues
show
Unused Code introduced by
The parameter $nothing is not used and could be removed. ( Ignorable by Annotation )

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

102
    function it_throws_an_exception_if_callable_returns_somthing_else_than_an_array_or_an_array_access(/** @scrutinizer ignore-unused */ $nothing)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    {
104
        $this->beConstructedWith('foo', function (): void {});
105
106
        $this
107
            ->shouldThrow(new \InvalidArgumentException('Dictionary callable must return an array or an instance of ArrayAccess.'))
108
            ->duringGetValues()
109
        ;
110
    }
111
112
    function it_generates_an_iterator()
113
    {
114
        $this->shouldIterateLike([
115
            'foo' => 0,
116
            'bar' => 1,
117
            'baz' => 2,
118
        ]);
119
    }
120
121
    function its_getname_should_return_dictionary_name()
122
    {
123
        $this->getName()->shouldReturn('foo');
0 ignored issues
show
Bug introduced by
The method getName() does not exist on spec\Knp\DictionaryBundle\Dictionary\InvokableSpec. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

123
        $this->/** @scrutinizer ignore-call */ 
124
               getName()->shouldReturn('foo');
Loading history...
124
    }
125
126
    function it_returns_the_count_of_elements()
127
    {
128
        $this->count()->shouldReturn(3);
0 ignored issues
show
Bug introduced by
The method count() does not exist on spec\Knp\DictionaryBundle\Dictionary\InvokableSpec. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

128
        $this->/** @scrutinizer ignore-call */ 
129
               count()->shouldReturn(3);
Loading history...
129
    }
130
131
    private function execution()
132
    {
133
        if (!$this->executed) {
134
            $this->executed = true;
135
            $args           = \func_get_args();
136
137
            return !empty($args) ? current($args) : [
138
                'foo' => 0,
139
                'bar' => 1,
140
                'baz' => 2,
141
            ];
142
        }
143
144
        throw new \Exception('Executed twice.');
145
    }
146
}
147