|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: benedikt |
|
6
|
|
|
* Date: 1/3/18 |
|
7
|
|
|
* Time: 12:29 PM |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Tfboe\FmLib\Tests\Unit\Entity\Helpers; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
use Tfboe\FmLib\Entity\Helpers\SubClassData; |
|
14
|
|
|
use Tfboe\FmLib\Exceptions\MethodNotExistingException; |
|
15
|
|
|
use Tfboe\FmLib\Exceptions\PropertyNotExistingException; |
|
16
|
|
|
use Tfboe\FmLib\Tests\Helpers\UnitTestCase; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class SubClassDataTest |
|
20
|
|
|
* @package Tfboe\FmLib\Tests\Unit\Entity\Helpers |
|
21
|
|
|
*/ |
|
22
|
|
|
class SubClassDataTest extends UnitTestCase |
|
23
|
|
|
{ |
|
24
|
|
|
//<editor-fold desc="Public Methods"> |
|
25
|
|
|
/** |
|
26
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::addPropertyIfNotExistent |
|
27
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::__call |
|
28
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::getProperty |
|
29
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::hasProperty |
|
30
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
31
|
|
|
*/ |
|
32
|
|
|
public function testAddPropertyIfNotExistent() |
|
33
|
|
|
{ |
|
34
|
|
|
$entity = $this->mock(); |
|
35
|
|
|
$entity->initSubClassData([]); |
|
36
|
|
|
self::assertFalse($entity->hasProperty("prop")); |
|
37
|
|
|
$entity->addPropertyIfNotExistent("prop", "default"); |
|
38
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
39
|
|
|
self::assertEquals("default", $entity->getProp()); |
|
|
|
|
|
|
40
|
|
|
$entity->addPropertyIfNotExistent("prop", "other"); |
|
41
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
42
|
|
|
self::assertEquals("default", $entity->getProp()); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::__call |
|
47
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::getProperty |
|
48
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::setProperty |
|
49
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::hasProperty |
|
50
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testCall() |
|
53
|
|
|
{ |
|
54
|
|
|
$entity = $this->mock(); |
|
55
|
|
|
$entity->initSubClassData(["Prop"]); |
|
56
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
57
|
|
|
self::assertNull($entity->getProp()); |
|
|
|
|
|
|
58
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
59
|
|
|
$entity->setProp("test"); |
|
|
|
|
|
|
60
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
61
|
|
|
self::assertEquals("test", $entity->getProp()); |
|
|
|
|
|
|
62
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
63
|
|
|
self::assertEquals("test", $entity->isProp()); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::cloneSubClassDataFrom |
|
68
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::hasProperty |
|
69
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testCloneSubClassDataFrom() |
|
72
|
|
|
{ |
|
73
|
|
|
$entity = $this->mock(); |
|
74
|
|
|
$other = $this->getMockForTrait(SubClassData::class, [], get_class($entity)); |
|
75
|
|
|
/** @var SubClassData $other */ |
|
76
|
|
|
$other->initSubClassData(['test']); |
|
77
|
|
|
$entity->initSubClassData(['other']); |
|
78
|
|
|
self::assertFalse($entity->hasProperty('test')); |
|
79
|
|
|
self::assertTrue($entity->hasProperty('other')); |
|
80
|
|
|
$entity->cloneSubClassDataFrom($other); |
|
81
|
|
|
self::assertTrue($entity->hasProperty('test')); |
|
82
|
|
|
self::assertFalse($entity->hasProperty('other')); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
87
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::hasProperty |
|
88
|
|
|
*/ |
|
89
|
|
View Code Duplication |
public function testInitSubClassDataAndHasProperty() |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
$entity = $this->mock(); |
|
92
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
93
|
|
|
$entity->initSubClassData(["TESTUPPER", "testlower"]); |
|
94
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
95
|
|
|
self::assertTrue($entity->hasProperty("testupper")); |
|
96
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
97
|
|
|
self::assertTrue($entity->hasProperty("TESTUPPER")); |
|
98
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
99
|
|
|
self::assertTrue($entity->hasProperty("TESTupper")); |
|
100
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
101
|
|
|
self::assertTrue($entity->hasProperty("testlower")); |
|
102
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
103
|
|
|
self::assertTrue($entity->hasProperty("TESTLOWER")); |
|
104
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
105
|
|
|
self::assertTrue($entity->hasProperty("TESTlower")); |
|
106
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
107
|
|
|
self::assertFalse($entity->hasProperty("notexisting")); |
|
108
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
109
|
|
|
self::assertFalse($entity->hasProperty("NOTEXISTING")); |
|
110
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
111
|
|
|
self::assertFalse($entity->hasProperty("NOTexistING")); |
|
112
|
|
|
self::assertTrue($entity->hasProperty("subClassData")); |
|
113
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
114
|
|
|
self::assertFalse($entity->hasProperty("SUBCLASSDATA")); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::methodExists |
|
119
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::addPropertyIfNotExistent |
|
120
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::hasProperty |
|
121
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
122
|
|
|
*/ |
|
123
|
|
View Code Duplication |
public function testMethodExists() |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
$entity = $this->mock(); |
|
126
|
|
|
$entity->initSubClassData(["test"]); |
|
127
|
|
|
self::assertTrue($entity->methodExists("methodExists")); |
|
128
|
|
|
self::assertFalse($entity->methodExists("notExistingMethod")); |
|
129
|
|
|
self::assertFalse($entity->methodExists("getProp")); |
|
130
|
|
|
self::assertFalse($entity->methodExists("isProp")); |
|
131
|
|
|
self::assertFalse($entity->methodExists("setProp")); |
|
132
|
|
|
self::assertTrue($entity->methodExists("getTest")); |
|
133
|
|
|
self::assertTrue($entity->methodExists("isTest")); |
|
134
|
|
|
self::assertTrue($entity->methodExists("setTest")); |
|
135
|
|
|
$entity->addPropertyIfNotExistent("prop", null); |
|
136
|
|
|
self::assertTrue($entity->methodExists("getProp")); |
|
137
|
|
|
self::assertTrue($entity->methodExists("isProp")); |
|
138
|
|
|
self::assertTrue($entity->methodExists("setProp")); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::__call |
|
143
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
144
|
|
|
* @uses \Tfboe\FmLib\Exceptions\MethodNotExistingException::__construct |
|
145
|
|
|
*/ |
|
146
|
|
View Code Duplication |
public function testNotExistingMethodCall() |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
|
|
$entity = $this->mock(); |
|
149
|
|
|
$entity->initSubClassData([]); |
|
150
|
|
|
$this->expectException(MethodNotExistingException::class); |
|
151
|
|
|
$this->expectExceptionMessage("An object of the class " . get_class($entity) . " had no method notExistingMethod"); |
|
152
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
153
|
|
|
$entity->notExistingMethod(); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::__call |
|
158
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
159
|
|
|
* @uses \Tfboe\FmLib\Exceptions\MethodNotExistingException::__construct |
|
160
|
|
|
*/ |
|
161
|
|
View Code Duplication |
public function testNotExistingMethodSetterNoArgument() |
|
|
|
|
|
|
162
|
|
|
{ |
|
163
|
|
|
$entity = $this->mock(); |
|
164
|
|
|
$entity->initSubClassData([]); |
|
165
|
|
|
$this->expectException(MethodNotExistingException::class); |
|
166
|
|
|
$this->expectExceptionMessage("An object of the class " . get_class($entity) . " had no method setProp"); |
|
167
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
168
|
|
|
$entity->setProp(); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::__call |
|
173
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::getProperty |
|
174
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
175
|
|
|
* @uses \Tfboe\FmLib\Exceptions\PropertyNotExistingException::__construct |
|
176
|
|
|
*/ |
|
177
|
|
View Code Duplication |
public function testNotExistingPropertyGetCall() |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
|
|
$entity = $this->mock(); |
|
180
|
|
|
$entity->initSubClassData([]); |
|
181
|
|
|
$this->expectException(PropertyNotExistingException::class); |
|
182
|
|
|
$this->expectExceptionMessage("An object of the class " . get_class($entity) . |
|
183
|
|
|
" had no property prop via getProperty"); |
|
184
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
185
|
|
|
$entity->getProp(); |
|
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::__call |
|
190
|
|
|
* @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::setProperty |
|
191
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
192
|
|
|
* @uses \Tfboe\FmLib\Exceptions\PropertyNotExistingException::__construct |
|
193
|
|
|
*/ |
|
194
|
|
View Code Duplication |
public function testNotExistingPropertySetCall() |
|
|
|
|
|
|
195
|
|
|
{ |
|
196
|
|
|
$entity = $this->mock(); |
|
197
|
|
|
$entity->initSubClassData([]); |
|
198
|
|
|
$this->expectException(PropertyNotExistingException::class); |
|
199
|
|
|
$this->expectExceptionMessage("An object of the class " . get_class($entity) . |
|
200
|
|
|
" had no property prop via setProperty"); |
|
201
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
202
|
|
|
$entity->setProp(5); |
|
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
//</editor-fold desc="Public Methods"> |
|
205
|
|
|
|
|
206
|
|
|
//<editor-fold desc="Private Methods"> |
|
207
|
|
|
/** |
|
208
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|SubClassData |
|
209
|
|
|
*/ |
|
210
|
|
|
private function mock(): \PHPUnit_Framework_MockObject_MockObject |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->getMockForTrait(SubClassData::class); |
|
213
|
|
|
} |
|
214
|
|
|
//</editor-fold desc="Private Methods"> |
|
215
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.