FaultTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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-2009 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. require_once 'PHPUnit/Framework/TestCase.php';
  24. require_once 'PHPUnit/Framework/IncompleteTestError.php';
  25. /**
  26. * Test case for Zend_XmlRpc_Fault
  27. *
  28. * @category Zend
  29. * @package Zend_XmlRpc
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_XmlRpc
  34. */
  35. class Zend_XmlRpc_FaultTest extends PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * Zend_XmlRpc_Fault object
  39. * @var Zend_XmlRpc_Fault
  40. */
  41. protected $_fault;
  42. /**
  43. * Setup environment
  44. */
  45. public function setUp()
  46. {
  47. $this->_fault = new Zend_XmlRpc_Fault();
  48. }
  49. /**
  50. * Teardown environment
  51. */
  52. public function tearDown()
  53. {
  54. unset($this->_fault);
  55. }
  56. /**
  57. * __construct() test
  58. */
  59. public function test__construct()
  60. {
  61. $this->assertTrue($this->_fault instanceof Zend_XmlRpc_Fault);
  62. $this->assertEquals(404, $this->_fault->getCode());
  63. $this->assertEquals('Unknown Error', $this->_fault->getMessage());
  64. }
  65. /**
  66. * get/setCode() test
  67. */
  68. public function testCode()
  69. {
  70. $this->_fault->setCode('1000');
  71. $this->assertEquals(1000, $this->_fault->getCode());
  72. }
  73. /**
  74. * get/setMessage() test
  75. */
  76. public function testMessage()
  77. {
  78. $this->_fault->setMessage('Message');
  79. $this->assertEquals('Message', $this->_fault->getMessage());
  80. }
  81. protected function _createXml()
  82. {
  83. $dom = new DOMDocument('1.0', 'UTF-8');
  84. $response = $dom->appendChild($dom->createElement('methodResponse'));
  85. $fault = $response->appendChild($dom->createElement('fault'));
  86. $value = $fault->appendChild($dom->createElement('value'));
  87. $struct = $value->appendChild($dom->createElement('struct'));
  88. $member1 = $struct->appendChild($dom->createElement('member'));
  89. $member1->appendChild($dom->createElement('name', 'faultCode'));
  90. $value1 = $member1->appendChild($dom->createElement('value'));
  91. $value1->appendChild($dom->createElement('int', 1000));
  92. $member2 = $struct->appendChild($dom->createElement('member'));
  93. $member2->appendChild($dom->createElement('name', 'faultString'));
  94. $value2 = $member2->appendChild($dom->createElement('value'));
  95. $value2->appendChild($dom->createElement('string', 'Error string'));
  96. return $dom->saveXML();
  97. }
  98. protected function _createNonStandardXml()
  99. {
  100. $dom = new DOMDocument('1.0', 'UTF-8');
  101. $response = $dom->appendChild($dom->createElement('methodResponse'));
  102. $fault = $response->appendChild($dom->createElement('fault'));
  103. $value = $fault->appendChild($dom->createElement('value'));
  104. $struct = $value->appendChild($dom->createElement('struct'));
  105. $member1 = $struct->appendChild($dom->createElement('member'));
  106. $member1->appendChild($dom->createElement('name', 'faultCode'));
  107. $value1 = $member1->appendChild($dom->createElement('value'));
  108. $value1->appendChild($dom->createElement('int', 1000));
  109. $member2 = $struct->appendChild($dom->createElement('member'));
  110. $member2->appendChild($dom->createElement('name', 'faultString'));
  111. $value2 = $member2->appendChild($dom->createElement('value', 'Error string'));
  112. return $dom->saveXML();
  113. }
  114. /**
  115. * loadXml() test
  116. */
  117. public function testLoadXml()
  118. {
  119. $xml = $this->_createXml();
  120. try {
  121. $parsed = $this->_fault->loadXml($xml);
  122. } catch (Exception $e) {
  123. $this->fail('Failed to parse XML: ' . $e->getMessage());
  124. }
  125. $this->assertTrue($parsed, $xml);
  126. $this->assertEquals(1000, $this->_fault->getCode());
  127. $this->assertEquals('Error string', $this->_fault->getMessage());
  128. try {
  129. $parsed = $this->_fault->loadXml('foo');
  130. $this->fail('Should not parse invalid XML');
  131. } catch (Exception $e) {
  132. // do nothing
  133. }
  134. }
  135. /**
  136. * Zend_XmlRpc_Fault::isFault() test
  137. */
  138. public function testIsFault()
  139. {
  140. $xml = $this->_createXml();
  141. $this->assertTrue(Zend_XmlRpc_Fault::isFault($xml), $xml);
  142. $this->assertFalse(Zend_XmlRpc_Fault::isFault('foo'));
  143. $this->assertFalse(Zend_XmlRpc_Fault::isFault(array('foo')));
  144. }
  145. /**
  146. * helper for saveXML() and __toString() tests
  147. *
  148. * @param string $xml
  149. * @return void
  150. */
  151. protected function _testXmlFault($xml)
  152. {
  153. try {
  154. $sx = new SimpleXMLElement($xml);
  155. } catch (Exception $e) {
  156. $this->fail('Unable to parse generated XML');
  157. }
  158. $this->assertTrue($sx->fault ? true : false, $xml);
  159. $this->assertTrue($sx->fault->value ? true : false, $xml);
  160. $this->assertTrue($sx->fault->value->struct ? true : false, $xml);
  161. $count = 0;
  162. foreach ($sx->fault->value->struct->member as $member) {
  163. $count++;
  164. $this->assertTrue($member->name ? true : false, $xml);
  165. $this->assertTrue($member->value ? true : false, $xml);
  166. if ('faultCode' == (string) $member->name) {
  167. $this->assertTrue($member->value->int ? true : false, $xml);
  168. $this->assertEquals(1000, (int) $member->value->int, $xml);
  169. }
  170. if ('faultString' == (string) $member->name) {
  171. $this->assertTrue($member->value->string ? true : false, $xml);
  172. $this->assertEquals('Fault message', (string) $member->value->string, $xml);
  173. }
  174. }
  175. $this->assertEquals(2, $count, $xml);
  176. }
  177. /**
  178. * saveXML() test
  179. */
  180. public function testSaveXML()
  181. {
  182. $this->_fault->setCode(1000);
  183. $this->_fault->setMessage('Fault message');
  184. $xml = $this->_fault->saveXML();
  185. $this->_testXmlFault($xml);
  186. }
  187. /**
  188. * __toString() test
  189. */
  190. public function test__toString()
  191. {
  192. $this->_fault->setCode(1000);
  193. $this->_fault->setMessage('Fault message');
  194. $xml = $this->_fault->__toString();
  195. $this->_testXmlFault($xml);
  196. }
  197. /**
  198. * Test encoding settings
  199. */
  200. public function testSetGetEncoding()
  201. {
  202. $this->assertEquals('UTF-8', $this->_fault->getEncoding());
  203. $this->_fault->setEncoding('ISO-8859-1');
  204. $this->assertEquals('ISO-8859-1', $this->_fault->getEncoding());
  205. }
  206. public function testFaultStringWithoutStringTypeDeclaration()
  207. {
  208. $xml = $this->_createNonStandardXml();
  209. try {
  210. $parsed = $this->_fault->loadXml($xml);
  211. } catch (Exception $e) {
  212. $this->fail('Failed to parse XML: ' . $e->getMessage());
  213. }
  214. $this->assertTrue($parsed, $xml);
  215. $this->assertEquals('Error string', $this->_fault->getMessage());
  216. }
  217. }