PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/ZendTest/XmlRpc/Server/FaultTest.php

https://github.com/EvanDotPro/zf2
PHP | 249 lines | 171 code | 37 blank | 41 comment | 1 complexity | 0d1c684c89e18dcf94be60e01f22045a MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\XmlRpc\Server;
  10. use Zend\XmlRpc\Server;
  11. /**
  12. * @group Zend_XmlRpc
  13. */
  14. class FaultTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * Zend\XmlRpc\Server\Fault::getInstance() test
  18. */
  19. public function testGetInstance()
  20. {
  21. $e = new Server\Exception\RuntimeException('Testing fault', 411);
  22. $fault = Server\Fault::getInstance($e);
  23. $this->assertTrue($fault instanceof Server\Fault);
  24. }
  25. /**
  26. * Zend\XmlRpc\Server\Fault::attachFaultException() test
  27. */
  28. public function testAttachFaultException()
  29. {
  30. Server\Fault::attachFaultException('ZendTest\\XmlRpc\\Server\\Exception');
  31. $e = new Exception('test exception', 411);
  32. $fault = Server\Fault::getInstance($e);
  33. $this->assertEquals('test exception', $fault->getMessage());
  34. $this->assertEquals(411, $fault->getCode());
  35. Server\Fault::detachFaultException('ZendTest\\XmlRpc\\Server\\Exception');
  36. $exceptions = array(
  37. 'ZendTest\\XmlRpc\\Server\\Exception',
  38. 'ZendTest\\XmlRpc\\Server\\Exception2',
  39. 'ZendTest\\XmlRpc\\Server\\Exception3',
  40. );
  41. Server\Fault::attachFaultException($exceptions);
  42. foreach ($exceptions as $class) {
  43. $e = new $class('test exception', 411);
  44. $fault = Server\Fault::getInstance($e);
  45. $this->assertEquals('test exception', $fault->getMessage());
  46. $this->assertEquals(411, $fault->getCode());
  47. }
  48. Server\Fault::detachFaultException($exceptions);
  49. }
  50. /**
  51. * Tests ZF-1825
  52. * @return void
  53. */
  54. public function testAttachFaultExceptionAllowsForDerivativeExceptionClasses()
  55. {
  56. Server\Fault::attachFaultException('ZendTest\\XmlRpc\\Server\\Exception');
  57. $e = new Exception4('test exception', 411);
  58. $fault = Server\Fault::getInstance($e);
  59. $this->assertEquals('test exception', $fault->getMessage());
  60. $this->assertEquals(411, $fault->getCode());
  61. Server\Fault::detachFaultException('ZendTest\\XmlRpc\\Server\\Exception');
  62. }
  63. /**
  64. * Zend_XmlRpc_Server_Fault::detachFaultException() test
  65. */
  66. public function testDetachFaultException()
  67. {
  68. Server\Fault::attachFaultException('ZendTest\\XmlRpc\\Server\\Exception');
  69. $e = new Exception('test exception', 411);
  70. $fault = Server\Fault::getInstance($e);
  71. $this->assertEquals('test exception', $fault->getMessage());
  72. $this->assertEquals(411, $fault->getCode());
  73. Server\Fault::detachFaultException('ZendTest\\XmlRpc\\Server\\Exception');
  74. $fault = Server\Fault::getInstance($e);
  75. $this->assertEquals('Unknown error', $fault->getMessage());
  76. $this->assertEquals(404, $fault->getCode());
  77. $exceptions = array(
  78. 'ZendTest\\XmlRpc\\Server\\Exception',
  79. 'ZendTest\\XmlRpc\\Server\\Exception2',
  80. 'ZendTest\\XmlRpc\\Server\\Exception3'
  81. );
  82. Server\Fault::attachFaultException($exceptions);
  83. foreach ($exceptions as $class) {
  84. $e = new $class('test exception', 411);
  85. $fault = Server\Fault::getInstance($e);
  86. $this->assertEquals('test exception', $fault->getMessage());
  87. $this->assertEquals(411, $fault->getCode());
  88. }
  89. Server\Fault::detachFaultException($exceptions);
  90. foreach ($exceptions as $class) {
  91. $e = new $class('test exception', 411);
  92. $fault = Server\Fault::getInstance($e);
  93. $this->assertEquals('Unknown error', $fault->getMessage());
  94. $this->assertEquals(404, $fault->getCode());
  95. }
  96. }
  97. /**
  98. * Zend_XmlRpc_Server_Fault::attachObserver() test
  99. */
  100. public function testAttachObserver()
  101. {
  102. Server\Fault::attachObserver('ZendTest\\XmlRpc\\Server\\Observer');
  103. $e = new Server\Exception\RuntimeException('Checking observers', 411);
  104. $fault = Server\Fault::getInstance($e);
  105. $observed = Observer::getObserved();
  106. Observer::clearObserved();
  107. Server\Fault::detachObserver('ZendTest\\XmlRpc\\Server\\Observer');
  108. $this->assertTrue(!empty($observed));
  109. $f = array_shift($observed);
  110. $this->assertTrue($f instanceof Server\Fault);
  111. $this->assertEquals('Checking observers', $f->getMessage());
  112. $this->assertEquals(411, $f->getCode());
  113. $this->assertFalse(Server\Fault::attachObserver('foo'));
  114. }
  115. /**
  116. * Zend\XmlRpc\Server\Fault::detachObserver() test
  117. */
  118. public function testDetachObserver()
  119. {
  120. Server\Fault::attachObserver('ZendTest\\XmlRpc\\Server\\Observer');
  121. $e = new Server\Exception\RuntimeException('Checking observers', 411);
  122. $fault = Server\Fault::getInstance($e);
  123. Observer::clearObserved();
  124. Server\Fault::detachObserver('ZendTest\\XmlRpc\\Server\\Observer');
  125. $e = new Server\Exception\RuntimeException('Checking observers', 411);
  126. $fault = Server\Fault::getInstance($e);
  127. $observed = Observer::getObserved();
  128. $this->assertTrue(empty($observed));
  129. $this->assertFalse(Server\Fault::detachObserver('foo'));
  130. }
  131. /**
  132. * getCode() test
  133. */
  134. public function testGetCode()
  135. {
  136. $e = new Server\Exception\RuntimeException('Testing fault', 411);
  137. $fault = Server\Fault::getInstance($e);
  138. $this->assertEquals(411, $fault->getCode());
  139. }
  140. /**
  141. * getException() test
  142. */
  143. public function testGetException()
  144. {
  145. $e = new Server\Exception\RuntimeException('Testing fault', 411);
  146. $fault = Server\Fault::getInstance($e);
  147. $this->assertSame($e, $fault->getException());
  148. }
  149. /**
  150. * getMessage() test
  151. */
  152. public function testGetMessage()
  153. {
  154. $e = new Server\Exception\RuntimeException('Testing fault', 411);
  155. $fault = Server\Fault::getInstance($e);
  156. $this->assertEquals('Testing fault', $fault->getMessage());
  157. }
  158. /**
  159. * __toString() test
  160. */
  161. public function test__toString()
  162. {
  163. $dom = new \DOMDocument('1.0', 'UTF-8');
  164. $r = $dom->appendChild($dom->createElement('methodResponse'));
  165. $f = $r->appendChild($dom->createElement('fault'));
  166. $v = $f->appendChild($dom->createElement('value'));
  167. $s = $v->appendChild($dom->createElement('struct'));
  168. $m1 = $s->appendChild($dom->createElement('member'));
  169. $m1->appendChild($dom->createElement('name', 'faultCode'));
  170. $cv = $m1->appendChild($dom->createElement('value'));
  171. $cv->appendChild($dom->createElement('int', 411));
  172. $m2 = $s->appendChild($dom->createElement('member'));
  173. $m2->appendChild($dom->createElement('name', 'faultString'));
  174. $sv = $m2->appendChild($dom->createElement('value'));
  175. $sv->appendChild($dom->createElement('string', 'Testing fault'));
  176. $xml = $dom->saveXML();
  177. $e = new Server\Exception\RuntimeException('Testing fault', 411);
  178. $fault = Server\Fault::getInstance($e);
  179. $this->assertEquals(trim($xml), trim($fault->__toString()));
  180. }
  181. }
  182. class Exception extends \Exception {}
  183. class Exception2 extends \Exception {}
  184. class Exception3 extends \Exception {}
  185. class Exception4 extends Exception {}
  186. class Observer
  187. {
  188. private static $_instance = false;
  189. public $observed = array();
  190. private function __construct()
  191. {
  192. }
  193. public static function getInstance()
  194. {
  195. if (!static::$_instance) {
  196. static::$_instance = new self();
  197. }
  198. return static::$_instance;
  199. }
  200. public static function observe(Server\Fault $fault)
  201. {
  202. self::getInstance()->observed[] = $fault;
  203. }
  204. public static function clearObserved()
  205. {
  206. self::getInstance()->observed = array();
  207. }
  208. public static function getObserved()
  209. {
  210. return self::getInstance()->observed;
  211. }
  212. }