1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of the Happyr Doctrine Specification package. |
6
|
|
|
* |
7
|
|
|
* (c) Tobias Nyholm <[email protected]> |
8
|
|
|
* Kacper Gunia <[email protected]> |
9
|
|
|
* Peter Gribanov <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace tests\Happyr\DoctrineSpecification; |
16
|
|
|
|
17
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
18
|
|
|
use Doctrine\ORM\QueryBuilder; |
19
|
|
|
use Happyr\DoctrineSpecification\DQLContextResolver; |
20
|
|
|
use PhpSpec\ObjectBehavior; |
21
|
|
|
use Prophecy\Argument; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @mixin DQLContextResolver |
25
|
|
|
*/ |
26
|
|
|
final class DQLContextResolverSpec extends ObjectBehavior |
27
|
|
|
{ |
28
|
|
|
public function let(): void |
29
|
|
|
{ |
30
|
|
|
$this::enableDeadJoinsProtection(); |
31
|
|
|
$this::enableConflictProtection(); |
32
|
|
|
$this::enableAutoJoining(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function it_resolve_not_joined_aliases(QueryBuilder $qb): void |
36
|
|
|
{ |
37
|
|
|
$this::disableDeadJoinsProtection(); |
38
|
|
|
|
39
|
|
|
$this::resolveAlias($qb, 'root')->shouldBe('root'); |
40
|
|
|
$this::resolveAlias($qb, 'root.contestant')->shouldBe('contestant'); |
41
|
|
|
$this::resolveAlias($qb, 'root.contestant.contest')->shouldBe('contest'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function it_join_entity_without_conflict_protection(QueryBuilder $qb): void |
45
|
|
|
{ |
46
|
|
|
$this::disableConflictProtection(); |
47
|
|
|
|
48
|
|
|
$qb->getAllAliases()->willReturn(['contestant']); |
49
|
|
|
$qb->getDQLPart('join')->willReturn([ |
50
|
|
|
'root' => [ |
51
|
|
|
new Join(Join::INNER_JOIN, 'root.contestant', 'contestant'), |
52
|
|
|
], |
53
|
|
|
]); |
54
|
|
|
$qb->join('contestant.contest', 'contest')->willReturn($qb); |
55
|
|
|
|
56
|
|
|
$this::resolveAlias($qb, 'root.contestant.contest')->shouldBe('contest'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function it_use_wrong_alias_from_another_entity(QueryBuilder $qb): void |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$this::disableConflictProtection(); |
62
|
|
|
|
63
|
|
|
$qb->getAllAliases()->willReturn(['contestant']); |
64
|
|
|
$qb->getDQLPart('join')->willReturn([ |
65
|
|
|
'foo' => [ |
66
|
|
|
new Join(Join::INNER_JOIN, 'foo.bar', 'contestant'), |
67
|
|
|
], |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
$this::resolveAlias($qb, 'root.contestant')->shouldBe('contestant'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function it_resolve_exists_alias(QueryBuilder $qb): void |
74
|
|
|
{ |
75
|
|
|
$qb->getAllAliases()->willReturn(['contestant']); |
76
|
|
|
$qb->getDQLPart('join')->willReturn([ |
77
|
|
|
'root' => [ |
78
|
|
|
new Join(Join::INNER_JOIN, 'root.contestant', 'contestant'), |
79
|
|
|
], |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
$this::resolveAlias($qb, 'root.contestant')->shouldBe('contestant'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function it_resolve_wrong_alias_without_joining(QueryBuilder $qb): void |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$this::disableConflictProtection(); |
88
|
|
|
$this::disableAutoJoining(); |
89
|
|
|
|
90
|
|
|
$qb->getAllAliases()->willReturn(['contestant']); |
91
|
|
|
$qb->getDQLPart('join')->willReturn([ |
92
|
|
|
'foo' => [ |
93
|
|
|
new Join(Join::INNER_JOIN, 'foo.bar', 'contestant'), |
94
|
|
|
], |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
$this::resolveAlias($qb, 'root.contestant.contest')->shouldBe('contest'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function it_join_entities(QueryBuilder $qb): void |
101
|
|
|
{ |
102
|
|
|
$qb->getAllAliases()->willReturn([]); |
103
|
|
|
$qb->getDQLPart('join')->willReturn([]); |
104
|
|
|
$qb->join('root.contestant', 'contestant')->willReturn($qb); |
105
|
|
|
$qb->join('contestant.contest', 'contest')->willReturn($qb); |
106
|
|
|
|
107
|
|
|
$this::resolveAlias($qb, 'root.contestant.contest')->shouldBe('contest'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function it_resolve_conflict(QueryBuilder $qb): void |
111
|
|
|
{ |
112
|
|
|
$qb->getAllAliases()->willReturn(['contestant']); |
113
|
|
|
$qb->getDQLPart('join')->willReturn([ |
114
|
|
|
'foo' => [ |
115
|
|
|
new Join(Join::INNER_JOIN, 'foo.bar', 'contestant'), |
116
|
|
|
], |
117
|
|
|
]); |
118
|
|
|
$qb->join('root.contestant', Argument::that(function ($argument) { |
|
|
|
|
119
|
|
|
return preg_match('/^contestant[a-f0-9]+/', $argument); |
120
|
|
|
}))->willReturn($qb); |
121
|
|
|
$qb->join(Argument::that(function ($argument) { |
|
|
|
|
122
|
|
|
return preg_match('/^contestant[a-f0-9]+\.contest$/', $argument); |
123
|
|
|
}), 'contest')->willReturn($qb); |
124
|
|
|
|
125
|
|
|
$this::resolveAlias($qb, 'root.contestant.contest')->shouldBe('contest'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.