1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP: Nelson Martell Library file |
4
|
|
|
* |
5
|
|
|
* Content: |
6
|
|
|
* - Trait definition |
7
|
|
|
* |
8
|
|
|
* Copyright © 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 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
|
|
|
use BadMethodCallException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Split of ImplementsIStrictPropertiesContainer, for classes implementing any write-only property. |
30
|
|
|
* |
31
|
|
|
* @author Nelson Martell <[email protected]> |
32
|
|
|
* */ |
33
|
|
|
trait HasReadOnlyProperties |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* ImplementsIStrictPropertiesContainer trait provides this method implementation. |
37
|
|
|
* |
38
|
|
|
* @returns IStrictPropertiesContainer |
39
|
|
|
* @see ImplementsIStrictPropertiesContainer::testImplementsIStrictPropertiesContainerInterface() |
40
|
|
|
*/ |
41
|
|
|
public abstract function testImplementsIStrictPropertiesContainerInterface($obj); |
|
|
|
|
42
|
|
|
|
43
|
|
|
public abstract function readonlyPropertiesProvider(); |
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @depends testImplementsIStrictPropertiesContainerInterface |
48
|
|
|
* |
49
|
|
|
* @dataProvider readonlyPropertiesProvider |
50
|
|
|
*/ |
51
|
|
|
public function testReadonlyPropertiesAreReadables( |
52
|
|
|
IStrictPropertiesContainer $obj, |
53
|
|
|
$property, |
54
|
|
|
$expected |
55
|
|
|
) { |
56
|
|
|
try { |
57
|
|
|
$actual = $obj->$property; |
58
|
|
|
} catch (BadMethodCallException $e) { |
|
|
|
|
59
|
|
|
$message = Text::format( |
60
|
|
|
'Property `{1}` it should be accessible, but does it throws an exception: "{2}".', |
61
|
|
|
get_class($obj), |
62
|
|
|
$property, |
63
|
|
|
$e->getMessage() |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$this->fail($message); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$exporter = new Exporter(); |
70
|
|
|
|
71
|
|
|
$var = get_class($obj); |
72
|
|
|
$var = Inflector::variable(substr( |
73
|
|
|
$var, |
74
|
|
|
strrpos($var, '\\') === false ? 0 : strrpos($var, '\\') + 1 |
75
|
|
|
)); |
76
|
|
|
|
77
|
|
|
$message = Text::format( |
78
|
|
|
'$actual = ${var}->{property}; // {actual}', |
79
|
|
|
[ |
80
|
|
|
'var' => $var, |
81
|
|
|
'property' => $property, |
82
|
|
|
'actual' => $exporter->shortenedExport($actual) |
83
|
|
|
] |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$this->assertEquals($expected, $actual, $message); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @depends testImplementsIStrictPropertiesContainerInterface |
91
|
|
|
* @dataProvider readonlyPropertiesProvider |
92
|
|
|
* @expectedException \BadMethodCallException |
93
|
|
|
*/ |
94
|
|
|
public function testReadonlyPropertiesAreNotWritables( |
95
|
|
|
IStrictPropertiesContainer $obj = null, |
96
|
|
|
$property = null, |
97
|
|
|
$value = null |
98
|
|
|
) { |
99
|
|
|
$obj->$property = $value; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.