1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHP: Nelson Martell Library file |
5
|
|
|
* |
6
|
|
|
* Copyright © 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 2021 Nelson Martell |
13
|
|
|
* @link http://nelson6e65.github.io/php_nml/ |
14
|
|
|
* @since 1.0.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 NelsonMartell\Extensions\Text; |
24
|
|
|
use PHPUnit\Framework\TestCase; |
25
|
|
|
use NelsonMartell\IConvertibleToString; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Test helper for classes implementing {@see IConvertibleToString} interface. |
29
|
|
|
* |
30
|
|
|
* @author Nelson Martell <[email protected]> |
31
|
|
|
* @since 1.0.0 |
32
|
|
|
* */ |
33
|
|
|
trait ImplementsIConvertibleToString |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
abstract public function getTargetClassName(): string; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
abstract public function toStringProvider(): array; |
44
|
|
|
|
45
|
|
|
public function testImplementsIConvertibleToString(): void |
46
|
|
|
{ |
47
|
|
|
$className = $this->getTargetClassName(); |
48
|
|
|
$class = new ReflectionClass($className); |
49
|
|
|
|
50
|
|
|
$message = Text::format( |
51
|
|
|
'"{0}" do not implements "{1}" interface.', |
52
|
|
|
$className, |
53
|
|
|
IConvertibleToString::class |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
/** @var TestCase $this */ |
57
|
|
|
$this->assertContains( |
58
|
|
|
IConvertibleToString::class, |
59
|
|
|
$class->getInterfaceNames(), |
60
|
|
|
$message |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @covers ::toString |
66
|
|
|
* @covers ::__toString |
67
|
|
|
* @dataProvider toStringProvider |
68
|
|
|
* |
69
|
|
|
* @param string $expected |
70
|
|
|
* @param IConvertibleToString $obj |
71
|
|
|
* |
72
|
|
|
* @see IConvertibleToString::toString() |
73
|
|
|
* @see IConvertibleToString::__toString() |
74
|
|
|
*/ |
75
|
|
|
public function testPerformsConversionToString(string $expected, IConvertibleToString $obj): void |
76
|
|
|
{ |
77
|
|
|
/** @var TestCase $this */ |
78
|
|
|
$this->assertSame($expected, $obj->toString(), 'Failed explicit conversion to string'); |
79
|
|
|
|
80
|
|
|
$this->assertSame($expected, $obj . '', 'Failed implicit conversion to string'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|