~clinton-collins/familyproject/trunk

« back to all changes in this revision

Viewing changes to ZendFramework/tests/Zend/Rest/ClientTest.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
/**
 
3
 * @package Zend_Rest
 
4
 * @subpackage UnitTests
 
5
 */
 
6
 
 
7
require_once dirname(__FILE__)."/../../TestHelper.php";
 
8
 
 
9
/** Zend_Rest_Client */
 
10
require_once 'Zend/Rest/Client.php';
 
11
 
 
12
/** Zend_Http_Client_Adapter_Test */
 
13
require_once 'Zend/Http/Client/Adapter/Test.php';
 
14
 
 
15
/** PHPUnit Test Case */
 
16
require_once 'PHPUnit/Framework/TestCase.php';
 
17
 
 
18
/**
 
19
 * Test cases for Zend_Rest_Client
 
20
 *
 
21
 * @package Zend_Rest
 
22
 * @subpackage UnitTests
 
23
 */
 
24
class Zend_Rest_ClientTest extends PHPUnit_Framework_TestCase 
 
25
{
 
26
    public function setUp()
 
27
    {
 
28
        $this->path = dirname(__FILE__) . '/responses/';
 
29
 
 
30
        $this->adapter = new Zend_Http_Client_Adapter_Test();
 
31
        $client        = new Zend_Http_Client(null, array(
 
32
            'adapter' => $this->adapter
 
33
        ));
 
34
        Zend_Rest_Client::setHttpClient($client);
 
35
 
 
36
        $this->rest = new Zend_Rest_Client('http://framework.zend.com/');
 
37
    }
 
38
 
 
39
    public function testUri()
 
40
    {
 
41
        $client = new Zend_Rest_Client('http://framework.zend.com/rest/');
 
42
        $uri = $client->getUri();
 
43
        $this->assertTrue($uri instanceof Zend_Uri_Http);
 
44
        $this->assertEquals('http://framework.zend.com/rest/', $uri->getUri());
 
45
 
 
46
        $client->setUri(Zend_Uri::factory('http://framework.zend.com/soap/'));
 
47
        $uri = $client->getUri();
 
48
        $this->assertTrue($uri instanceof Zend_Uri_Http);
 
49
        $this->assertEquals('http://framework.zend.com/soap/', $uri->getUri());
 
50
 
 
51
        $client->setUri('http://framework.zend.com/xmlrpc/');
 
52
        $uri = $client->getUri();
 
53
        $this->assertTrue($uri instanceof Zend_Uri_Http);
 
54
        $this->assertEquals('http://framework.zend.com/xmlrpc/', $uri->getUri());
 
55
    }
 
56
 
 
57
    public function testRestGetThrowsExceptionWithNoUri()
 
58
    {
 
59
        $expXml   = file_get_contents($this->path . 'returnString.xml');
 
60
        $response = "HTTP/1.0 200 OK\r\n"
 
61
                  . "X-powered-by: PHP/5.2.0\r\n"
 
62
                  . "Content-type: text/xml\r\n"
 
63
                  . "Content-length: " . strlen($expXml) . "\r\n"
 
64
                  . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
 
65
                  . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
 
66
                  . "Connection: close\r\n"
 
67
                  . "\r\n"
 
68
                  . $expXml;
 
69
        $this->adapter->setResponse($response);
 
70
 
 
71
        $rest = new Zend_Rest_Client();
 
72
 
 
73
        try {
 
74
            $response = $rest->restGet('/rest/');
 
75
            $this->fail('Should throw exception if no URI in object');
 
76
        } catch (Exception $e) {
 
77
            // success
 
78
        }
 
79
    }
 
80
 
 
81
    public function testRestFixesPathWithMissingSlashes()
 
82
    {
 
83
        $expXml   = file_get_contents($this->path . 'returnString.xml');
 
84
        $response = "HTTP/1.0 200 OK\r\n"
 
85
                  . "X-powered-by: PHP/5.2.0\r\n"
 
86
                  . "Content-type: text/xml\r\n"
 
87
                  . "Content-length: " . strlen($expXml) . "\r\n"
 
88
                  . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
 
89
                  . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
 
90
                  . "Connection: close\r\n"
 
91
                  . "\r\n"
 
92
                  . $expXml;
 
93
        $this->adapter->setResponse($response);
 
94
 
 
95
        $rest = new Zend_Rest_Client('http://framework.zend.com');
 
96
 
 
97
        $response = $rest->restGet('rest');
 
98
        $this->assertTrue($response instanceof Zend_Http_Response);
 
99
        $this->assertContains($expXml, $response->getBody());
 
100
    }
 
101
 
 
102
    public function testRestGet()
 
103
    {
 
104
        $expXml   = file_get_contents($this->path . 'returnString.xml');
 
105
        $response = "HTTP/1.0 200 OK\r\n"
 
106
                  . "X-powered-by: PHP/5.2.0\r\n"
 
107
                  . "Content-type: text/xml\r\n"
 
108
                  . "Content-length: " . strlen($expXml) . "\r\n"
 
109
                  . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
 
110
                  . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
 
111
                  . "Connection: close\r\n"
 
112
                  . "\r\n"
 
113
                  . $expXml;
 
114
        $this->adapter->setResponse($response);
 
115
 
 
116
        $response = $this->rest->restGet('/rest/');
 
117
        $this->assertTrue($response instanceof Zend_Http_Response);
 
118
        $this->assertContains($expXml, $response->getBody());
 
119
    }
 
120
 
 
121
    public function testRestPost()
 
122
    {
 
123
        $expXml   = file_get_contents($this->path . 'returnString.xml');
 
124
        $response = "HTTP/1.0 200 OK\r\n"
 
125
                  . "X-powered-by: PHP/5.2.0\r\n"
 
126
                  . "Content-type: text/xml\r\n"
 
127
                  . "Content-length: " . strlen($expXml) . "\r\n"
 
128
                  . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
 
129
                  . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
 
130
                  . "Connection: close\r\n"
 
131
                  . "\r\n"
 
132
                  . $expXml;
 
133
        $this->adapter->setResponse($response);
 
134
 
 
135
        $reqXml   = file_get_contents($this->path . 'returnInt.xml');
 
136
        $response = $this->rest->restPost('/rest/', $reqXml);
 
137
        $this->assertTrue($response instanceof Zend_Http_Response);
 
138
        $body = $response->getBody();
 
139
        $this->assertContains($expXml, $response->getBody());
 
140
 
 
141
        $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
 
142
        $this->assertContains($reqXml, $request, $request);
 
143
    }
 
144
 
 
145
    public function testRestPostWithArrayData()
 
146
    {
 
147
        $expXml   = file_get_contents($this->path . 'returnString.xml');
 
148
        $response = "HTTP/1.0 200 OK\r\n"
 
149
                  . "X-powered-by: PHP/5.2.0\r\n"
 
150
                  . "Content-type: text/xml\r\n"
 
151
                  . "Content-length: " . strlen($expXml) . "\r\n"
 
152
                  . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
 
153
                  . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
 
154
                  . "Connection: close\r\n"
 
155
                  . "\r\n"
 
156
                  . $expXml;
 
157
        $this->adapter->setResponse($response);
 
158
 
 
159
        $response = $this->rest->restPost('/rest/', array('foo' => 'bar', 'baz' => 'bat'));
 
160
        $this->assertTrue($response instanceof Zend_Http_Response);
 
161
        $body = $response->getBody();
 
162
        $this->assertContains($expXml, $response->getBody());
 
163
 
 
164
        $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
 
165
        $this->assertContains('foo=bar&baz=bat', $request, $request);
 
166
    }
 
167
 
 
168
    public function testRestPut()
 
169
    {
 
170
        $expXml   = file_get_contents($this->path . 'returnString.xml');
 
171
        $response = "HTTP/1.0 200 OK\r\n"
 
172
                  . "X-powered-by: PHP/5.2.0\r\n"
 
173
                  . "Content-type: text/xml\r\n"
 
174
                  . "Content-length: " . strlen($expXml) . "\r\n"
 
175
                  . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
 
176
                  . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
 
177
                  . "Connection: close\r\n"
 
178
                  . "\r\n"
 
179
                  . $expXml;
 
180
        $this->adapter->setResponse($response);
 
181
 
 
182
        $reqXml   = file_get_contents($this->path . 'returnInt.xml');
 
183
        $response = $this->rest->restPut('/rest/', $reqXml);
 
184
        $this->assertTrue($response instanceof Zend_Http_Response);
 
185
        $body = $response->getBody();
 
186
        $this->assertContains($expXml, $response->getBody());
 
187
 
 
188
        $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
 
189
        $this->assertContains($reqXml, $request, $request);
 
190
    }
 
191
 
 
192
    public function testRestDelete()
 
193
    {
 
194
        $expXml   = file_get_contents($this->path . 'returnString.xml');
 
195
        $response = "HTTP/1.0 200 OK\r\n"
 
196
                  . "X-powered-by: PHP/5.2.0\r\n"
 
197
                  . "Content-type: text/xml\r\n"
 
198
                  . "Content-length: " . strlen($expXml) . "\r\n"
 
199
                  . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
 
200
                  . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
 
201
                  . "Connection: close\r\n"
 
202
                  . "\r\n"
 
203
                  . $expXml;
 
204
        $this->adapter->setResponse($response);
 
205
 
 
206
        $reqXml   = file_get_contents($this->path . 'returnInt.xml');
 
207
        $response = $this->rest->restDelete('/rest/');
 
208
        $this->assertTrue($response instanceof Zend_Http_Response);
 
209
        $body = $response->getBody();
 
210
        $this->assertContains($expXml, $response->getBody());
 
211
    }
 
212
 
 
213
    public function testCallWithHttpMethod()
 
214
    {
 
215
        $expXml   = file_get_contents($this->path . 'returnString.xml');
 
216
        $response = "HTTP/1.0 200 OK\r\n"
 
217
                  . "X-powered-by: PHP/5.2.0\r\n"
 
218
                  . "Content-type: text/xml\r\n"
 
219
                  . "Content-length: " . strlen($expXml) . "\r\n"
 
220
                  . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
 
221
                  . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
 
222
                  . "Connection: close\r\n"
 
223
                  . "\r\n"
 
224
                  . $expXml;
 
225
        $this->adapter->setResponse($response);
 
226
 
 
227
        $response = $this->rest->get('/rest/');
 
228
        $this->assertTrue($response instanceof Zend_Rest_Client_Result);
 
229
        $this->assertTrue($response->isSuccess());
 
230
        $this->assertEquals('string', $response->response());
 
231
    }
 
232
 
 
233
    public function testCallAsObjectMethodReturnsClient()
 
234
    {
 
235
        $expXml   = file_get_contents($this->path . 'returnString.xml');
 
236
        $response = "HTTP/1.0 200 OK\r\n"
 
237
                  . "X-powered-by: PHP/5.2.0\r\n"
 
238
                  . "Content-type: text/xml\r\n"
 
239
                  . "Content-length: " . strlen($expXml) . "\r\n"
 
240
                  . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
 
241
                  . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
 
242
                  . "Connection: close\r\n"
 
243
                  . "\r\n"
 
244
                  . $expXml;
 
245
        $this->adapter->setResponse($response);
 
246
 
 
247
        $response = $this->rest->doStuff('why', 'not');
 
248
        $this->assertTrue($response instanceof Zend_Rest_Client);
 
249
        $this->assertSame($this->rest, $response);
 
250
    }
 
251
 
 
252
    public function testCallAsObjectMethodChainPerformsRequest()
 
253
    {
 
254
        $expXml   = file_get_contents($this->path . 'returnString.xml');
 
255
        $response = "HTTP/1.0 200 OK\r\n"
 
256
                  . "X-powered-by: PHP/5.2.0\r\n"
 
257
                  . "Content-type: text/xml\r\n"
 
258
                  . "Content-length: " . strlen($expXml) . "\r\n"
 
259
                  . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
 
260
                  . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
 
261
                  . "Connection: close\r\n"
 
262
                  . "\r\n"
 
263
                  . $expXml;
 
264
        $this->adapter->setResponse($response);
 
265
 
 
266
        $response = $this->rest->doStuff('why', 'not')->get();
 
267
        $this->assertTrue($response instanceof Zend_Rest_Client_Result);
 
268
        $this->assertEquals('string', $response->response());
 
269
    }
 
270
 
 
271
    /**
 
272
     * @group ZF-3705
 
273
     * @group ZF-3647
 
274
     */
 
275
    public function testInvalidXmlInClientResultLeadsToException()
 
276
    {
 
277
        try {
 
278
            $result = new Zend_Rest_Client_Result("invalidxml");
 
279
            $this->fail();
 
280
        } catch(Zend_Rest_Client_Result_Exception $e) {
 
281
            
 
282
        }
 
283
    }
 
284
}