|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fwk\Db; |
|
4
|
|
|
|
|
5
|
|
|
class TestObj |
|
6
|
|
|
{ |
|
7
|
|
|
public $test; |
|
8
|
|
|
|
|
9
|
|
|
protected $protected; |
|
10
|
|
|
|
|
11
|
|
|
private $private; |
|
12
|
|
|
|
|
13
|
|
|
private $privWithoutGetter; |
|
14
|
|
|
|
|
15
|
|
|
private $privWithoutSetter; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
public function getProtected() |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
return $this->protected; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function setProtected($protected) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->protected = $protected; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getPrivate() |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
return $this->private; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function setPrivate($private) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->private = $private; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function setPrivWithoutGetter($privWithoutGetter) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->privWithoutGetter = $privWithoutGetter; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
class AATestObj extends \ArrayObject |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
public function __construct($array) |
|
46
|
|
|
{ |
|
47
|
|
|
parent::__construct($array); |
|
48
|
|
|
$this->setFlags(\ArrayObject::ARRAY_AS_PROPS); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Test class for Accessor. |
|
54
|
|
|
* Generated by PHPUnit on 2012-05-27 at 17:46:42. |
|
55
|
|
|
*/ |
|
56
|
|
|
class AccessorTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
/** |
|
59
|
|
|
* @var Accessor |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $object; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var TestObj |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $testable; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
70
|
|
|
* This method is called before a test is executed. |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function setUp() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->testable = new TestObj(); |
|
75
|
|
|
$this->testable->test = "testing"; |
|
76
|
|
|
$this->testable->setProtected("protectedValue"); |
|
77
|
|
|
$this->testable->setPrivate("privateValue"); |
|
78
|
|
|
$this->testable->setPrivWithoutGetter("noGetter"); |
|
79
|
|
|
|
|
80
|
|
|
$this->object = new Accessor($this->testable); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function tearDown() |
|
84
|
|
|
{ |
|
85
|
|
|
unset($this->object); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
*/ |
|
90
|
|
|
public function testGetAA() |
|
91
|
|
|
{ |
|
92
|
|
|
$aaObj = new AATestObj(array( |
|
93
|
|
|
'set' => true, |
|
94
|
|
|
'test' => 'coucou' |
|
95
|
|
|
)); |
|
96
|
|
|
$this->object = new Accessor($aaObj); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertEquals(true, $this->object->get('set')); |
|
99
|
|
|
$this->assertEquals("coucou", $this->object->get('test')); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
*/ |
|
104
|
|
|
public function testSetAA() |
|
105
|
|
|
{ |
|
106
|
|
|
$aaObj = new AATestObj(array( |
|
107
|
|
|
'set' => true, |
|
108
|
|
|
'test' => 'coucou' |
|
109
|
|
|
)); |
|
110
|
|
|
$this->object = new Accessor($aaObj); |
|
111
|
|
|
$this->assertTrue($this->object->set('testing', 'ok')); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
*/ |
|
116
|
|
|
public function testGet() |
|
117
|
|
|
{ |
|
118
|
|
|
$this->assertEquals("testing", $this->object->get('test')); |
|
119
|
|
|
$this->assertEquals("privateValue", $this->object->get('private')); |
|
120
|
|
|
$this->assertEquals("protectedValue", $this->object->get('protected')); |
|
121
|
|
|
|
|
122
|
|
|
$this->assertFalse($this->object->get('privWithoutGetter')); |
|
123
|
|
|
|
|
124
|
|
|
// test with override should fetch the value |
|
125
|
|
|
$this->object->overrideVisibility(true); |
|
126
|
|
|
$this->assertEquals("noGetter", $this->object->get('privWithoutGetter')); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
*/ |
|
131
|
|
|
public function testSet() |
|
132
|
|
|
{ |
|
133
|
|
|
$this->assertFalse($this->object->set('testing', 'nonExistant')); |
|
134
|
|
|
$this->assertTrue($this->object->set('test', "testeuh")); |
|
135
|
|
|
$this->assertTrue($this->object->set('private', 'priv')); |
|
136
|
|
|
$this->assertTrue($this->object->set('protected', 'prot')); |
|
137
|
|
|
$this->assertFalse($this->object->set('privWithoutSetter', 'shouldFail')); |
|
138
|
|
|
|
|
139
|
|
|
// test with override should set the value |
|
140
|
|
|
$this->object->overrideVisibility(true); |
|
141
|
|
|
$this->assertTrue($this->object->set('privWithoutSetter', "newValue")); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
*/ |
|
146
|
|
|
public function testGetReflector() |
|
147
|
|
|
{ |
|
148
|
|
|
$this->assertInstanceOf('\ReflectionObject', $this->object->getReflector()); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
*/ |
|
153
|
|
|
public function testToArrayAndSetValues() |
|
154
|
|
|
{ |
|
155
|
|
|
$values = array( |
|
156
|
|
|
'test' => "testeuh", |
|
157
|
|
|
"private" => "priv", |
|
158
|
|
|
"protected" => "prot", |
|
159
|
|
|
"privWithoutGetter" => "coucou" |
|
160
|
|
|
); |
|
161
|
|
|
|
|
162
|
|
|
$this->object->setValues($values); |
|
163
|
|
|
|
|
164
|
|
|
$result = array( |
|
165
|
|
|
"test" => "testeuh", |
|
166
|
|
|
"private" => "priv", |
|
167
|
|
|
"protected" => "prot", |
|
168
|
|
|
"privWithoutGetter" => false, |
|
169
|
|
|
"privWithoutSetter" => false |
|
170
|
|
|
); |
|
171
|
|
|
|
|
172
|
|
|
$this->assertEquals($result, $this->object->toArray()); |
|
173
|
|
|
|
|
174
|
|
|
$this->object->overrideVisibility(true); |
|
175
|
|
|
$result['privWithoutGetter'] = "coucou"; |
|
176
|
|
|
|
|
177
|
|
|
$this->assertEquals($result, $this->object->toArray()); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
*/ |
|
182
|
|
|
public function testHashCode() |
|
183
|
|
|
{ |
|
184
|
|
|
$obj = new \stdClass; |
|
185
|
|
|
$obj->test = "pwet"; |
|
186
|
|
|
|
|
187
|
|
|
$firstHash = $this->object->hashCode(); |
|
188
|
|
|
$this->assertEquals($firstHash,$this->object->hashCode()); |
|
189
|
|
|
$values = array( |
|
190
|
|
|
'test' => "testeuh", |
|
191
|
|
|
"private" => array('coucou' => 'pwet'), |
|
192
|
|
|
"protected" => "prot", |
|
193
|
|
|
"privWithoutGetter" => $obj |
|
194
|
|
|
); |
|
195
|
|
|
|
|
196
|
|
|
$this->object->setValues($values); |
|
197
|
|
|
$this->assertNotEquals($firstHash,$this->object->hashCode()); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
*/ |
|
202
|
|
|
public function testFactory() |
|
203
|
|
|
{ |
|
204
|
|
|
$this->assertEquals($this->object, Accessor::factory($this->testable)); |
|
205
|
|
|
$this->setExpectedException('\InvalidArgumentException'); |
|
206
|
|
|
Accessor::factory('stringArgumentIsInvalid'); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.