Completed
Push — remove-pamil-phpspec-generator ( 3b9465...f51036 )
by Kamil
24:18 queued 05:52
created

SuiteFactorySpec::createGenerator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace spec\Sylius\Bundle\FixturesBundle\Suite;
13
14
use PhpSpec\ObjectBehavior;
15
use PhpSpec\Wrapper\Collaborator;
16
use Prophecy\Argument;
17
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
18
use Sylius\Bundle\FixturesBundle\Fixture\FixtureRegistryInterface;
19
use Sylius\Bundle\FixturesBundle\Listener\ListenerInterface;
20
use Sylius\Bundle\FixturesBundle\Listener\ListenerRegistryInterface;
21
use Sylius\Bundle\FixturesBundle\Suite\SuiteFactory;
22
use Sylius\Bundle\FixturesBundle\Suite\SuiteFactoryInterface;
23
use Symfony\Component\Config\Definition\Processor;
24
25
/**
26
 * @author Kamil Kokot <[email protected]>
27
 */
28
final class SuiteFactorySpec extends ObjectBehavior
29
{
30
    function let(FixtureRegistryInterface $fixtureRegistry, ListenerRegistryInterface $listenerRegistry, Processor $optionsProcessor)
31
    {
32
        $this->beConstructedWith($fixtureRegistry, $listenerRegistry, $optionsProcessor);
33
    }
34
35
    function it_is_initializable()
36
    {
37
        $this->shouldHaveType(SuiteFactory::class);
38
    }
39
40
    function it_implements_suite_factory_interface()
41
    {
42
        $this->shouldImplement(SuiteFactoryInterface::class);
43
    }
44
45
    function it_creates_a_new_empty_suite()
46
    {
47
        $suite = $this->createSuite('suite_name', ['listeners' => [], 'fixtures' => []]);
48
49
        $suite->getName()->shouldReturn('suite_name');
50
        $suite->getFixtures()->shouldIterateAs([]);
51
    }
52
53
    function it_creates_a_new_suite_with_fixtures(
54
        FixtureRegistryInterface $fixtureRegistry,
55
        Processor $optionsProcessor,
56
        FixtureInterface $firstFixture,
57
        FixtureInterface $secondFixture
58
    ) {
59
        $fixtureRegistry->getFixture('first_fixture')->willReturn($firstFixture);
60
        $fixtureRegistry->getFixture('second_fixture')->willReturn($secondFixture);
61
62
        $optionsProcessor->processConfiguration($firstFixture, [[]])->willReturn([]);
63
        $optionsProcessor->processConfiguration($secondFixture, [[]])->willReturn([]);
64
65
        $suite = $this->createSuite('suite_name', ['listeners' => [], 'fixtures' => [
66
            'first_fixture' => ['name' => 'first_fixture', 'options' => [[]]],
67
            'second_fixture' => ['name' => 'second_fixture', 'options' => [[]]],
68
        ]]);
69
70
        $suite->getName()->shouldReturn('suite_name');
71
        $suite->getFixtures()->shouldIterateAs($this->createGenerator($firstFixture, $secondFixture));
72
    }
73
74
    function it_creates_a_new_suite_with_fixtures_based_on_its_name_rather_than_alias(
75
        FixtureRegistryInterface $fixtureRegistry,
76
        Processor $optionsProcessor,
77
        FixtureInterface $fixture
78
    ) {
79
        $fixtureRegistry->getFixture('fixture_name')->shouldBeCalled()->willReturn($fixture);
80
        $fixtureRegistry->getFixture('fixture_alias')->shouldNotBeCalled();
81
82
        $optionsProcessor->processConfiguration($fixture, [[]])->willReturn([]);
83
84
        $suite = $this->createSuite('suite_name', ['listeners' => [], 'fixtures' => [
85
            'fixture_alias' => ['name' => 'fixture_name', 'options' => [[]]],
86
        ]]);
87
88
        $suite->getName()->shouldReturn('suite_name');
89
        $suite->getFixtures()->shouldIterateAs($this->createGenerator($fixture));
90
    }
91
92
    function it_creates_a_new_suite_with_prioritized_fixtures(
93
        FixtureRegistryInterface $fixtureRegistry,
94
        Processor $optionsProcessor,
95
        FixtureInterface $fixture,
96
        FixtureInterface $higherPriorityFixture
97
    ) {
98
        $fixtureRegistry->getFixture('fixture')->willReturn($fixture);
99
        $fixtureRegistry->getFixture('higher_priority_fixture')->willReturn($higherPriorityFixture);
100
101
        $optionsProcessor->processConfiguration($fixture, [[]])->willReturn([]);
102
        $optionsProcessor->processConfiguration($higherPriorityFixture, [[]])->willReturn([]);
103
104
        $suite = $this->createSuite('suite_name', ['listeners' => [], 'fixtures' => [
105
            'fixture' => ['name' => 'fixture', 'priority' => 5, 'options' => [[]]],
106
            'higher_priority_fixture' => ['name' => 'higher_priority_fixture', 'priority' => 10, 'options' => [[]]],
107
        ]]);
108
109
        $suite->getName()->shouldReturn('suite_name');
110
        $suite->getFixtures()->shouldIterateAs($this->createGenerator($higherPriorityFixture, $fixture));
111
    }
112
113
    function it_creates_a_new_suite_with_customized_fixture(
114
        FixtureRegistryInterface $fixtureRegistry,
115
        Processor $optionsProcessor,
116
        FixtureInterface $fixture
117
    ) {
118
        $fixtureRegistry->getFixture('fixture')->willReturn($fixture);
119
120
        $optionsProcessor->processConfiguration($fixture, [['fixture_option' => 'fixture_value']])->willReturn(['fixture_option' => 'fixture_value']);
121
122
        $suite = $this->createSuite('suite_name', ['listeners' => [], 'fixtures' => [
123
            'fixture' => ['name' => 'fixture', 'options' => [['fixture_option' => 'fixture_value']]],
124
        ]]);
125
126
        $suite->getName()->shouldReturn('suite_name');
127
        $suite->getFixtures()->shouldHaveKeyWithValue($fixture, ['fixture_option' => 'fixture_value']);
128
    }
129
130
    function it_creates_a_new_suite_with_listeners(
131
        ListenerRegistryInterface $listenerRegistry,
132
        Processor $optionsProcessor,
133
        ListenerInterface $firstListener,
134
        ListenerInterface $secondListener
135
    ) {
136
        $listenerRegistry->getListener('first_listener')->willReturn($firstListener);
137
        $listenerRegistry->getListener('second_listener')->willReturn($secondListener);
138
139
        $optionsProcessor->processConfiguration($firstListener, [[]])->willReturn([]);
140
        $optionsProcessor->processConfiguration($secondListener, [[]])->willReturn([]);
141
142
        $suite = $this->createSuite('suite_name', ['fixtures' => [], 'listeners' => [
143
            'first_listener' => ['options' => [[]]],
144
            'second_listener' => ['options' => [[]]],
145
        ]]);
146
147
        $suite->getName()->shouldReturn('suite_name');
148
        $suite->getListeners()->shouldIterateAs($this->createGenerator($firstListener, $secondListener));
149
    }
150
151
    function it_creates_a_new_suite_with_prioritized_listeners(
152
        ListenerRegistryInterface $listenerRegistry,
153
        Processor $optionsProcessor,
154
        ListenerInterface $listener,
155
        ListenerInterface $higherPriorityListener
156
    ) {
157
        $listenerRegistry->getListener('listener')->willReturn($listener);
158
        $listenerRegistry->getListener('higher_priority_listener')->willReturn($higherPriorityListener);
159
160
        $optionsProcessor->processConfiguration($listener, [[]])->willReturn([]);
161
        $optionsProcessor->processConfiguration($higherPriorityListener, [[]])->willReturn([]);
162
163
        $suite = $this->createSuite('suite_name', ['fixtures' => [], 'listeners' => [
164
            'listener' => ['priority' => 5, 'options' => [[]]],
165
            'higher_priority_listener' => ['priority' => 10, 'options' => [[]]],
166
        ]]);
167
168
        $suite->getName()->shouldReturn('suite_name');
169
        $suite->getListeners()->shouldIterateAs($this->createGenerator($higherPriorityListener, $listener));
170
    }
171
172
    function it_creates_a_new_suite_with_customized_listener(
173
        ListenerRegistryInterface $listenerRegistry,
174
        Processor $optionsProcessor,
175
        ListenerInterface $listener
176
    ) {
177
        $listenerRegistry->getListener('listener')->willReturn($listener);
178
179
        $optionsProcessor->processConfiguration($listener, [['listener_option' => 'listener_value']])->willReturn(['listener_option' => 'listener_value']);
180
181
        $suite = $this->createSuite('suite_name', ['fixtures' => [], 'listeners' => [
182
            'listener' => ['options' => [['listener_option' => 'listener_value']]],
183
        ]]);
184
185
        $suite->getName()->shouldReturn('suite_name');
186
        $suite->getListeners()->shouldHaveKeyWithValue($listener, ['listener_option' => 'listener_value']);
187
    }
188
189
    function it_throws_an_exception_if_suite_options_does_not_have_fixtures()
190
    {
191
        $this->shouldThrow(\InvalidArgumentException::class)->during('createSuite', ['suite_name', ['listeners' => []]]);
192
    }
193
194
    function it_throws_an_exception_if_suite_options_does_not_have_listeners()
195
    {
196
        $this->shouldThrow(\InvalidArgumentException::class)->during('createSuite', ['suite_name', ['fixtures' => []]]);
197
    }
198
199
    function it_throws_an_exception_if_fixture_does_not_have_options_defined()
200
    {
201
        $this->shouldThrow(\InvalidArgumentException::class)->during('createSuite', ['suite_name', ['listeners' => [], 'fixtures' => [
202
            'fixture' => ['name' => 'fixture'],
203
        ]]]);
204
    }
205
206
    function it_throws_an_exception_if_fixture_does_not_have_name_defined()
207
    {
208
        $this->shouldThrow(\InvalidArgumentException::class)->during('createSuite', ['suite_name', ['listeners' => [], 'fixtures' => [
209
            'fixture' => ['options' => []],
210
        ]]]);
211
    }
212
213
    function it_throws_an_exception_if_listener_does_not_have_options_defined()
214
    {
215
        $this->shouldThrow(\InvalidArgumentException::class)->during('createSuite', ['suite_name', ['fixtures' => [], 'listeners' => [
216
            'listener' => [],
217
        ]]]);
218
    }
219
220
    /**
221
     * @param Collaborator[] ...$collaborators
222
     *
223
     * @return \Generator
224
     */
0 ignored issues
show
Documentation introduced by
Should the type for parameter $collaborators not be Collaborator[][]?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
225
    private function createGenerator(Collaborator ...$collaborators) {
226
        foreach ($collaborators as $collaborator) {
227
            yield $collaborator->getWrappedObject() => [];
228
        }
229
    }
230
}
231