1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP: Nelson Martell Library file |
4
|
|
|
* |
5
|
|
|
* Content: |
6
|
|
|
* - Trait definition |
7
|
|
|
* |
8
|
|
|
* Copyright © 2016-2017 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-2017 Nelson Martell |
15
|
|
|
* @link http://nelson6e65.github.io/php_nml/ |
16
|
|
|
* @since v0.7.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 Cake\Utility\Inflector; |
23
|
|
|
use NelsonMartell\Extensions\Text; |
24
|
|
|
use NelsonMartell\IStrictPropertiesContainer; |
25
|
|
|
use SebastianBergmann\Exporter\Exporter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Split of ImplementsIStrictPropertiesContainer, for classes implementing any read-only property |
29
|
|
|
* |
30
|
|
|
* @author Nelson Martell <[email protected]> |
31
|
|
|
* */ |
32
|
|
|
trait HasReadWriteProperties |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @returns IStrictPropertiesContainer |
36
|
|
|
*/ |
37
|
|
|
public abstract function testImplementsIStrictPropertiesContainerInterface($obj); |
|
|
|
|
38
|
|
|
|
39
|
|
|
public abstract function readwritePropertiesProvider(); |
|
|
|
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @depends testImplementsIStrictPropertiesContainerInterface |
44
|
|
|
* @dataProvider readwritePropertiesProvider |
45
|
|
|
*/ |
46
|
|
|
public function testPropertiesWithFullAccessAreReadablesAndWritables( |
47
|
|
|
IStrictPropertiesContainer $obj, |
48
|
|
|
$property, |
49
|
|
|
$value, |
50
|
|
|
$expected |
51
|
|
|
) { |
52
|
|
|
$exporter = new Exporter(); |
53
|
|
|
|
54
|
|
|
$var = get_class($obj); |
55
|
|
|
$var = Inflector::variable(substr( |
56
|
|
|
$var, |
57
|
|
|
strrpos($var, '\\') === false ? 0 : strrpos($var, '\\') + 1 |
58
|
|
|
)); |
59
|
|
|
|
60
|
|
|
$obj->$property = $value; |
61
|
|
|
|
62
|
|
|
$actual = $obj->$property; |
63
|
|
|
|
64
|
|
|
$message = Text::format( |
65
|
|
|
'${var}->{property} = {value}; $actual = ${var}->{property}; // {actual}', |
66
|
|
|
[ |
67
|
|
|
'var' => $var, |
68
|
|
|
'property' => $property, |
69
|
|
|
'actual' => $exporter->shortenedExport($actual), |
70
|
|
|
'value' => $exporter->shortenedExport($value), |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$this->assertEquals($expected, $actual, $message); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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.