|
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 Cake\Utility\Inflector; |
|
23
|
|
|
use NelsonMartell\Extensions\String; |
|
24
|
|
|
use NelsonMartell\IStrictPropertiesContainer; |
|
25
|
|
|
use SebastianBergmann\Exporter\Exporter; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Test helper for classes implementing ``NelsonMartell\IStrictPropertiesContainer`` interface and |
|
29
|
|
|
* ``NelsonMartell\PropertiesHandler`` trait. |
|
30
|
|
|
* |
|
31
|
|
|
* You can pass an empty in some providers to skip test. For example, if a class has not 'write-only' properties) |
|
32
|
|
|
* |
|
33
|
|
|
* @author Nelson Martell <[email protected]> |
|
34
|
|
|
* */ |
|
35
|
|
|
trait IStrictPropertiesContainerTester |
|
36
|
|
|
{ |
|
37
|
|
|
public abstract function readonlyPropertiesProvider(); |
|
|
|
|
|
|
38
|
|
|
public abstract function writeonlyPropertiesProvider(); |
|
|
|
|
|
|
39
|
|
|
public abstract function readwritePropertiesProvider(); |
|
|
|
|
|
|
40
|
|
|
public abstract function unaccesiblePropertiesProvider(); |
|
|
|
|
|
|
41
|
|
|
public abstract function objectInstanceProvider(); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Optional if provider returns an empty array. |
|
45
|
|
|
* |
|
46
|
|
|
* @dataProvider readonlyPropertiesProvider |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testReadonlyPropertiesAreReadables( |
|
49
|
|
|
IStrictPropertiesContainer $obj = null, |
|
50
|
|
|
$property = null, |
|
51
|
|
|
$expected = null |
|
52
|
|
|
) { |
|
53
|
|
|
if ($obj === null) { |
|
54
|
|
|
$this->markTestSkipped('Target class has not read-only properties.'); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$exporter = new Exporter(); |
|
58
|
|
|
|
|
59
|
|
|
$var = get_class($obj); |
|
60
|
|
|
$var = Inflector::variable(substr( |
|
61
|
|
|
$var, |
|
62
|
|
|
strrpos($var, '\\') === false ? 0 : strrpos($var, '\\') + 1 |
|
63
|
|
|
)); |
|
64
|
|
|
|
|
65
|
|
|
$actual = $obj->$property; |
|
66
|
|
|
|
|
67
|
|
|
$message = String::format( |
|
68
|
|
|
'$actual = ${var}->{property}; // {actual}', |
|
69
|
|
|
[ |
|
70
|
|
|
'var' => $var, |
|
71
|
|
|
'property' => $property, |
|
72
|
|
|
'actual' => $exporter->shortenedExport($actual) |
|
73
|
|
|
] |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals($expected, $actual, $message); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @depends testReadonlyPropertiesAreReadables |
|
81
|
|
|
* @dataProvider readonlyPropertiesProvider |
|
82
|
|
|
* @expectedException \BadMethodCallException |
|
83
|
|
|
*/ |
|
84
|
|
|
public function testReadonlyPropertiesAreNotWritables( |
|
85
|
|
|
IStrictPropertiesContainer $obj = null, |
|
86
|
|
|
$property = null, |
|
87
|
|
|
$value = null |
|
88
|
|
|
) { |
|
89
|
|
|
$obj->$property = $value; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @dataProvider writeonlyPropertiesProvider |
|
94
|
|
|
*/ |
|
95
|
|
|
public function testWriteonlyPropertiesAreWritables( |
|
96
|
|
|
IStrictPropertiesContainer $obj = null, |
|
97
|
|
|
$property = null, |
|
98
|
|
|
$value = null |
|
99
|
|
|
) { |
|
100
|
|
|
if ($obj === null) { |
|
101
|
|
|
$this->markTestSkipped('Target class has not write-only properties to test.'); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$obj->$property = $value; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @depends testWriteonlyPropertiesAreWritables |
|
109
|
|
|
* @dataProvider writeonlyPropertiesProvider |
|
110
|
|
|
* @expectedException \BadMethodCallException |
|
111
|
|
|
*/ |
|
112
|
|
|
public function testWriteonlyPropertiesAreNotReadables( |
|
113
|
|
|
IStrictPropertiesContainer $obj = null, |
|
114
|
|
|
$property = null, |
|
115
|
|
|
$value = null |
|
116
|
|
|
) { |
|
117
|
|
|
$obj->$property = $value; |
|
118
|
|
|
$actual = $obj->$property; |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @dataProvider readwritePropertiesProvider |
|
123
|
|
|
*/ |
|
124
|
|
|
public function testPropertiesWithFullAccessAreReadablesAndWritables( |
|
125
|
|
|
IStrictPropertiesContainer $obj = null, |
|
126
|
|
|
$property = null, |
|
127
|
|
|
$value = null, |
|
128
|
|
|
$expected = null |
|
129
|
|
|
) { |
|
130
|
|
|
if ($obj === null) { |
|
131
|
|
|
$this->markTestSkipped('Target class has not read-write properties to test.'); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$exporter = new Exporter(); |
|
135
|
|
|
|
|
136
|
|
|
$var = get_class($obj); |
|
137
|
|
|
$var = Inflector::variable(substr( |
|
138
|
|
|
$var, |
|
139
|
|
|
strrpos($var, '\\') === false ? 0 : strrpos($var, '\\') + 1 |
|
140
|
|
|
)); |
|
141
|
|
|
|
|
142
|
|
|
$obj->$property = $value; |
|
143
|
|
|
|
|
144
|
|
|
$actual = $obj->$property; |
|
145
|
|
|
|
|
146
|
|
|
$message = String::format( |
|
147
|
|
|
'${var}->{property} = {value}; $actual = ${var}->{property}; // {actual}', |
|
148
|
|
|
[ |
|
149
|
|
|
'var' => $var, |
|
150
|
|
|
'property' => $property, |
|
151
|
|
|
'actual' => $exporter->shortenedExport($actual), |
|
152
|
|
|
'value' => $exporter->shortenedExport($value), |
|
153
|
|
|
] |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
$this->assertEquals($expected, $actual, $message); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @dataProvider unaccesiblePropertiesProvider |
|
161
|
|
|
* @expectedException \BadMethodCallException |
|
162
|
|
|
*/ |
|
163
|
|
|
public function testUnaccessiblePropertiesThrowsCatchableError( |
|
164
|
|
|
IStrictPropertiesContainer $obj = null, |
|
165
|
|
|
$property = null, |
|
166
|
|
|
$value = null |
|
167
|
|
|
) { |
|
168
|
|
|
if ($obj === null) { |
|
169
|
|
|
$this->markTestSkipped('Target class has not unaccesible properties to test.'); |
|
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if ($value === null) { |
|
173
|
|
|
// Getter exception |
|
174
|
|
|
$actual = $obj->$property; |
|
|
|
|
|
|
175
|
|
|
} else { |
|
176
|
|
|
// Setter exception |
|
177
|
|
|
$obj->$property = $value; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @dataProvider objectInstanceProvider |
|
183
|
|
|
* @expectedException \BadMethodCallException |
|
184
|
|
|
*/ |
|
185
|
|
|
public function testIsUnableToCreateDirectAttributesOutsideOfClassDefinition(IStrictPropertiesContainer $obj) |
|
186
|
|
|
{ |
|
187
|
|
|
$obj->thisPropertyNameIsMaybeImposibleThatExistsInClassToBeUsedAsNameOfPropertyOfAnyClassGiven = 'No way'; |
|
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
} |
|
|
|
|
|
|
190
|
|
|
|
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
@returndoc comment to communicate to implementors of these methods what they are expected to return.