ResultTest.php 6.0 KB

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