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\IComparable; |
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 IComparableTester |
33
|
|
|
{ |
34
|
|
|
public abstract function getTargetClassInstance(); // use ConstructorMethodTester; |
|
|
|
|
35
|
|
|
public abstract function getTargetClassName(); // use ConstructorMethodTester; |
|
|
|
|
36
|
|
|
public abstract function getTargetClassReflection(); // use ConstructorMethodTester; |
|
|
|
|
37
|
|
|
public abstract function export($obj, $depth = 2, $short = false); // use plugin/ExporterPlugin; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Datasets for ``testIComparableCompareToMethod(integer|null $expected, IComparable $left, mixed $right)``. |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public abstract function IComparableCompareToMethodArgumentsProvider(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @testdox Can compare relative position with other objects |
48
|
|
|
* @dataProvider IComparableCompareToMethodArgumentsProvider |
49
|
|
|
*/ |
50
|
|
|
public function testIComparableCompareToMethod($expected, IComparable $left, $right) |
51
|
|
|
{ |
52
|
|
|
$actual = $left->compareTo($right); |
53
|
|
|
|
54
|
|
|
$message = String::format( |
55
|
|
|
'$obj->{method}({right}); // Returned: {actual} ($obj: {left})', |
56
|
|
|
[ |
57
|
|
|
'method' => 'compareTo', |
58
|
|
|
'left' => static::export($left), |
59
|
|
|
'right' => static::export($right), |
60
|
|
|
'actual' => static::export($actual) |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
|
64
|
|
View Code Duplication |
if ($expected === 0) { |
|
|
|
|
65
|
|
|
$this->assertInternalType('integer', $actual, $message); |
|
|
|
|
66
|
|
|
$this->assertEquals(0, $actual, $message); |
|
|
|
|
67
|
|
|
} else { |
68
|
|
|
if ($expected === null) { |
69
|
|
|
$this->assertNull($actual, $message); |
|
|
|
|
70
|
|
|
} else { |
71
|
|
|
$major = $minor = 0; |
72
|
|
|
|
73
|
|
|
if ($expected < 0) { |
74
|
|
|
$minor = $actual; |
75
|
|
|
} else { |
76
|
|
|
$major = $actual; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->assertInternalType('integer', $actual, $message); |
|
|
|
|
80
|
|
|
$this->assertGreaterThan($minor, $major, $message); |
|
|
|
|
81
|
|
|
$this->assertLessThan($major, $minor, $message); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @testdox Is compliant with ``NelsonMartell\IComparable`` interface |
88
|
|
|
* @depends testIComparableCompareToMethod |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function testIsCompliantWithIComparableIterface() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$message = String::format( |
93
|
|
|
'"{0}" do not implements "{1}" interface.', |
94
|
|
|
$this->getTargetClassName(), |
95
|
|
|
IComparable::class |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$this->assertContains(IComparable::class, $this->getTargetClassReflection()->getInterfaceNames(), $message); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
} |
|
|
|
|
101
|
|
|
|
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.