Completed
Push — master ( 5d896f...588e78 )
by Peter
10:12 queued 13s
created

prepareSpecificationStub()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of the Happyr Doctrine Specification package.
5
 *
6
 * (c) Tobias Nyholm <[email protected]>
7
 *     Kacper Gunia <[email protected]>
8
 *     Peter Gribanov <[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 tests\Happyr\DoctrineSpecification\Repository;
15
16
use Doctrine\ORM\AbstractQuery;
17
use Doctrine\ORM\EntityManager;
18
use Doctrine\ORM\Mapping\ClassMetadata;
19
use Doctrine\ORM\NonUniqueResultException as DoctrineNonUniqueResultException;
20
use Doctrine\ORM\NoResultException as DoctrineNoResultException;
21
use Doctrine\ORM\QueryBuilder;
22
use Happyr\DoctrineSpecification\Exception\NonUniqueResultException;
23
use Happyr\DoctrineSpecification\Exception\NoResultException;
24
use Happyr\DoctrineSpecification\Exception\UnexpectedResultException;
25
use Happyr\DoctrineSpecification\Filter\Filter;
26
use Happyr\DoctrineSpecification\Query\QueryModifier;
27
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
28
use Happyr\DoctrineSpecification\Result\ResultModifier;
29
use Happyr\DoctrineSpecification\Specification\Specification;
30
use PhpSpec\ObjectBehavior;
31
use Prophecy\Argument;
32
33
/**
34
 * @mixin EntitySpecificationRepository
35
 */
36
class EntitySpecificationRepositorySpec extends ObjectBehavior
37
{
38
    private $alias = 'e';
39
40
    private $expression = 'expression';
41
42
    private $result = 'result';
43
44
    public function let(EntityManager $entityManager, ClassMetadata $classMetadata)
45
    {
46
        $this->beConstructedWith($entityManager, $classMetadata);
47
    }
48
49
    public function it_should_modify_query(
50
        QueryModifier $specification,
51
        EntityManager $entityManager,
52
        QueryBuilder $qb,
53
        AbstractQuery $query
54
    ) {
55
        $this->prepareEntityManagerStub($entityManager, $qb);
56
        $this->prepareQueryBuilderStub($qb, $query);
57
        $query->execute()->willReturn($this->result);
58
59
        $specification->modify($qb, $this->alias)->shouldBeCalled();
60
61
        $this->match($specification);
62
    }
63
64 View Code Duplication
    public function it_should_apply_filter(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
        Filter $specification,
66
        EntityManager $entityManager,
67
        QueryBuilder $qb,
68
        AbstractQuery $query
69
    ) {
70
        $this->prepareEntityManagerStub($entityManager, $qb);
71
        $this->prepareQueryBuilderStub($qb, $query);
72
        $specification->getFilter($qb, $this->alias)->willReturn($this->expression);
73
74
        $qb->andWhere($this->expression)->willReturn($qb);
75
        $qb->where()->shouldNotBeCalled();
76
77
        $this->match($specification);
78
    }
79
80
    public function it_should_skip_apply_empty_specification(
81
        EntityManager $entityManager,
82
        QueryBuilder $qb,
83
        AbstractQuery $query
84
    ) {
85
        $this->prepareEntityManagerStub($entityManager, $qb);
86
        $this->prepareQueryBuilderStub($qb, $query);
87
88
        $qb->andWhere()->shouldNotBeCalled();
89
        $qb->where()->shouldNotBeCalled();
90
91
        $this->match(null);
92
    }
93
94
    public function it_should_throw_exception_when_apply_not_specification(
95
        EntityManager $entityManager,
96
        QueryBuilder $qb,
97
        AbstractQuery $query
98
    ) {
99
        $this->prepareEntityManagerStub($entityManager, $qb);
100
        $this->prepareQueryBuilderStub($qb, $query);
101
102
        $this->shouldThrow('\InvalidArgumentException')->duringMatch(new \stdClass());
103
        $this->shouldThrow('\InvalidArgumentException')->duringMatch(['fake', 'array']);
104
        $this->shouldThrow('\InvalidArgumentException')->duringMatch('fake');
105
    }
106
107
    public function it_matches_a_specification_with_empty_filter(
108
        Specification $specification,
109
        EntityManager $entityManager,
110
        QueryBuilder $qb,
111
        AbstractQuery $query
112
    ) {
113
        $this->prepareEntityManagerStub($entityManager, $qb);
114
        $this->prepareQueryBuilderStub($qb, $query);
115
        $query->execute()->willReturn($this->result);
116
117
        $qb->andWhere()->shouldNotBeCalled();
118
        $qb->where()->shouldNotBeCalled();
119
120
        $this->match($specification)->shouldReturn($this->result);
121
    }
122
123
    public function it_matches_a_specification_without_result_modifier(
124
        Specification $specification,
125
        EntityManager $entityManager,
126
        QueryBuilder $qb,
127
        AbstractQuery $query
128
    ) {
129
        $this->prepareStubs($specification, $entityManager, $qb, $query);
130
        $query->execute()->willReturn($this->result);
131
132
        $specification->modify($qb, $this->alias)->shouldBeCalled();
133
134
        $this->match($specification)->shouldReturn($this->result);
135
    }
136
137 View Code Duplication
    public function it_matches_a_single_result_without_result_modifier(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
        Specification $specification,
139
        EntityManager $entityManager,
140
        QueryBuilder $qb,
141
        AbstractQuery $query
142
    ) {
143
        $singleResult = new \stdClass();
144
145
        $this->prepareStubs($specification, $entityManager, $qb, $query);
146
147
        $specification->modify($qb, $this->alias)->shouldBeCalled();
148
149
        $query->getSingleResult()->willReturn($singleResult);
150
151
        $this->matchSingleResult($specification)->shouldReturn($singleResult);
152
    }
153
154 View Code Duplication
    public function it_throws_exception_when_expecting_single_result_finding_none_without_result_modifier(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
        Specification $specification,
156
        EntityManager $entityManager,
157
        QueryBuilder $qb,
158
        AbstractQuery $query
159
    ) {
160
        $this->prepareStubs($specification, $entityManager, $qb, $query);
161
162
        $specification->modify($qb, $this->alias)->shouldBeCalled();
163
164
        $query->getSingleResult()->willThrow(new DoctrineNoResultException());
165
166
        $this->shouldThrow(NoResultException::class)->duringMatchSingleResult($specification);
167
    }
168
169 View Code Duplication
    public function it_throws_exception_when_expecting_single_result_finding_multiple_without_result_modifier(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
        Specification $specification,
171
        EntityManager $entityManager,
172
        QueryBuilder $qb,
173
        AbstractQuery $query
174
    ) {
175
        $this->prepareStubs($specification, $entityManager, $qb, $query);
176
177
        $specification->modify($qb, $this->alias)->shouldBeCalled();
178
179
        $query->getSingleResult()->willThrow(new DoctrineNonUniqueResultException());
180
181
        $this->shouldThrow(NonUniqueResultException::class)->duringMatchSingleResult($specification);
182
    }
183
184 View Code Duplication
    public function it_matches_a_single_scalar_result_without_result_modifier(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
        Specification $specification,
186
        EntityManager $entityManager,
187
        QueryBuilder $qb,
188
        AbstractQuery $query
189
    ) {
190
        $singleScalarResult = '1';
191
192
        $this->prepareStubs($specification, $entityManager, $qb, $query);
193
194
        $specification->modify($qb, $this->alias)->shouldBeCalled();
195
196
        $query->getSingleScalarResult()->willReturn($singleScalarResult);
197
198
        $this->matchSingleScalarResult($specification)->shouldReturn($singleScalarResult);
199
    }
200
201 View Code Duplication
    public function it_throws_exception_when_expecting_single_scalar_result_finding_multiple_without_result_modifier(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
        Specification $specification,
203
        EntityManager $entityManager,
204
        QueryBuilder $qb,
205
        AbstractQuery $query
206
    ) {
207
        $this->prepareStubs($specification, $entityManager, $qb, $query);
208
209
        $specification->modify($qb, $this->alias)->shouldBeCalled();
210
211
        $query->getSingleScalarResult()->willThrow(new DoctrineNonUniqueResultException());
212
213
        $this->shouldThrow(NonUniqueResultException::class)->duringMatchSingleScalarResult($specification);
214
    }
215
216
    public function it_matches_a_scalar_result_when_expecting_one_or_null_without_result_modifier(
217
        Specification $specification,
218
        EntityManager $entityManager,
219
        QueryBuilder $qb,
220
        AbstractQuery $query
221
    ) {
222
        $scalarResult = ['1', '2', '3'];
223
224
        $this->prepareStubs($specification, $entityManager, $qb, $query);
225
226
        $specification->modify($qb, $this->alias)->shouldBeCalled();
227
228
        $query->getScalarResult()->willReturn($scalarResult);
229
230
        $this->matchScalarResult($specification)->shouldReturn($scalarResult);
231
    }
232
233 View Code Duplication
    public function it_matches_a_single_result_when_expecting_one_or_null_without_result_modifier(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
        Specification $specification,
235
        EntityManager $entityManager,
236
        QueryBuilder $qb,
237
        AbstractQuery $query
238
    ) {
239
        $singleResult = new \stdClass();
240
241
        $this->prepareStubs($specification, $entityManager, $qb, $query);
242
243
        $specification->modify($qb, $this->alias)->shouldBeCalled();
244
245
        $query->getSingleResult()->willReturn($singleResult);
246
247
        $this->matchOneOrNullResult($specification)->shouldReturn($singleResult);
248
    }
249
250 View Code Duplication
    public function it_matches_null_when_expecting_one_or_null_without_result_modifier(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
251
        Specification $specification,
252
        EntityManager $entityManager,
253
        QueryBuilder $qb,
254
        AbstractQuery $query
255
    ) {
256
        $this->prepareStubs($specification, $entityManager, $qb, $query);
257
258
        $specification->modify($qb, $this->alias)->shouldBeCalled();
259
260
        $query->getSingleResult()->willThrow(new DoctrineNonUniqueResultException());
261
262
        $this->shouldThrow(NonUniqueResultException::class)->duringMatchOneOrNullResult($specification);
263
    }
264
265 View Code Duplication
    public function it_throws_exception_when_expecting_one_or_null_finding_multiple_without_result_modifier(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
266
        Specification $specification,
267
        EntityManager $entityManager,
268
        QueryBuilder $qb,
269
        AbstractQuery $query
270
    ) {
271
        $this->prepareStubs($specification, $entityManager, $qb, $query);
272
273
        $specification->modify($qb, $this->alias)->shouldBeCalled();
274
275
        $query->getSingleResult()->willThrow(new DoctrineNonUniqueResultException());
276
277
        $this->shouldThrow(UnexpectedResultException::class)->duringMatchOneOrNullResult($specification);
278
    }
279
280 View Code Duplication
    public function it_matches_a_specification_with_result_modifier(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
        Specification $specification,
282
        EntityManager $entityManager,
283
        QueryBuilder $qb,
284
        AbstractQuery $query,
285
        ResultModifier $modifier
286
    ) {
287
        $this->prepareStubs($specification, $entityManager, $qb, $query);
288
        $query->execute()->willReturn($this->result);
289
290
        $specification->modify($qb, $this->alias)->shouldBeCalled();
291
        $modifier->modify($query)->shouldBeCalled();
292
293
        $this->match($specification, $modifier)->shouldReturn($this->result);
294
    }
295
296
    private function prepareStubs(Specification $specification, EntityManager $entityManager, QueryBuilder $qb, AbstractQuery $query)
297
    {
298
        $this->prepareEntityManagerStub($entityManager, $qb);
299
        $this->prepareSpecificationStub($specification, $qb);
300
        $this->prepareQueryBuilderStub($qb, $query);
301
    }
302
303
    private function prepareEntityManagerStub(EntityManager $entityManager, QueryBuilder $qb)
304
    {
305
        $entityManager->createQueryBuilder()->willReturn($qb);
306
    }
307
308
    private function prepareSpecificationStub(Specification $specification, QueryBuilder $qb)
309
    {
310
        $specification->getFilter($qb, $this->alias)->willReturn($this->expression);
311
    }
312
313
    private function prepareQueryBuilderStub(QueryBuilder $qb, Query $query)
314
    {
315
        $qb->from(Argument::any(), $this->alias, null)->willReturn($qb);
316
        $qb->select($this->alias)->willReturn($qb);
317
        $qb->andWhere($this->expression)->willReturn($qb);
318
        $qb->getQuery()->willReturn($query);
319
    }
320
}
321