1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Cubiche\Core\Specification\Tests\Units; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Specification\AndSpecification; |
13
|
|
|
use Cubiche\Core\Specification\Criteria; |
14
|
|
|
use Cubiche\Core\Specification\OrSpecification; |
15
|
|
|
use Cubiche\Core\Specification\Specification; |
16
|
|
|
use Cubiche\Core\Specification\SpecificationInterface; |
17
|
|
|
use Cubiche\Core\Visitor\Tests\Units\VisiteeInterfaceTestCase; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Specification Interface Test Case class. |
21
|
|
|
* |
22
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
23
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class SpecificationInterfaceTestCase extends VisiteeInterfaceTestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Test class. |
29
|
|
|
*/ |
30
|
|
|
public function testClass() |
31
|
|
|
{ |
32
|
|
|
$this |
33
|
|
|
->testedClass |
34
|
|
|
->implements(SpecificationInterface::class) |
35
|
|
|
; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Test andX. |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function testAndX() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this |
44
|
|
|
->given( |
45
|
|
|
/** @var \Cubiche\Core\Specification\SpecificationInterface $specification */ |
46
|
|
|
$specification = $this->newDefaultTestedInstance(), |
47
|
|
|
$other = $this->newRandomSpecification() |
48
|
|
|
) |
49
|
|
|
->then() |
50
|
|
|
->when($andSpecification = $specification->andX($other)) |
51
|
|
|
->object($andSpecification) |
52
|
|
|
->isInstanceOf(AndSpecification::class) |
53
|
|
|
->object($andSpecification->left()) |
54
|
|
|
->isIdenticalTo($specification) |
55
|
|
|
->object($andSpecification->right()) |
56
|
|
|
->isIdenticalTo($other) |
57
|
|
|
; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Test orX. |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function testOrX() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this |
66
|
|
|
->given( |
67
|
|
|
/** @var \Cubiche\Core\Specification\SpecificationInterface $specification */ |
68
|
|
|
$specification = $this->newDefaultTestedInstance(), |
69
|
|
|
$other = $this->newRandomSpecification() |
70
|
|
|
) |
71
|
|
|
->then() |
72
|
|
|
->when($orSpecification = $specification->orX($other)) |
73
|
|
|
->object($orSpecification) |
74
|
|
|
->isInstanceOf(OrSpecification::class) |
75
|
|
|
->object($orSpecification->left()) |
76
|
|
|
->isIdenticalTo($specification) |
77
|
|
|
->object($orSpecification->right()) |
78
|
|
|
->isIdenticalTo($other) |
79
|
|
|
; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Test magic and/or. |
84
|
|
|
*/ |
85
|
|
|
public function testMagicCall() |
86
|
|
|
{ |
87
|
|
|
$this |
88
|
|
|
/* @var \Cubiche\Core\Specification\SpecificationInterface $specificationMock */ |
89
|
|
|
->given($specificationMock = $this->newDefaultMockTestedInstance()) |
90
|
|
|
->given($specification = $this->newRandomSpecification()) |
91
|
|
|
->when($specificationMock->and($specification)) |
92
|
|
|
->mock($specificationMock) |
93
|
|
|
->call('andX') |
94
|
|
|
->withArguments($specification)->once() |
95
|
|
|
->when($specificationMock->or($specification)) |
96
|
|
|
->mock($specificationMock) |
97
|
|
|
->call('orX') |
98
|
|
|
->withArguments($specification)->once() |
99
|
|
|
; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Test not. |
104
|
|
|
*/ |
105
|
|
|
public function testNot() |
106
|
|
|
{ |
107
|
|
|
$this |
108
|
|
|
/* @var \Cubiche\Core\Specification\SpecificationInterface $specification */ |
109
|
|
|
->given($specification = $this->newDefaultTestedInstance()) |
110
|
|
|
->then() |
111
|
|
|
->when($notSpecification = $specification->not()) |
112
|
|
|
->object($notSpecification) |
113
|
|
|
->isInstanceOf(SpecificationInterface::class) |
114
|
|
|
->object($notSpecification->not()) |
115
|
|
|
->isEqualTo($specification) |
116
|
|
|
; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Test evaluate success. |
121
|
|
|
* |
122
|
|
|
* @dataProvider evaluateSuccessDataProvider |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function testEvaluateSuccess($value) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$this |
127
|
|
|
/* @var \Cubiche\Core\Specification\SpecificationInterface $specification */ |
128
|
|
|
->given($specification = $this->newDefaultTestedInstance()) |
129
|
|
|
->then() |
130
|
|
|
->boolean($specification->evaluate($value)) |
131
|
|
|
->isTrue() |
132
|
|
|
->boolean($specification->not()->evaluate($value)) |
133
|
|
|
->isFalse() |
134
|
|
|
; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Test evaluate failure. |
139
|
|
|
* |
140
|
|
|
* @dataProvider evaluateFailureDataProvider |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function testEvaluateFailure($value) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$this |
145
|
|
|
/* @var \Cubiche\Core\Specification\SpecificationInterface $specification */ |
146
|
|
|
->given($specification = $this->newDefaultTestedInstance()) |
147
|
|
|
->then() |
148
|
|
|
->boolean($specification->evaluate($value)) |
149
|
|
|
->isFalse() |
150
|
|
|
->boolean($specification->not()->evaluate($value)) |
151
|
|
|
->isTrue() |
152
|
|
|
; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
abstract protected function evaluateSuccessDataProvider(); |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
abstract protected function evaluateFailureDataProvider(); |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return \Cubiche\Core\Specification\SpecificationInterface |
167
|
|
|
*/ |
168
|
|
|
protected function newRandomSpecification() |
169
|
|
|
{ |
170
|
|
|
switch (\rand(0, 3)) { |
171
|
|
|
case 0: |
172
|
|
|
return Criteria::eq(5); |
173
|
|
|
case 1: |
174
|
|
|
return Criteria::gte(5); |
175
|
|
|
case 2: |
176
|
|
|
return Criteria::property('foo')->lte(10); |
177
|
|
|
case 3: |
178
|
|
|
default: |
179
|
|
|
return Criteria::false(); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
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.