1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPKitchen\Platform\Specs\Unit\Mixin; |
4
|
|
|
|
5
|
|
|
use PHPKitchen\Platform\Exception\Runtime\Property\InvalidAccessException; |
6
|
|
|
use PHPKitchen\Platform\Exception\Runtime\Property\UndefinedPropertyException; |
7
|
|
|
use PHPKitchen\Platform\Mixin\Properties; |
8
|
|
|
use PHPKitchen\Platform\Specs\Base\Spec; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Specification of {@link Properies} |
12
|
|
|
* |
13
|
|
|
* @author Dmitry Kolodko <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class PropertiesSpec extends Spec { |
16
|
|
|
protected const TEST_USER_NAME = 'Alex'; |
17
|
|
|
protected const TEST_USER_SEX = 'Male'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @test |
21
|
|
|
*/ |
22
|
|
|
public function getterAndSetterBehavior() { |
23
|
|
|
$user = $this->createClassWithProperties(); |
24
|
|
|
$I = $this->tester; |
|
|
|
|
25
|
|
|
$I->describe('behavior of read/write available properties'); |
|
|
|
|
26
|
|
|
|
27
|
|
|
$I->verifyThat('property defined by get/set methods is accessible to read and writes'); |
|
|
|
|
28
|
|
|
|
29
|
|
|
$I->expectThat('property is readable'); |
|
|
|
|
30
|
|
|
$I->lookAt('name property'); |
|
|
|
|
31
|
|
|
$I->seeString($user->name) |
|
|
|
|
32
|
|
|
->isEqualTo(self::TEST_USER_NAME); |
33
|
|
|
|
34
|
|
|
$user->name = $newName = 'Anton'; |
|
|
|
|
35
|
|
|
|
36
|
|
|
$I->expectThat('property is writable'); |
|
|
|
|
37
|
|
|
$I->lookAt('name property'); |
|
|
|
|
38
|
|
|
$I->seeString($user->name) |
|
|
|
|
39
|
|
|
->isEqualTo($newName); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
*/ |
45
|
|
|
public function readOnlyPropertyBehavior() { |
46
|
|
|
$user = $this->createClassWithProperties(); |
47
|
|
|
$I = $this->tester; |
|
|
|
|
48
|
|
|
$I->describe('behavior of read only properties'); |
|
|
|
|
49
|
|
|
|
50
|
|
|
$I->verifyThat('property defined by "get" method accessible to read but not to write'); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$I->expectThat('property is readable'); |
|
|
|
|
53
|
|
|
$I->lookAt('sex property'); |
|
|
|
|
54
|
|
|
$I->seeString($user->sex) |
|
|
|
|
55
|
|
|
->isEqualTo(self::TEST_USER_SEX); |
56
|
|
|
|
57
|
|
|
$I->expectThat('property is not writable'); |
|
|
|
|
58
|
|
|
$I->seeObject($user) |
|
|
|
|
59
|
|
|
->throwsException(InvalidAccessException::class) |
60
|
|
|
->when(function ($user) { |
61
|
|
|
$user->sex = 'Female'; |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
*/ |
68
|
|
|
public function writeOnlyPropertyBehavior() { |
69
|
|
|
$user = $this->createClassWithProperties(); |
70
|
|
|
$I = $this->tester; |
|
|
|
|
71
|
|
|
$I->describe('behavior of write only properties'); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$I->verifyThat('property defined by "set" method is accessible to write and not to read'); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$I->expectThat('property is writable'); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$user->punch = 1; |
|
|
|
|
78
|
|
|
|
79
|
|
|
$I->expectThat('property is not readable'); |
|
|
|
|
80
|
|
|
$I->seeObject($user) |
|
|
|
|
81
|
|
|
->throwsException(InvalidAccessException::class) |
82
|
|
|
->when(function ($user) { |
83
|
|
|
$p = $user->punch; |
|
|
|
|
84
|
|
|
}); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @test |
89
|
|
|
*/ |
90
|
|
|
public function undefinedPropertyBehavior() { |
91
|
|
|
$user = $this->createClassWithProperties(); |
92
|
|
|
$I = $this->tester; |
|
|
|
|
93
|
|
|
$I->describe('behavior of undefined properties access'); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$I->expectThat('reading of undefined properties is not allowed'); |
|
|
|
|
96
|
|
|
$I->seeObject($user) |
|
|
|
|
97
|
|
|
->throwsException(UndefinedPropertyException::class) |
98
|
|
|
->when(function ($user) { |
99
|
|
|
$no = $user->noProperty; |
|
|
|
|
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
$I->expectThat('writing to undefined properties is not allowed'); |
|
|
|
|
103
|
|
|
$I->seeObject($user) |
|
|
|
|
104
|
|
|
->throwsException(UndefinedPropertyException::class) |
105
|
|
|
->when(function ($user) { |
106
|
|
|
$user->noProperty = 123; |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @test |
112
|
|
|
*/ |
113
|
|
|
public function conditionalPropertiesBehavior() { |
114
|
|
|
$user = $this->createClassWithProperties(); |
115
|
|
|
$I = $this->tester; |
|
|
|
|
116
|
|
|
$I->describe('behavior of read conditional properties'); |
|
|
|
|
117
|
|
|
|
118
|
|
|
$I->verifyThat('property defined by "is" or "has" method accessible to read but not to write'); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$I->expectThat('"is*" property is readable'); |
|
|
|
|
121
|
|
|
$I->lookAt('status condition'); |
|
|
|
|
122
|
|
|
$I->seeBool($user->isActive) |
|
|
|
|
123
|
|
|
->isTrue(); |
124
|
|
|
|
125
|
|
|
$I->expectThat('"has*" property is readable'); |
|
|
|
|
126
|
|
|
$I->lookAt('name status condition'); |
|
|
|
|
127
|
|
|
$I->seeBool($user->hasName) |
|
|
|
|
128
|
|
|
->isTrue(); |
129
|
|
|
|
130
|
|
|
$I->expectThat('"is*" condition property is not writable'); |
|
|
|
|
131
|
|
|
$I->seeObject($user) |
|
|
|
|
132
|
|
|
->throwsException(UndefinedPropertyException::class) |
133
|
|
|
->when(function ($user) { |
134
|
|
|
$user->isActive = false; |
135
|
|
|
}); |
136
|
|
|
|
137
|
|
|
$I->expectThat('"has*" condition property is not writable'); |
|
|
|
|
138
|
|
|
$I->seeObject($user) |
|
|
|
|
139
|
|
|
->throwsException(UndefinedPropertyException::class) |
140
|
|
|
->when(function ($user) { |
141
|
|
|
$user->hasName = false; |
142
|
|
|
}); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
protected function createClassWithProperties() { |
146
|
|
|
return new class { |
|
|
|
|
147
|
|
|
use Properties; |
148
|
|
|
protected $_name = 'Alex'; |
|
|
|
|
149
|
|
|
protected $_sex = 'Male'; |
|
|
|
|
150
|
|
|
protected $active = true; |
151
|
|
|
|
152
|
|
|
public function getSex() { |
153
|
|
|
return $this->_sex; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getName() { |
157
|
|
|
return $this->_name; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function setName($name) { |
161
|
|
|
$this->_name = $name; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function setPunch($p) { |
|
|
|
|
165
|
|
|
// just a stub |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function isActive() { |
169
|
|
|
return $this->active; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function hasName() { |
173
|
|
|
return !empty($this->_name); |
174
|
|
|
} |
175
|
|
|
}; |
176
|
|
|
} |
177
|
|
|
} |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.