1 | <?php |
---|
2 | /** |
---|
3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions are met: |
---|
7 | * |
---|
8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
9 | * this list of conditions and the following disclaimer. |
---|
10 | * |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | * this listof conditions and the following disclaimer in the documentation |
---|
13 | * and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | * |
---|
27 | * The views and conclusions contained in the software and documentation are |
---|
28 | * those of the authors and should not be interpreted as representing official |
---|
29 | * policies, either expressed or implied, of the copyright holder. |
---|
30 | */ |
---|
31 | |
---|
32 | class Solarium_Client_Adapter_ZendHttpTest extends PHPUnit_Framework_TestCase |
---|
33 | { |
---|
34 | /** |
---|
35 | * @var Solarium_Client_Adapter_ZendHttp |
---|
36 | */ |
---|
37 | protected $_adapter; |
---|
38 | |
---|
39 | public function setUp() |
---|
40 | { |
---|
41 | if (!class_exists('Zend_Loader_Autoloader') && (@include_once 'Zend/Loader/Autoloader.php') !== 'OK') { |
---|
42 | $this->markTestSkipped('ZF not in include_path, skipping ZendHttp adapter tests'); |
---|
43 | } |
---|
44 | |
---|
45 | Zend_Loader_Autoloader::getInstance(); |
---|
46 | |
---|
47 | $this->_adapter = new Solarium_Client_Adapter_ZendHttp(); |
---|
48 | } |
---|
49 | |
---|
50 | public function testForwardingToZendHttpInSetOptions() |
---|
51 | { |
---|
52 | $options = array('timeout' => 10, 'optionZ' => 123, 'options' => array('optionX' => 'Y')); |
---|
53 | $adapterOptions = array('timeout' => 10, 'optionX' => 'Y'); |
---|
54 | |
---|
55 | $mock = $this->getMock('Zend_Http_Client'); |
---|
56 | $mock->expects($this->once()) |
---|
57 | ->method('setConfig') |
---|
58 | ->with($this->equalTo($adapterOptions)); |
---|
59 | |
---|
60 | $this->_adapter->setZendHttp($mock); |
---|
61 | $this->_adapter->setOptions($options); |
---|
62 | } |
---|
63 | |
---|
64 | public function testSetAndGetZendHttp() |
---|
65 | { |
---|
66 | $dummy = new StdClass(); |
---|
67 | |
---|
68 | $this->_adapter->setZendHttp($dummy); |
---|
69 | |
---|
70 | $this->assertEquals( |
---|
71 | $dummy, |
---|
72 | $this->_adapter->getZendHttp() |
---|
73 | ); |
---|
74 | } |
---|
75 | |
---|
76 | public function testGetZendHttpAutoload() |
---|
77 | { |
---|
78 | $options = array('timeout' => 10, 'optionZ' => 123, 'options' => array('adapter' => 'Zend_Http_Client_Adapter_Curl')); |
---|
79 | $this->_adapter->setOptions($options); |
---|
80 | |
---|
81 | $zendHttp = $this->_adapter->getZendHttp(); |
---|
82 | $this->assertThat($zendHttp, $this->isInstanceOf('Zend_Http_Client')); |
---|
83 | } |
---|
84 | |
---|
85 | public function testExecute() |
---|
86 | { |
---|
87 | $method = Solarium_Client_Request::METHOD_GET; |
---|
88 | $rawData = 'xyz'; |
---|
89 | $responseData = 'abc'; |
---|
90 | $handler = 'myhandler'; |
---|
91 | $headers = array( |
---|
92 | 'Content-Type: application/x-www-form-urlencoded' |
---|
93 | ); |
---|
94 | |
---|
95 | $request = new Solarium_Client_Request(); |
---|
96 | $request->setMethod($method); |
---|
97 | $request->setHandler($handler); |
---|
98 | $request->setHeaders($headers); |
---|
99 | $request->setRawData($rawData); |
---|
100 | |
---|
101 | $response = new Zend_Http_Response(200, array('status' => 'HTTP 1.1 200 OK'), $responseData); |
---|
102 | |
---|
103 | $mock = $this->getMock('Zend_Http_Client'); |
---|
104 | $mock->expects($this->once()) |
---|
105 | ->method('setMethod') |
---|
106 | ->with($this->equalTo($method)); |
---|
107 | $mock->expects($this->once()) |
---|
108 | ->method('setUri') |
---|
109 | ->with($this->equalTo('http://127.0.0.1:8983/solr/myhandler?')); |
---|
110 | $mock->expects($this->once()) |
---|
111 | ->method('setHeaders') |
---|
112 | ->with($this->equalTo($headers)); |
---|
113 | $mock->expects($this->once()) |
---|
114 | ->method('setRawData') |
---|
115 | ->with($this->equalTo($rawData)); |
---|
116 | $mock->expects($this->once()) |
---|
117 | ->method('request') |
---|
118 | ->will($this->returnValue($response)); |
---|
119 | |
---|
120 | $this->_adapter->setZendHttp($mock); |
---|
121 | $adapterResponse = $this->_adapter->execute($request); |
---|
122 | |
---|
123 | $this->assertEquals( |
---|
124 | $responseData, |
---|
125 | $adapterResponse->getBody() |
---|
126 | ); |
---|
127 | } |
---|
128 | |
---|
129 | public function testExecuteErrorResponse() |
---|
130 | { |
---|
131 | $request = new Solarium_Client_Request(); |
---|
132 | $response = new Zend_Http_Response(404, array(), ''); |
---|
133 | |
---|
134 | $mock = $this->getMock('Zend_Http_Client'); |
---|
135 | $mock->expects($this->once()) |
---|
136 | ->method('request') |
---|
137 | ->will($this->returnValue($response)); |
---|
138 | |
---|
139 | $this->_adapter->setZendHttp($mock); |
---|
140 | |
---|
141 | $this->setExpectedException('Solarium_Client_HttpException'); |
---|
142 | $this->_adapter->execute($request); |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | public function testExecuteHeadRequestReturnsNoData() |
---|
147 | { |
---|
148 | $request = new Solarium_Client_Request(); |
---|
149 | $request->setMethod(Solarium_Client_Request::METHOD_HEAD); |
---|
150 | $response = new Zend_Http_Response(200, array('status' => 'HTTP 1.1 200 OK'), 'data'); |
---|
151 | |
---|
152 | $mock = $this->getMock('Zend_Http_Client'); |
---|
153 | $mock->expects($this->once()) |
---|
154 | ->method('request') |
---|
155 | ->will($this->returnValue($response)); |
---|
156 | |
---|
157 | $this->_adapter->setZendHttp($mock); |
---|
158 | $response = $this->_adapter->execute($request); |
---|
159 | |
---|
160 | $this->assertEquals( |
---|
161 | '', |
---|
162 | $response->getBody() |
---|
163 | ); |
---|
164 | } |
---|
165 | |
---|
166 | } |
---|