ResourceTest.php 5.7 KB

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