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\Operand; |
16
|
|
|
|
17
|
|
|
use Doctrine\ORM\QueryBuilder; |
18
|
|
|
use Happyr\DoctrineSpecification\Operand\Field; |
19
|
|
|
use Happyr\DoctrineSpecification\Operand\Operand; |
20
|
|
|
use PhpSpec\ObjectBehavior; |
21
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException; |
22
|
|
|
use tests\Happyr\DoctrineSpecification\Game; |
23
|
|
|
use tests\Happyr\DoctrineSpecification\Player; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @mixin Field |
27
|
|
|
*/ |
28
|
|
|
final class FieldSpec extends ObjectBehavior |
29
|
|
|
{ |
30
|
|
|
private $fieldName = 'foo'; |
31
|
|
|
|
32
|
|
|
public function let(): void |
33
|
|
|
{ |
34
|
|
|
$this->beConstructedWith($this->fieldName, null); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function it_is_a_field(): void |
38
|
|
|
{ |
39
|
|
|
$this->shouldBeAnInstanceOf(Field::class); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function it_is_a_operand(): void |
43
|
|
|
{ |
44
|
|
|
$this->shouldBeAnInstanceOf(Operand::class); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function it_is_transformable(QueryBuilder $qb): void |
48
|
|
|
{ |
49
|
|
|
$context = 'a'; |
50
|
|
|
$expression = 'a.foo'; |
51
|
|
|
|
52
|
|
|
$this->transform($qb, $context)->shouldReturn($expression); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function it_is_in_context(QueryBuilder $qb): void |
56
|
|
|
{ |
57
|
|
|
$this->beConstructedWith($this->fieldName, 'user'); |
58
|
|
|
|
59
|
|
|
$qb->getDQLPart('join')->willReturn([]); |
60
|
|
|
$qb->getAllAliases()->willReturn([]); |
61
|
|
|
$qb->join('root.user', 'user')->willReturn($qb); |
62
|
|
|
|
63
|
|
|
$this->transform($qb, 'root')->shouldReturn(sprintf('user.%s', $this->fieldName)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function it_is_executable_object(): void |
67
|
|
|
{ |
68
|
|
|
$this->beConstructedWith('pseudo'); |
69
|
|
|
|
70
|
|
|
$player = new Player('Moe', 'M', 1230); |
71
|
|
|
|
72
|
|
|
$this->execute($player, null)->shouldReturn($player->pseudo); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function it_is_executable_array(): void |
76
|
|
|
{ |
77
|
|
|
$this->beConstructedWith('pseudo'); |
78
|
|
|
|
79
|
|
|
$player = ['pseudo' => 'Moe', 'gender' => 'M', 'points' => 1230]; |
80
|
|
|
|
81
|
|
|
$this->execute($player, null)->shouldReturn($player['pseudo']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
public function it_is_executable_object_in_context(): void |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$this->beConstructedWith('name', 'inGame'); |
87
|
|
|
|
88
|
|
|
$game = new Game('Tetris'); |
89
|
|
|
$player = new Player('Moe', 'M', 1230, $game); |
90
|
|
|
|
91
|
|
|
$this->execute($player, null)->shouldReturn($game->name); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function it_is_executable_array_in_context(): void |
95
|
|
|
{ |
96
|
|
|
$this->beConstructedWith('name', 'inGame'); |
97
|
|
|
|
98
|
|
|
$game = ['name' => 'Tetris']; |
99
|
|
|
$player = ['pseudo' => 'Moe', 'gender' => 'M', 'points' => 1230, 'inGame' => $game]; |
100
|
|
|
|
101
|
|
|
$this->execute($player, null)->shouldReturn($game['name']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function it_is_executable_object_in_global_context(): void |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$this->beConstructedWith('name'); |
107
|
|
|
|
108
|
|
|
$game = new Game('Tetris'); |
109
|
|
|
$player = new Player('Moe', 'M', 1230, $game); |
110
|
|
|
|
111
|
|
|
$this->execute($player, 'inGame')->shouldReturn($game->name); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function it_is_executable_array_in_global_context(): void |
115
|
|
|
{ |
116
|
|
|
$this->beConstructedWith('name'); |
117
|
|
|
|
118
|
|
|
$game = ['name' => 'Tetris']; |
119
|
|
|
$player = ['pseudo' => 'Moe', 'gender' => 'M', 'points' => 1230, 'inGame' => $game]; |
120
|
|
|
|
121
|
|
|
$this->execute($player, 'inGame')->shouldReturn($game['name']); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function it_is_executable_mixed_candidate(): void |
125
|
|
|
{ |
126
|
|
|
$this->beConstructedWith('name', 'inGame'); |
127
|
|
|
|
128
|
|
|
$game = new Game('Tetris'); |
129
|
|
|
$player = ['pseudo' => 'Moe', 'gender' => 'M', 'points' => 1230, 'inGame' => $game]; |
130
|
|
|
|
131
|
|
|
$this->shouldThrow(NoSuchIndexException::class)->duringExecute($player, null); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function it_is_executable_collection(): void |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$this->beConstructedWith('pseudo', 'players'); |
137
|
|
|
|
138
|
|
|
$game = [ |
139
|
|
|
'name' => 'Tetris', |
140
|
|
|
'players' => [ |
141
|
|
|
['pseudo' => 'Moe', 'gender' => 'M', 'points' => 1230], |
142
|
|
|
], |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
$this->execute($game, null)->shouldReturn(null); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
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.