1 | <?php |
||
2 | |||
3 | /** |
||
4 | * PHP: Nelson Martell Library file |
||
5 | * |
||
6 | * Copyright © 2016-2021 Nelson Martell (http://nelson6e65.github.io) |
||
7 | * |
||
8 | * Licensed under The MIT License (MIT) |
||
9 | * For full copyright and license information, please see the LICENSE |
||
10 | * Redistributions of files must retain the above copyright notice. |
||
11 | * |
||
12 | * @copyright 2016-2021 Nelson Martell |
||
13 | * @link http://nelson6e65.github.io/php_nml/ |
||
14 | * @since v0.6.0 |
||
15 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
||
16 | * */ |
||
17 | |||
18 | declare(strict_types=1); |
||
19 | |||
20 | namespace NelsonMartell\Test\Helpers; |
||
21 | |||
22 | use ReflectionClass; |
||
23 | use InvalidArgumentException; |
||
24 | use NelsonMartell\Extensions\Text; |
||
25 | use NelsonMartell\IEquatable; |
||
26 | use PHPUnit\Framework\TestCase; |
||
27 | |||
28 | use function NelsonMartell\typeof; |
||
29 | |||
30 | /** |
||
31 | * Test helper for classes implementing ``NelsonMartell\IEquatable`` interface. |
||
32 | * |
||
33 | * Note: Classes using this trait MUST use ConstructorMethodTester and ExporterPlugin traits too. |
||
34 | * |
||
35 | * @author Nelson Martell <[email protected]> |
||
36 | * */ |
||
37 | trait IEquatableTester |
||
38 | { |
||
39 | /** |
||
40 | * @return string |
||
41 | * |
||
42 | * @see ConstructorMethodTester |
||
43 | */ |
||
44 | abstract public function getTargetClassName(): string; |
||
45 | |||
46 | /** |
||
47 | * @param mixed $obj |
||
48 | * @param int $depth |
||
49 | * @param bool $short |
||
50 | * |
||
51 | * @return string |
||
52 | * |
||
53 | * @see ExporterPlugin |
||
54 | */ |
||
55 | abstract public static function export($obj, int $depth = 2, bool $short = false): string; |
||
56 | |||
57 | /** |
||
58 | * Datasets for ``testIEquatableEqualsMethod(bool $expected, IEquatable $left, mixed $right)``. |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | abstract public function IEquatableMethodArgumentsProvider(): array; |
||
63 | |||
64 | |||
65 | /** |
||
66 | * @testdox Can check if instances are equals to other objects |
||
67 | * @dataProvider IEquatableMethodArgumentsProvider |
||
68 | * |
||
69 | * @param int|null $expected |
||
70 | * @param IEquatable $left |
||
71 | * @param mixed $right |
||
72 | */ |
||
73 | public function testIEquatableEqualsMethod($expected, IEquatable $left, $right): void |
||
74 | { |
||
75 | $actual = $left->equals($right); |
||
76 | |||
77 | $message = Text::format( |
||
78 | '$obj->{method}({right}); // Returned: {actual} ($obj: {left})', |
||
79 | [ |
||
80 | 'method' => 'equals', |
||
81 | 'left' => static::export($left), |
||
82 | 'right' => static::export($right), |
||
83 | 'actual' => static::export($actual), |
||
84 | ] |
||
85 | ); |
||
86 | |||
87 | /** @var TestCase $this */ |
||
88 | $this->assertIsBool($actual, $message); |
||
89 | |||
90 | if (!is_bool($expected)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
91 | throw new InvalidArgumentException(Text::format( |
||
92 | '1st argument of data provider should be of "boolean" type, "{0}" given.', |
||
93 | typeof($expected) |
||
94 | )); |
||
95 | } |
||
96 | |||
97 | $this->assertEquals($expected, $actual, $message); |
||
98 | |||
99 | $this->assertTrue($left->equals($left), '[Shold be equal to itself]'); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @testdox Is compliant with ``NelsonMartell\IEquatable`` interface |
||
104 | * @depends testIEquatableEqualsMethod |
||
105 | */ |
||
106 | public function testIsCompliantWithIEquatableIterface() |
||
107 | { |
||
108 | $className = $this->getTargetClassName(); |
||
109 | |||
110 | $message = Text::format( |
||
111 | '"{0}" do not implements "{1}" interface.', |
||
112 | $className, |
||
113 | IEquatable::class |
||
114 | ); |
||
115 | |||
116 | $reflectionClass = new ReflectionClass($className); |
||
117 | |||
118 | /** @var TestCase $this */ |
||
119 | $this->assertContains(IEquatable::class, $reflectionClass->getInterfaceNames(), $message); |
||
120 | } |
||
121 | } |
||
122 |