ResourceTest.php 5.4 KB

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