This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
FaultTest.php
253 lines (216 loc) · 8.2 KB
/
FaultTest.php
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_XmlRpc
*/
namespace ZendTest\XmlRpc\Server;
use Zend\XmlRpc\Server;
/**
* @category Zend
* @package Zend_XmlRpc
* @subpackage UnitTests
* @group Zend_XmlRpc
*/
class FaultTest extends \PHPUnit_Framework_TestCase
{
/**
* Zend\XmlRpc\Server\Fault::getInstance() test
*/
public function testGetInstance()
{
$e = new Server\Exception\RuntimeException('Testing fault', 411);
$fault = Server\Fault::getInstance($e);
$this->assertTrue($fault instanceof Server\Fault);
}
/**
* Zend\XmlRpc\Server\Fault::attachFaultException() test
*/
public function testAttachFaultException()
{
Server\Fault::attachFaultException('ZendTest\\XmlRpc\\Server\\Exception');
$e = new Exception('test exception', 411);
$fault = Server\Fault::getInstance($e);
$this->assertEquals('test exception', $fault->getMessage());
$this->assertEquals(411, $fault->getCode());
Server\Fault::detachFaultException('ZendTest\\XmlRpc\\Server\\Exception');
$exceptions = array(
'ZendTest\\XmlRpc\\Server\\Exception',
'ZendTest\\XmlRpc\\Server\\Exception2',
'ZendTest\\XmlRpc\\Server\\Exception3',
);
Server\Fault::attachFaultException($exceptions);
foreach ($exceptions as $class) {
$e = new $class('test exception', 411);
$fault = Server\Fault::getInstance($e);
$this->assertEquals('test exception', $fault->getMessage());
$this->assertEquals(411, $fault->getCode());
}
Server\Fault::detachFaultException($exceptions);
}
/**
* Tests ZF-1825
* @return void
*/
public function testAttachFaultExceptionAllowsForDerivativeExceptionClasses()
{
Server\Fault::attachFaultException('ZendTest\\XmlRpc\\Server\\Exception');
$e = new Exception4('test exception', 411);
$fault = Server\Fault::getInstance($e);
$this->assertEquals('test exception', $fault->getMessage());
$this->assertEquals(411, $fault->getCode());
Server\Fault::detachFaultException('ZendTest\\XmlRpc\\Server\\Exception');
}
/**
* Zend_XmlRpc_Server_Fault::detachFaultException() test
*/
public function testDetachFaultException()
{
Server\Fault::attachFaultException('ZendTest\\XmlRpc\\Server\\Exception');
$e = new Exception('test exception', 411);
$fault = Server\Fault::getInstance($e);
$this->assertEquals('test exception', $fault->getMessage());
$this->assertEquals(411, $fault->getCode());
Server\Fault::detachFaultException('ZendTest\\XmlRpc\\Server\\Exception');
$fault = Server\Fault::getInstance($e);
$this->assertEquals('Unknown error', $fault->getMessage());
$this->assertEquals(404, $fault->getCode());
$exceptions = array(
'ZendTest\\XmlRpc\\Server\\Exception',
'ZendTest\\XmlRpc\\Server\\Exception2',
'ZendTest\\XmlRpc\\Server\\Exception3'
);
Server\Fault::attachFaultException($exceptions);
foreach ($exceptions as $class) {
$e = new $class('test exception', 411);
$fault = Server\Fault::getInstance($e);
$this->assertEquals('test exception', $fault->getMessage());
$this->assertEquals(411, $fault->getCode());
}
Server\Fault::detachFaultException($exceptions);
foreach ($exceptions as $class) {
$e = new $class('test exception', 411);
$fault = Server\Fault::getInstance($e);
$this->assertEquals('Unknown error', $fault->getMessage());
$this->assertEquals(404, $fault->getCode());
}
}
/**
* Zend_XmlRpc_Server_Fault::attachObserver() test
*/
public function testAttachObserver()
{
Server\Fault::attachObserver('ZendTest\\XmlRpc\\Server\\Observer');
$e = new Server\Exception\RuntimeException('Checking observers', 411);
$fault = Server\Fault::getInstance($e);
$observed = Observer::getObserved();
Observer::clearObserved();
Server\Fault::detachObserver('ZendTest\\XmlRpc\\Server\\Observer');
$this->assertTrue(!empty($observed));
$f = array_shift($observed);
$this->assertTrue($f instanceof Server\Fault);
$this->assertEquals('Checking observers', $f->getMessage());
$this->assertEquals(411, $f->getCode());
$this->assertFalse(Server\Fault::attachObserver('foo'));
}
/**
* Zend\XmlRpc\Server\Fault::detachObserver() test
*/
public function testDetachObserver()
{
Server\Fault::attachObserver('ZendTest\\XmlRpc\\Server\\Observer');
$e = new Server\Exception\RuntimeException('Checking observers', 411);
$fault = Server\Fault::getInstance($e);
Observer::clearObserved();
Server\Fault::detachObserver('ZendTest\\XmlRpc\\Server\\Observer');
$e = new Server\Exception\RuntimeException('Checking observers', 411);
$fault = Server\Fault::getInstance($e);
$observed = Observer::getObserved();
$this->assertTrue(empty($observed));
$this->assertFalse(Server\Fault::detachObserver('foo'));
}
/**
* getCode() test
*/
public function testGetCode()
{
$e = new Server\Exception\RuntimeException('Testing fault', 411);
$fault = Server\Fault::getInstance($e);
$this->assertEquals(411, $fault->getCode());
}
/**
* getException() test
*/
public function testGetException()
{
$e = new Server\Exception\RuntimeException('Testing fault', 411);
$fault = Server\Fault::getInstance($e);
$this->assertSame($e, $fault->getException());
}
/**
* getMessage() test
*/
public function testGetMessage()
{
$e = new Server\Exception\RuntimeException('Testing fault', 411);
$fault = Server\Fault::getInstance($e);
$this->assertEquals('Testing fault', $fault->getMessage());
}
/**
* __toString() test
*/
public function test__toString()
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$r = $dom->appendChild($dom->createElement('methodResponse'));
$f = $r->appendChild($dom->createElement('fault'));
$v = $f->appendChild($dom->createElement('value'));
$s = $v->appendChild($dom->createElement('struct'));
$m1 = $s->appendChild($dom->createElement('member'));
$m1->appendChild($dom->createElement('name', 'faultCode'));
$cv = $m1->appendChild($dom->createElement('value'));
$cv->appendChild($dom->createElement('int', 411));
$m2 = $s->appendChild($dom->createElement('member'));
$m2->appendChild($dom->createElement('name', 'faultString'));
$sv = $m2->appendChild($dom->createElement('value'));
$sv->appendChild($dom->createElement('string', 'Testing fault'));
$xml = $dom->saveXML();
$e = new Server\Exception\RuntimeException('Testing fault', 411);
$fault = Server\Fault::getInstance($e);
$this->assertEquals(trim($xml), trim($fault->__toString()));
}
}
class Exception extends \Exception {}
class Exception2 extends \Exception {}
class Exception3 extends \Exception {}
class Exception4 extends Exception {}
class Observer
{
private static $_instance = false;
public $observed = array();
private function __construct()
{
}
public static function getInstance()
{
if (!self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
public static function observe(Server\Fault $fault)
{
self::getInstance()->observed[] = $fault;
}
public static function clearObserved()
{
self::getInstance()->observed = array();
}
public static function getObserved()
{
return self::getInstance()->observed;
}
}