2
0

ResultTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_Rest
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 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. /**
  23. * Zend_Rest_Server
  24. */
  25. require_once 'Zend/Rest/Client/Result.php';
  26. /**
  27. * PHPUnit Test Case
  28. */
  29. require_once 'PHPUnit/Framework/TestCase.php';
  30. /**
  31. * Test cases for Zend_Rest_Server
  32. *
  33. * @category Zend
  34. * @package Zend_Rest
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Rest
  39. * @group Zend_Rest_Result
  40. */
  41. class Zend_Rest_ResultTest extends PHPUnit_Framework_TestCase
  42. {
  43. static $path;
  44. public function __construct()
  45. {
  46. self::$path = dirname(__FILE__).'/responses/';
  47. }
  48. public function testResponseSuccess()
  49. {
  50. $xml = file_get_contents(self::$path ."returnString.xml");
  51. $client = new Zend_Rest_Client_Result($xml);
  52. $this->assertTrue($client->isSuccess());
  53. }
  54. public function testResponseIsError()
  55. {
  56. $xml = file_get_contents(self::$path ."returnError.xml");
  57. $client = new Zend_Rest_Client_Result($xml);
  58. $this->assertTrue($client->isError());
  59. }
  60. public function testResponseString()
  61. {
  62. $xml = file_get_contents(self::$path ."returnString.xml");
  63. $client = new Zend_Rest_Client_Result($xml);
  64. $this->assertEquals("string", $client->__toString());
  65. }
  66. public function testResponseInt()
  67. {
  68. $xml = file_get_contents(self::$path ."returnInt.xml");
  69. $client = new Zend_Rest_Client_Result($xml);
  70. $this->assertEquals("123", $client->__toString());
  71. }
  72. public function testResponseArray()
  73. {
  74. $xml = file_get_contents(self::$path ."returnArray.xml");
  75. // <foo>bar</foo><baz>1</baz><key_1>0</key_1><bat>123</bat>
  76. $client = new Zend_Rest_Client_Result($xml);
  77. foreach ($client as $key => $value) {
  78. $result_array[$key] = (string) $value;
  79. }
  80. $this->assertEquals(array("foo" => "bar", "baz" => "1", "key_1" => "0", "bat" => "123", "status" => "success"), $result_array);
  81. }
  82. public function testResponseObject()
  83. {
  84. $xml = file_get_contents(self::$path ."returnObject.xml");
  85. // <foo>bar</foo><baz>1</baz><bat>123</bat><qux>0</qux><status>success</status>
  86. $client = new Zend_Rest_Client_Result($xml);
  87. $this->assertEquals("bar", $client->foo());
  88. $this->assertEquals(1, $client->baz());
  89. $this->assertEquals(123, $client->bat());
  90. $this->assertEquals(0, $client->qux());
  91. $this->assertEquals("success", $client->status());
  92. }
  93. public function testResponseTrue()
  94. {
  95. $xml = file_get_contents(self::$path ."returnTrue.xml");
  96. $client = new Zend_Rest_Client_Result($xml);
  97. $this->assertTrue((bool)$client->response);
  98. }
  99. public function testResponseFalse()
  100. {
  101. $xml = file_get_contents(self::$path ."returnFalse.xml");
  102. $client = new Zend_Rest_Client_Result($xml);
  103. $this->assertFalse((bool) $client->response());
  104. }
  105. public function testResponseVoid()
  106. {
  107. $xml = file_get_contents(self::$path . "returnVoid.xml");
  108. $client = new Zend_Rest_Client_Result($xml);
  109. $this->assertEquals(null, $client->response());
  110. }
  111. public function testResponseException()
  112. {
  113. $xml = file_get_contents(self::$path . "returnError.xml");
  114. $client = new Zend_Rest_Client_Result($xml);
  115. $this->assertTrue($client->isError());
  116. }
  117. public function testGetXpathValue()
  118. {
  119. $xml = file_get_contents(self::$path . DIRECTORY_SEPARATOR . 'returnNestedArray.xml');
  120. $result = new Zend_Rest_Client_Result($xml);
  121. $key_1 = $result->key_1();
  122. $this->assertEquals(0, $key_1);
  123. }
  124. public function testGetXpathValueMissing()
  125. {
  126. $xml = file_get_contents(self::$path . DIRECTORY_SEPARATOR . 'returnNestedArray.xml');
  127. $result = new Zend_Rest_Client_Result($xml);
  128. $lola = $result->lola;
  129. $this->assertNull($lola);
  130. }
  131. public function testGetXpathValueArray()
  132. {
  133. $xml = file_get_contents(self::$path . DIRECTORY_SEPARATOR . 'returnNestedArray.xml');
  134. $result = new Zend_Rest_Client_Result($xml);
  135. $baz = $result->baz;
  136. $this->assertTrue(is_array($baz), var_export($baz, 1));
  137. $this->assertEquals('1', (string) $baz[0]);
  138. $this->assertEquals('farama', (string) $baz[1]);
  139. }
  140. public function testIsset()
  141. {
  142. $xml = file_get_contents(self::$path . DIRECTORY_SEPARATOR . 'returnNestedArray.xml');
  143. $result = new Zend_Rest_Client_Result($xml);
  144. $this->assertTrue(isset($result->bar));
  145. }
  146. public function testIssetXpathValue()
  147. {
  148. $xml = file_get_contents(self::$path . DIRECTORY_SEPARATOR . 'returnNestedArray.xml');
  149. $result = new Zend_Rest_Client_Result($xml);
  150. $this->assertTrue(isset($result->baz));
  151. }
  152. public function testIssetInvalidValue()
  153. {
  154. $xml = file_get_contents(self::$path . DIRECTORY_SEPARATOR . 'returnNestedArray.xml');
  155. $result = new Zend_Rest_Client_Result($xml);
  156. $this->assertFalse(isset($result->lola));
  157. }
  158. public function testCall()
  159. {
  160. $xml = file_get_contents(self::$path . DIRECTORY_SEPARATOR . 'returnNestedArray.xml');
  161. $result = new Zend_Rest_Client_Result($xml);
  162. $returned = $result->key_1();
  163. $this->assertEquals(0, $returned);
  164. }
  165. }