ResourceTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_Amf
  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. // Call Zend_Amf_AuthTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Amf_ResourceTest::main");
  25. }
  26. require_once 'Zend/Amf/Server.php';
  27. require_once 'Zend/Amf/Request.php';
  28. require_once 'Zend/Amf/Parse/TypeLoader.php';
  29. require_once 'Zend/Amf/Value/Messaging/RemotingMessage.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Amf
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Amf
  37. */
  38. class Zend_Amf_ResourceTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Enter description here...
  42. *
  43. * @var Zend_Amf_Server
  44. */
  45. protected $_server;
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite("Zend_Amf_ResourceTest");
  49. PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. public function setUp()
  52. {
  53. $this->_server = new Zend_Amf_Server();
  54. $this->_server->setProduction(false);
  55. Zend_Amf_Parse_TypeLoader::resetMap();
  56. }
  57. protected function tearDown()
  58. {
  59. unset($this->_server);
  60. }
  61. protected function _callService($method, $class = 'Zend_Amf_Resource_testclass')
  62. {
  63. $request = new Zend_Amf_Request();
  64. $request->setObjectEncoding(0x03);
  65. $this->_server->setClass($class);
  66. $newBody = new Zend_Amf_Value_MessageBody("$class.$method","/1",array("test"));
  67. $request->addAmfBody($newBody);
  68. $this->_server->handle($request);
  69. $response = $this->_server->getResponse();
  70. return $response;
  71. }
  72. public function testFile()
  73. {
  74. $resp = $this->_callService("returnFile");
  75. $this->assertContains("test data", $resp->getResponse());
  76. }
  77. /**
  78. * Defining new unknown resource type
  79. *
  80. * @expectException Zend_Amf_Server_Exception
  81. *
  82. */
  83. public function testCtxNoResource()
  84. {
  85. try {
  86. $this->_callService("returnCtx");
  87. } catch(Zend_Amf_Server_Exception $e) {
  88. $this->assertContains("serialize resource type", $e->getMessage());
  89. return;
  90. }
  91. $this->fail("Failed to throw exception on unknown resource");
  92. }
  93. /**
  94. * Defining new unknown resource type via plugin loader and handling it
  95. *
  96. */
  97. public function testCtxLoader()
  98. {
  99. Zend_Amf_Parse_TypeLoader::addResourceDirectory("Test_Resource", dirname(__FILE__)."/Resources");
  100. $resp = $this->_callService("returnCtx");
  101. $this->assertContains("Accept-language:", $resp->getResponse());
  102. $this->assertContains("foo=bar", $resp->getResponse());
  103. }
  104. /**
  105. * Defining new unknown resource type and handling it
  106. *
  107. */
  108. public function testCtx()
  109. {
  110. Zend_Amf_Parse_TypeLoader::setResourceLoader(new Zend_Amf_TestResourceLoader("2"));
  111. $resp = $this->_callService("returnCtx");
  112. $this->assertContains("Accept-language:", $resp->getResponse());
  113. $this->assertContains("foo=bar", $resp->getResponse());
  114. }
  115. /**
  116. * Defining new unknown resource type, handler has no parse()
  117. *
  118. */
  119. public function testCtxNoParse()
  120. {
  121. Zend_Amf_Parse_TypeLoader::setResourceLoader(new Zend_Amf_TestResourceLoader("3"));
  122. try {
  123. $resp = $this->_callService("returnCtx");
  124. } catch(Zend_Amf_Server_Exception $e) {
  125. $this->assertContains("Could not call parse()", $e->getMessage());
  126. return;
  127. }
  128. $this->fail("Failed to throw exception on unknown resource");
  129. }
  130. }
  131. class Zend_Amf_Resource_testclass {
  132. function returnFile()
  133. {
  134. return fopen(dirname(__FILE__)."/_files/testdata", "r");
  135. }
  136. function returnCtx()
  137. {
  138. $opts = array(
  139. 'http'=>array(
  140. 'method'=>"GET",
  141. 'header'=>"Accept-language: en\r\n" .
  142. "Cookie: foo=bar\r\n"
  143. )
  144. );
  145. $context = stream_context_create($opts);
  146. return $context;
  147. }
  148. }
  149. class StreamContext2
  150. {
  151. public function parse($resource)
  152. {
  153. return stream_context_get_options($resource);
  154. }
  155. }
  156. class StreamContext3
  157. {
  158. protected function parse($resource)
  159. {
  160. return stream_context_get_options($resource);
  161. }
  162. }
  163. class Zend_Amf_TestResourceLoader implements Zend_Loader_PluginLoader_Interface {
  164. public $suffix;
  165. public function __construct($suffix) {
  166. $this->suffix = $suffix;
  167. }
  168. public function addPrefixPath($prefix, $path) {}
  169. public function removePrefixPath($prefix, $path = null) {}
  170. public function isLoaded($name) {}
  171. public function getClassName($name) {}
  172. public function load($name) {
  173. return $name.$this->suffix;
  174. }
  175. }
  176. if (PHPUnit_MAIN_METHOD == "Zend_Amf_ResourceTest::main") {
  177. Zend_Amf_ResourceTest::main();
  178. }