1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
<?php
require_once(dirname(__FILE__).'/common.php');
class NewComponent extends TComponent
{
private $_object=null;
private $_text='default';
private $_eventHandled=false;
public function getText()
{
return $this->_text;
}
public function setText($value)
{
$this->_text=$value;
}
public function getObject()
{
if(!$this->_object)
{
$this->_object=new NewComponent;
$this->_object->_text='object text';
}
return $this->_object;
}
public function onMyEvent($param)
{
$this->raiseEvent('MyEvent',$this,$param);
}
public function myEventHandler($sender,$param)
{
$this->_eventHandled=true;
}
public function isEventHandled()
{
return $this->_eventHandled;
}
}
class utComponent extends UnitTestCase
{
protected $component;
public function setUp()
{
$this->component=new NewComponent;
}
public function tearDown()
{
$this->component=null;
}
public function testHasProperty()
{
$this->assertTrue($this->component->hasProperty('Text'));
$this->assertTrue($this->component->hasProperty('text'));
$this->assertFalse($this->component->hasProperty('Caption'));
}
public function testCanGetProperty()
{
$this->assertTrue($this->component->canGetProperty('Text'));
$this->assertTrue($this->component->canGetProperty('text'));
$this->assertFalse($this->component->canGetProperty('Caption'));
}
public function testCanSetProperty()
{
$this->assertTrue($this->component->canSetProperty('Text'));
$this->assertTrue($this->component->canSetProperty('text'));
$this->assertFalse($this->component->canSetProperty('Caption'));
}
public function testGetProperty()
{
$this->assertTrue('default'===$this->component->Text);
try
{
$value2=$this->component->Caption;
$this->fail('exception not raised when getting undefined property');
}
catch(TInvalidOperationException $e)
{
$this->pass();
}
}
public function testSetProperty()
{
$value='new value';
$this->component->Text=$value;
$text=$this->component->Text;
$this->assertTrue($value===$this->component->Text);
try
{
$this->component->NewMember=$value;
$this->fail('exception not raised when setting undefined property');
}
catch(TInvalidOperationException $e)
{
$this->pass();
}
}
public function testGetPropertyByPath()
{
$this->assertTrue('object text'===$this->component->getPropertyByPath('Object.Text'));
}
public function testSetPropertyByPath()
{
$this->component->setPropertyByPath('Object.Text','new object text');
$this->assertEqual('new object text',$this->component->getPropertyByPath('Object.Text'));
}
public function testHasEvent()
{
$this->assertTrue($this->component->hasEvent('MyEvent'));
$this->assertTrue($this->component->hasEvent('myevent'));
$this->assertFalse($this->component->hasEvent('YourEvent'));
}
public function testHasEventHandler()
{
$this->assertFalse($this->component->hasEventHandler('MyEvent'));
$this->component->attachEventHandler('MyEvent','foo');
$this->assertTrue($this->component->hasEventHandler('MyEvent'));
}
public function testGetEventHandlers()
{
$list=$this->component->getEventHandlers('MyEvent');
$this->assertTrue(($list instanceof TList) && ($list->getCount()===0));
$this->component->attachEventHandler('MyEvent','foo');
$this->assertTrue(($list instanceof TList) && ($list->getCount()===1));
try
{
$list=$this->component->getEventHandlers('YourEvent');
$this->fail('exception not raised when getting event handlers for undefined event');
}
catch(TInvalidOperationException $e)
{
$this->pass();
}
}
public function testAttachEventHandler()
{
$this->component->attachEventHandler('MyEvent','foo');
$this->assertTrue($this->component->getEventHandlers('MyEvent')->getCount()===1);
try
{
$this->component->attachEventHandler('YourEvent','foo');
$this->fail('exception not raised when attaching event handlers for undefined event');
}
catch(TInvalidOperationException $e)
{
$this->pass();
}
$this->component->MyEvent[]='foo2';
$this->assertTrue($this->component->getEventHandlers('MyEvent')->getCount()===2);
$this->component->getEventHandlers('MyEvent')->add('foo3');
$this->assertTrue($this->component->getEventHandlers('MyEvent')->getCount()===3);
$this->component->MyEvent[0]='foo4';
$this->assertTrue($this->component->getEventHandlers('MyEvent')->getCount()===3);
$this->component->getEventHandlers('MyEvent')->addAt(0,'foo5');
$this->assertTrue($this->component->MyEvent->Count===4 && $this->component->MyEvent[0]==='foo5');
$this->component->MyEvent='foo6';
$this->assertTrue($this->component->MyEvent->Count===5 && $this->component->MyEvent[4]==='foo6');
}
public function testRaiseEvent()
{
$this->component->attachEventHandler('MyEvent',array($this->component,'myEventHandler'));
$this->assertFalse($this->component->isEventHandled());
$this->component->raiseEvent('MyEvent',$this,null);
$this->assertTrue($this->component->isEventHandled());
$this->component->attachEventHandler('MyEvent',array($this->component,'Object.myEventHandler'));
$this->assertFalse($this->component->Object->isEventHandled());
$this->component->raiseEvent('MyEvent',$this,null);
$this->assertTrue($this->component->Object->isEventHandled());
}
public function testEvaluateExpression()
{
$expression="1+2";
$this->assertTrue(3===$this->component->evaluateExpression($expression));
try
{
$button=$this->component->evaluateExpression('$this->button');
$this->fail('exception not raised when evaluating an invalid exception');
}
catch(Exception $e)
{
$this->pass();
}
}
public function testEvaluateStatements()
{
$statements='$a="test string"; echo $a;';
$this->assertEqual('test string',$this->component->evaluateStatements($statements));
try
{
$statements='$a=new NewComponent; echo $a->button;';
$button=$this->component->evaluateStatements($statements);
$this->fail('exception not raised when evaluating an invalid statement');
}
catch(Exception $e)
{
$this->pass();
}
}
}
?>
|