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 as NML; |
23
|
|
|
use NelsonMartell\Extensions\String; |
24
|
|
|
use NelsonMartell\IEquatable; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Test helper for classes implementing ``NelsonMartell\IEquatable`` interface. |
28
|
|
|
* |
29
|
|
|
* Note: Classes using this trait MUST use ConstructorMethodTester and ExporterPlugin traits too. |
30
|
|
|
* |
31
|
|
|
* @author Nelson Martell <[email protected]> |
32
|
|
|
* */ |
33
|
|
|
trait IEquatableTester |
34
|
|
|
{ |
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 ``testIEquatableEqualsMethod(bool $expected, IEquatable $left, mixed $right)``. |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public abstract function IEquatableMethodArgumentsProvider(); |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @testdox Can check if instances are equals to other objects |
49
|
|
|
* @dataProvider IEquatableMethodArgumentsProvider |
50
|
|
|
*/ |
51
|
|
|
public function testIEquatableEqualsMethod($expected, IEquatable $left, $right) |
52
|
|
|
{ |
53
|
|
|
$actual = $left->equals($right); |
54
|
|
|
|
55
|
|
|
$message = String::format( |
56
|
|
|
'$obj->{method}({right}); // Returned: {actual} ($obj: {left})', |
57
|
|
|
[ |
58
|
|
|
'method' => 'equals', |
59
|
|
|
'left' => static::export($left), |
60
|
|
|
'right' => static::export($right), |
61
|
|
|
'actual' => static::export($actual) |
62
|
|
|
] |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$this->assertInternalType('boolean', $actual, $message); |
|
|
|
|
66
|
|
|
|
67
|
|
|
if (!is_bool($expected)) { |
68
|
|
|
throw new InvalidArgumentException(String::format( |
69
|
|
|
'1st argument of data provider should be of "boolean" type, "{0}" given.', |
70
|
|
|
NML\typeof($expected) |
|
|
|
|
71
|
|
|
)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->assertEquals($expected, $actual, $message); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$this->assertTrue($left->equals($left), '[Shold be equal to itself]'); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @testdox Is compliant with ``NelsonMartell\IEquatable`` interface |
81
|
|
|
* @depends testIEquatableEqualsMethod |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function testIsCompliantWithIEquatableIterface() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$message = String::format( |
86
|
|
|
'"{0}" do not implements "{1}" interface.', |
87
|
|
|
$this->getTargetClassName(), |
88
|
|
|
IEquatable::class |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$this->assertContains(IEquatable::class, $this->getTargetClassReflection()->getInterfaceNames(), $message); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
|
|
|
|
94
|
|
|
|
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.