1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Core\Comparable\Tests\Units; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Comparable\Comparator; |
14
|
|
|
use Cubiche\Core\Comparable\ComparatorInterface; |
15
|
|
|
use Cubiche\Core\Visitor\Tests\Units\VisiteeInterfaceTestCase; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Comparator Interface Test Case Class. |
19
|
|
|
* |
20
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
21
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
abstract class ComparatorInterfaceTestCase extends VisiteeInterfaceTestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Test class. |
27
|
|
|
*/ |
28
|
|
|
public function testClass() |
29
|
|
|
{ |
30
|
|
|
$this |
31
|
|
|
->testedClass |
32
|
|
|
->implements(ComparatorInterface::class) |
33
|
|
|
; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Test compare. |
38
|
|
|
* |
39
|
|
|
* @param mixed $a |
40
|
|
|
* @param mixed $b |
41
|
|
|
* @param int $expected |
42
|
|
|
* |
43
|
|
|
* @dataProvider compareDataProvider |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function testCompare($a, $b, $expected) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$this |
48
|
|
|
->assert('The comparision is right') |
49
|
|
|
/* @var \Cubiche\Core\Comparable\ComparatorInterface $comparator */ |
50
|
|
|
->given($comparator = $this->newDefaultTestedInstance()) |
51
|
|
|
->then($this->comparision($comparator, $a, $b, $expected)); |
52
|
|
|
|
53
|
|
|
$this |
54
|
|
|
->assert('The comparator is a callable') |
55
|
|
|
->given($result1 = $comparator->compare($a, $b)) |
56
|
|
|
->when($result2 = $comparator($a, $b)) |
57
|
|
|
->then() |
58
|
|
|
->variable($result1) |
59
|
|
|
->isEqualTo($result2); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test reverse method. |
64
|
|
|
* |
65
|
|
|
* @param mixed $a |
66
|
|
|
* @param mixed $b |
67
|
|
|
* @param int $expected |
68
|
|
|
* |
69
|
|
|
* @dataProvider compareDataProvider |
70
|
|
|
*/ |
71
|
|
|
public function testReverse($a, $b, $expected) |
72
|
|
|
{ |
73
|
|
|
$this |
74
|
|
|
->assert('Reverse is a comparator') |
75
|
|
|
/* @var \Cubiche\Core\Comparable\ComparatorInterface $comparator */ |
76
|
|
|
->given($comparator = $this->newDefaultTestedInstance()) |
77
|
|
|
->when($reverse = $comparator->reverse()) |
78
|
|
|
->then() |
79
|
|
|
->object($reverse) |
80
|
|
|
->isInstanceOf(ComparatorInterface::class) |
81
|
|
|
; |
82
|
|
|
|
83
|
|
|
$this |
84
|
|
|
->assert('Reverse result is the inverse result') |
85
|
|
|
->then($this->comparision($reverse, $a, $b, -1 * $expected)) |
86
|
|
|
; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Test otherwise method. |
91
|
|
|
* |
92
|
|
|
* @param callable $otherwise |
93
|
|
|
* @param mixed $a |
94
|
|
|
* @param mixed $b |
95
|
|
|
* @param int $expected |
96
|
|
|
* |
97
|
|
|
* @dataProvider otherwiseDataProvider |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function testOtherwise(callable $otherwise, $a, $b, $expected) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$this |
102
|
|
|
->assert('The otherwise result is a comparator') |
103
|
|
|
/* @var \Cubiche\Core\Comparable\ComparatorInterface $comparator */ |
104
|
|
|
->given($comparator = $this->newDefaultTestedInstance()) |
105
|
|
|
->when($otherwiseComparator = $comparator->otherwise($otherwise)) |
106
|
|
|
->object($otherwiseComparator) |
107
|
|
|
->isInstanceOf(ComparatorInterface::class) |
108
|
|
|
; |
109
|
|
|
|
110
|
|
|
$this |
111
|
|
|
->assert('The otherwise comparator result is right') |
112
|
|
|
->let($expected = $this->sign($expected)) |
113
|
|
|
->then( |
114
|
|
|
$this->comparision($otherwiseComparator, $a, $b, $expected == 0 ? $otherwise($a, $b) : $expected) |
115
|
|
|
) |
116
|
|
|
; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
abstract protected function compareDataProvider(); |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
protected function otherwiseDataProvider() |
128
|
|
|
{ |
129
|
|
|
foreach ($this->compareDataProvider() as $key => $data) { |
130
|
|
|
yield $key => \array_merge(array($this->newDefaultOtherwiseComparator()), $data); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return callable |
136
|
|
|
*/ |
137
|
|
|
abstract protected function newDefaultOtherwiseComparator(); |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param ComparatorInterface $comparator |
141
|
|
|
* @param mixed $a |
142
|
|
|
* @param mixed $b |
143
|
|
|
* @param int $expected |
144
|
|
|
*/ |
145
|
|
|
protected function comparision(ComparatorInterface $comparator, $a, $b, $expected) |
146
|
|
|
{ |
147
|
|
|
$this |
148
|
|
|
->given($comparator) |
149
|
|
|
->when($result = $comparator->compare($a, $b)) |
150
|
|
|
->then() |
151
|
|
|
->integer($this->sign($result)) |
152
|
|
|
->isEqualTo($this->sign($expected)) |
153
|
|
|
; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param float|int $number |
158
|
|
|
* |
159
|
|
|
* @return int |
160
|
|
|
*/ |
161
|
|
|
protected function sign($number) |
162
|
|
|
{ |
163
|
|
|
return $number >= 0 ? ($number == 0 ? 0 : 1) : -1; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
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.