FaultTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_XmlRpc
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once 'Zend/XmlRpc/Fault.php';
  23. /**
  24. * Test case for Zend_XmlRpc_Fault
  25. *
  26. * @category Zend
  27. * @package Zend_XmlRpc
  28. * @subpackage UnitTests
  29. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @group Zend_XmlRpc
  32. */
  33. class Zend_XmlRpc_FaultTest extends PHPUnit_Framework_TestCase
  34. {
  35. /**
  36. * Zend_XmlRpc_Fault object
  37. * @var Zend_XmlRpc_Fault
  38. */
  39. protected $_fault;
  40. /**
  41. * Setup environment
  42. */
  43. public function setUp()
  44. {
  45. $this->_fault = new Zend_XmlRpc_Fault();
  46. }
  47. /**
  48. * Teardown environment
  49. */
  50. public function tearDown()
  51. {
  52. unset($this->_fault);
  53. }
  54. /**
  55. * __construct() test
  56. */
  57. public function testConstructor()
  58. {
  59. $this->assertTrue($this->_fault instanceof Zend_XmlRpc_Fault);
  60. $this->assertEquals(404, $this->_fault->getCode());
  61. $this->assertEquals('Unknown Error', $this->_fault->getMessage());
  62. }
  63. /**
  64. * get/setCode() test
  65. */
  66. public function testCode()
  67. {
  68. $this->_fault->setCode('1000');
  69. $this->assertEquals(1000, $this->_fault->getCode());
  70. }
  71. /**
  72. * get/setMessage() test
  73. */
  74. public function testMessage()
  75. {
  76. $this->_fault->setMessage('Message');
  77. $this->assertEquals('Message', $this->_fault->getMessage());
  78. }
  79. protected function _createXml()
  80. {
  81. $dom = new DOMDocument('1.0', 'UTF-8');
  82. $response = $dom->appendChild($dom->createElement('methodResponse'));
  83. $fault = $response->appendChild($dom->createElement('fault'));
  84. $value = $fault->appendChild($dom->createElement('value'));
  85. $struct = $value->appendChild($dom->createElement('struct'));
  86. $member1 = $struct->appendChild($dom->createElement('member'));
  87. $member1->appendChild($dom->createElement('name', 'faultCode'));
  88. $value1 = $member1->appendChild($dom->createElement('value'));
  89. $value1->appendChild($dom->createElement('int', 1000));
  90. $member2 = $struct->appendChild($dom->createElement('member'));
  91. $member2->appendChild($dom->createElement('name', 'faultString'));
  92. $value2 = $member2->appendChild($dom->createElement('value'));
  93. $value2->appendChild($dom->createElement('string', 'Error string'));
  94. return $dom->saveXml();
  95. }
  96. protected function _createNonStandardXml()
  97. {
  98. $dom = new DOMDocument('1.0', 'UTF-8');
  99. $response = $dom->appendChild($dom->createElement('methodResponse'));
  100. $fault = $response->appendChild($dom->createElement('fault'));
  101. $value = $fault->appendChild($dom->createElement('value'));
  102. $struct = $value->appendChild($dom->createElement('struct'));
  103. $member1 = $struct->appendChild($dom->createElement('member'));
  104. $member1->appendChild($dom->createElement('name', 'faultCode'));
  105. $value1 = $member1->appendChild($dom->createElement('value'));
  106. $value1->appendChild($dom->createElement('int', 1000));
  107. $member2 = $struct->appendChild($dom->createElement('member'));
  108. $member2->appendChild($dom->createElement('name', 'faultString'));
  109. $value2 = $member2->appendChild($dom->createElement('value', 'Error string'));
  110. return $dom->saveXml();
  111. }
  112. /**
  113. * loadXml() test
  114. */
  115. public function testLoadXml()
  116. {
  117. $xml = $this->_createXml();
  118. try {
  119. $parsed = $this->_fault->loadXml($xml);
  120. } catch (Exception $e) {
  121. $this->fail('Failed to parse XML: ' . $e->getMessage());
  122. }
  123. $this->assertTrue($parsed, $xml);
  124. $this->assertEquals(1000, $this->_fault->getCode());
  125. $this->assertEquals('Error string', $this->_fault->getMessage());
  126. try {
  127. $parsed = $this->_fault->loadXml('foo');
  128. $this->fail('Should not parse invalid XML');
  129. } catch (Zend_XmlRpc_Exception $e) {
  130. // do nothing
  131. }
  132. $this->assertFalse($this->_fault->loadXml('<wellformedButInvalid/>'));
  133. try {
  134. $this->assertFalse($this->_fault->loadXml('<methodResponse><fault/></methodResponse>'));
  135. $this->fail('Should throw an exception. No value element in fault');
  136. } catch (Zend_XmlRpc_Exception $e) {
  137. $this->assertEquals('Invalid fault structure', $e->getMessage());
  138. $this->assertSame(500, $e->getCode());
  139. }
  140. try {
  141. $this->_fault->loadXml('<methodResponse><fault/></methodResponse>');
  142. $this->fail('Should throw an exception. No struct element in //fault/value');
  143. } catch (Zend_XmlRpc_Exception $e) {
  144. $this->assertEquals('Invalid fault structure', $e->getMessage());
  145. $this->assertSame(500, $e->getCode());
  146. }
  147. try {
  148. $this->_fault->loadXml('<methodResponse><fault><value><struct/></value></fault></methodResponse>');
  149. $this->fail('Should throw an exception. Empty fault code and string in //fault/value');
  150. } catch (Zend_XmlRpc_Exception $e) {
  151. $this->assertEquals('Fault code and string required', $e->getMessage());
  152. }
  153. $this->_fault->loadXml('<methodResponse><fault><value><struct>'
  154. . '<member><name>faultString</name><value><string>str</string></value></member>'
  155. . '</struct></value></fault></methodResponse>');
  156. $this->assertSame(404, $this->_fault->getCode(), 'If no fault code is given, use 404 as a default');
  157. $this->_fault->loadXml('<methodResponse><fault><value><struct>'
  158. . '<member><name>faultCode</name><value><int>610</int></value></member>'
  159. . '</struct></value></fault></methodResponse>');
  160. $this->assertSame(
  161. 'Invalid method class', $this->_fault->getMessage(), 'If empty fault string is given, resolve the code');
  162. $this->_fault->loadXml('<methodResponse><fault><value><struct>'
  163. . '<member><name>faultCode</name><value><int>1234</int></value></member>'
  164. . '</struct></value></fault></methodResponse>');
  165. $this->assertSame(
  166. 'Unknown Error',
  167. $this->_fault->getMessage(),
  168. 'If code resolval failed, use "Unknown Error"'
  169. );
  170. }
  171. /**
  172. * Zend_XmlRpc_Fault::isFault() test
  173. */
  174. public function testIsFault()
  175. {
  176. $xml = $this->_createXml();
  177. $this->assertTrue(Zend_XmlRpc_Fault::isFault($xml), $xml);
  178. $this->assertFalse(Zend_XmlRpc_Fault::isFault('foo'));
  179. $this->assertFalse(Zend_XmlRpc_Fault::isFault(array('foo')));
  180. }
  181. /**
  182. * helper for saveXml() and __toString() tests
  183. *
  184. * @param string $xml
  185. * @return void
  186. */
  187. protected function _testXmlFault($xml)
  188. {
  189. try {
  190. $sx = new SimpleXMLElement($xml);
  191. } catch (Exception $e) {
  192. $this->fail('Unable to parse generated XML');
  193. }
  194. $this->assertTrue($sx->fault ? true : false, $xml);
  195. $this->assertTrue($sx->fault->value ? true : false, $xml);
  196. $this->assertTrue($sx->fault->value->struct ? true : false, $xml);
  197. $count = 0;
  198. foreach ($sx->fault->value->struct->member as $member) {
  199. $count++;
  200. $this->assertTrue($member->name ? true : false, $xml);
  201. $this->assertTrue($member->value ? true : false, $xml);
  202. if ('faultCode' == (string) $member->name) {
  203. $this->assertTrue($member->value->int ? true : false, $xml);
  204. $this->assertEquals(1000, (int) $member->value->int, $xml);
  205. }
  206. if ('faultString' == (string) $member->name) {
  207. $this->assertTrue($member->value->string ? true : false, $xml);
  208. $this->assertEquals('Fault message', (string) $member->value->string, $xml);
  209. }
  210. }
  211. $this->assertEquals(2, $count, $xml);
  212. }
  213. /**
  214. * saveXml() test
  215. */
  216. public function testSaveXML()
  217. {
  218. $this->_fault->setCode(1000);
  219. $this->_fault->setMessage('Fault message');
  220. $xml = $this->_fault->saveXml();
  221. $this->_testXmlFault($xml);
  222. }
  223. /**
  224. * __toString() test
  225. */
  226. public function test__toString()
  227. {
  228. $this->_fault->setCode(1000);
  229. $this->_fault->setMessage('Fault message');
  230. $xml = $this->_fault->__toString();
  231. $this->_testXmlFault($xml);
  232. }
  233. /**
  234. * Test encoding settings
  235. */
  236. public function testSetGetEncoding()
  237. {
  238. $this->assertEquals('UTF-8', $this->_fault->getEncoding());
  239. $this->assertEquals('UTF-8', Zend_XmlRpc_Value::getGenerator()->getEncoding());
  240. $this->_fault->setEncoding('ISO-8859-1');
  241. $this->assertEquals('ISO-8859-1', $this->_fault->getEncoding());
  242. $this->assertEquals('ISO-8859-1', Zend_XmlRpc_Value::getGenerator()->getEncoding());
  243. }
  244. public function testUnknownErrorIsUsedIfUnknownErrorCodeEndEmptyMessageIsPassed()
  245. {
  246. $fault = new Zend_XmlRpc_Fault(1234);
  247. $this->assertSame(1234, $fault->getCode());
  248. $this->assertSame('Unknown error', $fault->getMessage());
  249. }
  250. public function testFaultStringWithoutStringTypeDeclaration()
  251. {
  252. $xml = $this->_createNonStandardXml();
  253. try {
  254. $parsed = $this->_fault->loadXml($xml);
  255. } catch (Exception $e) {
  256. $this->fail('Failed to parse XML: ' . $e->getMessage());
  257. }
  258. $this->assertTrue($parsed, $xml);
  259. $this->assertEquals('Error string', $this->_fault->getMessage());
  260. }
  261. }