ResourceTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. // Call Zend_Amf_AuthTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Amf_ResourceTest::main");
  5. }
  6. require_once 'PHPUnit/Framework/TestCase.php';
  7. require_once dirname(__FILE__) . '/../../TestHelper.php';
  8. require_once 'Zend/Amf/Server.php';
  9. require_once 'Zend/Amf/Request.php';
  10. require_once 'Zend/Amf/Parse/TypeLoader.php';
  11. require_once 'Zend/Amf/Value/Messaging/RemotingMessage.php';
  12. /**
  13. * test case.
  14. */
  15. class Zend_Amf_ResourceTest extends PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * Enter description here...
  19. *
  20. * @var Zend_Amf_Server
  21. */
  22. protected $_server;
  23. public static function main()
  24. {
  25. $suite = new PHPUnit_Framework_TestSuite("Zend_Amf_ResourceTest");
  26. PHPUnit_TextUI_TestRunner::run($suite);
  27. }
  28. public function setUp()
  29. {
  30. $this->_server = new Zend_Amf_Server();
  31. $this->_server->setProduction(false);
  32. Zend_Amf_Parse_TypeLoader::resetMap();
  33. }
  34. protected function tearDown()
  35. {
  36. unset($this->_server);
  37. }
  38. protected function _callService($method, $class = 'Zend_Amf_Resource_testclass')
  39. {
  40. $request = new Zend_Amf_Request();
  41. $request->setObjectEncoding(0x03);
  42. $this->_server->setClass($class);
  43. $newBody = new Zend_Amf_Value_MessageBody("$class.$method","/1",array("test"));
  44. $request->addAmfBody($newBody);
  45. $this->_server->handle($request);
  46. $response = $this->_server->getResponse();
  47. return $response;
  48. }
  49. public function testFile()
  50. {
  51. $resp = $this->_callService("returnFile");
  52. $this->assertContains("test data", $resp->getResponse());
  53. }
  54. /**
  55. * Defining new unknown resource type
  56. *
  57. * @expectException Zend_Amf_Server_Exception
  58. *
  59. */
  60. public function testCtxNoResource()
  61. {
  62. try {
  63. $this->_callService("returnCtx");
  64. } catch(Zend_Amf_Server_Exception $e) {
  65. $this->assertContains("serialize resource type", $e->getMessage());
  66. return;
  67. }
  68. $this->fail("Failed to throw exception on unknown resource");
  69. }
  70. /**
  71. * Defining new unknown resource type via plugin loader and handling it
  72. *
  73. */
  74. public function testCtxLoader()
  75. {
  76. Zend_Amf_Parse_TypeLoader::addResourceDirectory("Test_Resource", dirname(__FILE__)."/Resources");
  77. $resp = $this->_callService("returnCtx");
  78. $this->assertContains("Accept-language:", $resp->getResponse());
  79. $this->assertContains("foo=bar", $resp->getResponse());
  80. }
  81. /**
  82. * Defining new unknown resource type and handling it
  83. *
  84. */
  85. public function testCtx()
  86. {
  87. Zend_Amf_Parse_TypeLoader::setResourceLoader(new Zend_Amf_TestResourceLoader("2"));
  88. $resp = $this->_callService("returnCtx");
  89. $this->assertContains("Accept-language:", $resp->getResponse());
  90. $this->assertContains("foo=bar", $resp->getResponse());
  91. }
  92. /**
  93. * Defining new unknown resource type, handler has no parse()
  94. *
  95. */
  96. public function testCtxNoParse()
  97. {
  98. Zend_Amf_Parse_TypeLoader::setResourceLoader(new Zend_Amf_TestResourceLoader("3"));
  99. try {
  100. $resp = $this->_callService("returnCtx");
  101. } catch(Zend_Amf_Server_Exception $e) {
  102. $this->assertContains("Could not call parse()", $e->getMessage());
  103. return;
  104. }
  105. $this->fail("Failed to throw exception on unknown resource");
  106. }
  107. }
  108. class Zend_Amf_Resource_testclass {
  109. function returnFile()
  110. {
  111. return fopen(dirname(__FILE__)."/_files/testdata", "r");
  112. }
  113. function returnCtx()
  114. {
  115. $opts = array(
  116. 'http'=>array(
  117. 'method'=>"GET",
  118. 'header'=>"Accept-language: en\r\n" .
  119. "Cookie: foo=bar\r\n"
  120. )
  121. );
  122. $context = stream_context_create($opts);
  123. return $context;
  124. }
  125. }
  126. class StreamContext2
  127. {
  128. public function parse($resource) {
  129. return stream_context_get_options($resource);
  130. }
  131. }
  132. class StreamContext3
  133. {
  134. protected function parse($resource) {
  135. return stream_context_get_options($resource);
  136. }
  137. }
  138. class Zend_Amf_TestResourceLoader implements Zend_Loader_PluginLoader_Interface {
  139. public $suffix;
  140. public function __construct($suffix) {
  141. $this->suffix = $suffix;
  142. }
  143. public function addPrefixPath($prefix, $path) {}
  144. public function removePrefixPath($prefix, $path = null) {}
  145. public function isLoaded($name) {}
  146. public function getClassName($name) {}
  147. public function load($name) {
  148. return $name.$this->suffix;
  149. }
  150. }
  151. if (PHPUnit_MAIN_METHOD == "Zend_Amf_ResourceTest::main") {
  152. Zend_Amf_ResourceTest::main();
  153. }