ResourceTest.php 5.6 KB

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