1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP: Nelson Martell Library file |
4
|
|
|
* |
5
|
|
|
* Content: |
6
|
|
|
* - Trait definition |
7
|
|
|
* |
8
|
|
|
* Copyright © 2016 Nelson Martell (http://nelson6e65.github.io) |
9
|
|
|
* |
10
|
|
|
* Licensed under The MIT License (MIT) |
11
|
|
|
* For full copyright and license information, please see the LICENSE |
12
|
|
|
* Redistributions of files must retain the above copyright notice. |
13
|
|
|
* |
14
|
|
|
* @copyright 2016 Nelson Martell |
15
|
|
|
* @link http://nelson6e65.github.io/php_nml/ |
16
|
|
|
* @since v0.6.0 |
17
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
18
|
|
|
* */ |
19
|
|
|
|
20
|
|
|
namespace NelsonMartell\Test\Helpers; |
21
|
|
|
|
22
|
|
|
use NelsonMartell\Extensions\String; |
23
|
|
|
use NelsonMartell\IComparer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test helper for classes implementing ``NelsonMartell\IComparable`` interface. |
27
|
|
|
* |
28
|
|
|
* Note: Classes using this trait MUST use ConstructorMethodTester and ExporterPlugin traits too. |
29
|
|
|
* |
30
|
|
|
* @author Nelson Martell <[email protected]> |
31
|
|
|
* */ |
32
|
|
|
trait IComparerTester |
33
|
|
|
{ |
34
|
|
|
public abstract function getTargetClassName(); // use ConstructorMethodTester; |
|
|
|
|
35
|
|
|
public abstract function getTargetClassReflection(); // use ConstructorMethodTester; |
|
|
|
|
36
|
|
|
public abstract function export($obj, $depth = 2, $short = false); // use plugin/ExporterPlugin; |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Datasets for ``testCompareMethod(integer|null $expected, mixed $left, mixed $right)``. |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public abstract function compareMethodArgumentsProvider(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Datasets for ``testCanUseCompareMethodInArraySorting(integer|null $expected, mixed $left, mixed $right)``. |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public abstract function compareMethodArraysProvider(); |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @testdox Can compare relative position of objects of different type |
55
|
|
|
* @dataProvider compareMethodArgumentsProvider |
56
|
|
|
*/ |
57
|
|
|
public function testCompareMethod($expected, $left, $right) |
58
|
|
|
{ |
59
|
|
|
$class = $this->getTargetClassName(); |
|
|
|
|
60
|
|
|
$actual = $class::compare($left, $right); |
61
|
|
|
|
62
|
|
|
$message = String::format( |
63
|
|
|
'{class}::{method}({left}, {right}); // Returned: {actual}', |
64
|
|
|
[ |
65
|
|
|
'class' => $class, |
66
|
|
|
'method' => 'compare', |
67
|
|
|
'left' => static::export($left), |
68
|
|
|
'right' => static::export($right), |
69
|
|
|
'actual' => static::export($actual) |
70
|
|
|
] |
71
|
|
|
); |
72
|
|
|
|
73
|
|
View Code Duplication |
if ($expected === 0) { |
|
|
|
|
74
|
|
|
$this->assertInternalType('integer', $actual, $message); |
|
|
|
|
75
|
|
|
$this->assertEquals(0, $actual, $message); |
|
|
|
|
76
|
|
|
} else { |
77
|
|
|
if ($expected === null) { |
78
|
|
|
$this->assertNull($actual, $message); |
|
|
|
|
79
|
|
|
} else { |
80
|
|
|
$major = $minor = 0; |
81
|
|
|
|
82
|
|
|
if ($expected < 0) { |
83
|
|
|
$minor = $actual; |
84
|
|
|
} else { |
85
|
|
|
$major = $actual; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->assertInternalType('integer', $actual, $message); |
|
|
|
|
89
|
|
|
$this->assertGreaterThan($minor, $major, $message); |
|
|
|
|
90
|
|
|
$this->assertLessThan($major, $minor, $message); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @testdox Provides comparison function to array sorting |
98
|
|
|
* @dataProvider compareMethodArraysProvider |
99
|
|
|
*/ |
100
|
|
|
public function testCanUseCompareMethodInArraySorting(array $expected) |
101
|
|
|
{ |
102
|
|
|
$actual = $expected; |
103
|
|
|
|
104
|
|
|
@shuffle($actual); |
|
|
|
|
105
|
|
|
|
106
|
|
|
$message = String::format( |
107
|
|
|
'usort({actual}, [{class}, {method}]);', |
108
|
|
|
[ |
109
|
|
|
'class' => $this->getTargetClassName(), |
110
|
|
|
'method' => 'compare', |
111
|
|
|
'actual' => static::export($actual) |
112
|
|
|
] |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
@usort($actual, [$this->getTargetClassName(), 'compare']); |
|
|
|
|
116
|
|
|
|
117
|
|
|
$this->assertEquals($expected, $actual, $message); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @testdox Is compliant with ``NelsonMartell\IComparer`` interface |
122
|
|
|
* @depends testCanUseCompareMethodInArraySorting |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function testIsCompliantWithIComparerIterface() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$message = String::format( |
127
|
|
|
'"{0}" do not implements "{1}" interface.', |
128
|
|
|
$this->getTargetClassName(), |
129
|
|
|
IComparer::class |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$this->assertContains(IComparer::class, $this->getTargetClassReflection()->getInterfaceNames(), $message); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
} |
|
|
|
|
135
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.