1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Object\Test; |
4
|
|
|
|
5
|
|
|
use Dgame\Object\ObjectFacade; |
6
|
|
|
use Exception; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use ReflectionMethod; |
9
|
|
|
use ReflectionProperty; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ObjectFacadeTest |
13
|
|
|
* @package Dgame\Object\Test |
14
|
|
|
*/ |
15
|
|
|
final class ObjectFacadeTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testHasMethod() |
18
|
|
|
{ |
19
|
|
|
$this->assertTrue($this->new(Exception::class)->hasMethod('getMessage')); |
20
|
|
|
$this->assertFalse($this->new(Exception::class)->hasMethod('foo')); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testHasProperty() |
24
|
|
|
{ |
25
|
|
|
foreach (['message', 'code', 'file', 'line'] as $property) { |
26
|
|
|
$this->assertTrue($this->new(Exception::class)->hasProperty($property)); |
27
|
|
|
} |
28
|
|
|
$this->assertFalse($this->new(Exception::class)->hasProperty('foo')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testGetPropertyByName() |
32
|
|
|
{ |
33
|
|
|
foreach (['message', 'code', 'file', 'line'] as $name) { |
34
|
|
|
$property = $this->new(Exception::class)->getPropertyByName($name); |
35
|
|
|
|
36
|
|
|
$this->assertNotNull($property); |
37
|
|
|
$this->assertEquals($name, $property->getName()); |
38
|
|
|
$this->assertNotEquals(0, ReflectionProperty::IS_PROTECTED & $property->getModifiers()); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testGetMethodByName() |
43
|
|
|
{ |
44
|
|
|
$method = $this->new(Exception::class)->getMethodByName('getMessage'); |
45
|
|
|
|
46
|
|
|
$this->assertNotNull($method); |
47
|
|
|
$this->assertEquals('getMessage', $method->getName()); |
48
|
|
|
$this->assertNotEquals(0, (ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_FINAL) & $method->getModifiers()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testGetterMethod() |
52
|
|
|
{ |
53
|
|
|
$method = $this->new(Exception::class)->getGetterMethod('message'); |
54
|
|
|
|
55
|
|
|
$this->assertNotNull($method); |
56
|
|
|
$this->assertEquals('getMessage', $method->getName()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testSetterMethod() |
60
|
|
|
{ |
61
|
|
|
$facade = new ObjectFacade( |
62
|
|
|
new class() { |
63
|
|
|
public function setFoo() |
64
|
|
|
{ |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$method = $facade->getSetterMethod('foo'); |
70
|
|
|
|
71
|
|
|
$this->assertNotNull($method); |
72
|
|
|
$this->assertEquals('setFoo', $method->getName()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testGetValueByMethod() |
76
|
|
|
{ |
77
|
|
|
$exception = new Exception('Test'); |
78
|
|
|
$facade = new ObjectFacade($exception); |
79
|
|
|
|
80
|
|
|
$this->assertEquals($exception, $facade->getObject()); |
81
|
|
|
$this->assertEquals('Test', $facade->getValueByMethod('message')); |
82
|
|
|
$this->assertEquals($exception->getMessage(), $facade->getValueByMethod('message')); |
83
|
|
|
$this->assertEquals($exception->getFile(), $facade->getValueByMethod('file')); |
84
|
|
|
$this->assertEquals($exception->getLine(), $facade->getValueByMethod('line')); |
85
|
|
|
$this->assertNull($facade->getValueByMethod('unknown')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testGetValueByProperty() |
89
|
|
|
{ |
90
|
|
|
$facade = new ObjectFacade( |
91
|
|
|
new class() { |
92
|
|
|
public $foo = 42; |
93
|
|
|
public $bar = Exception::class; |
94
|
|
|
} |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$this->assertEquals(42, $facade->getValueByProperty('foo')); |
98
|
|
|
$this->assertEquals(Exception::class, $facade->getValueByProperty('bar')); |
99
|
|
|
$this->assertNull($facade->getValueByProperty('unknown')); |
100
|
|
|
$this->assertNull($this->new(Exception::class)->getValueByProperty('line')); |
101
|
|
|
$this->assertNull($this->new(Exception::class)->getValueByProperty('file')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testSetValueByMethod() |
105
|
|
|
{ |
106
|
|
|
$facade = new ObjectFacade( |
107
|
|
|
new class() { |
108
|
|
|
private $foo = 42; |
109
|
|
|
private $bar; |
110
|
|
|
|
111
|
|
|
public function setFoo(int $foo) |
112
|
|
|
{ |
113
|
|
|
$this->foo = $foo; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function setFooBar() |
117
|
|
|
{ |
118
|
|
|
$this->foo = 1; |
119
|
|
|
$this->bar = 2; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getFoo(): int |
123
|
|
|
{ |
124
|
|
|
return $this->foo; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function setBar(int $bar = null) |
128
|
|
|
{ |
129
|
|
|
$this->bar = $bar; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getBar() |
133
|
|
|
{ |
134
|
|
|
return $this->bar; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$this->assertEquals(42, $facade->getValueByMethod('foo')); |
140
|
|
|
$facade->setValueByMethod('foo', 23); |
141
|
|
|
$this->assertEquals(23, $facade->getValueByMethod('foo')); |
142
|
|
|
$facade->setValueByMethod('foo', null); |
143
|
|
|
$this->assertEquals(23, $facade->getValueByMethod('foo')); |
144
|
|
|
$facade->setValueByMethod('foo', 'abc'); |
145
|
|
|
$this->assertEquals(23, $facade->getValueByMethod('foo')); |
146
|
|
|
$facade->setValue('foo', 3537); |
147
|
|
|
$this->assertEquals(3537, $facade->getValue('foo')); |
148
|
|
|
|
149
|
|
|
$this->assertNull($facade->getValueByMethod('bar')); |
150
|
|
|
$facade->setValueByMethod('bar', 1337); |
151
|
|
|
$this->assertEquals(1337, $facade->getValueByMethod('bar')); |
152
|
|
|
$facade->setValueByMethod('bar', null); |
153
|
|
|
$this->assertNull($facade->getValueByMethod('bar')); |
154
|
|
|
|
155
|
|
|
$facade->setValueByMethod('foobar', uniqid()); |
156
|
|
|
$this->assertEquals(1, $facade->getValueByMethod('foo')); |
157
|
|
|
$this->assertEquals(2, $facade->getValueByMethod('bar')); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testSetValueByProperty() |
161
|
|
|
{ |
162
|
|
|
$facade = new ObjectFacade( |
163
|
|
|
new class() { |
164
|
|
|
public $foo = 42; |
165
|
|
|
} |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$this->assertEquals(42, $facade->getValueByProperty('foo')); |
169
|
|
|
$this->assertEquals($facade->getObject()->foo, $facade->getValueByProperty('foo')); |
170
|
|
|
$facade->setValueByProperty('foo', 23); |
171
|
|
|
$this->assertEquals(23, $facade->getValueByProperty('foo')); |
172
|
|
|
$this->assertEquals($facade->getObject()->foo, $facade->getValueByProperty('foo')); |
173
|
|
|
$facade->setValueByProperty('foo', null); |
174
|
|
|
$this->assertNull($facade->getValueByProperty('foo')); |
175
|
|
|
$this->assertEquals($facade->getObject()->foo, $facade->getValueByProperty('foo')); |
176
|
|
|
$facade->setValueByProperty('foo', 1337); |
177
|
|
|
$this->assertEquals(1337, $facade->getValueByProperty('foo')); |
178
|
|
|
$this->assertEquals($facade->getObject()->foo, $facade->getValueByProperty('foo')); |
179
|
|
|
$facade->setValue('foo', 3537); |
180
|
|
|
$this->assertEquals(3537, $facade->getValue('foo')); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function new(string $class) |
184
|
|
|
{ |
185
|
|
|
return new ObjectFacade(new $class()); |
186
|
|
|
} |
187
|
|
|
} |