~clinton-collins/familyproject/trunk

« back to all changes in this revision

Viewing changes to ZendFramework/tests/Zend/Amf/ResourceTest.php

  • Committer: Clinton Collins
  • Date: 2009-06-26 19:54:58 UTC
  • Revision ID: clinton.collins@gmail.com-20090626195458-5ebba0qcvo15xlpy
Initial Import

Show diffs side-by-side

added added

removed removed

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