source: sandbox/expresso-solr/expressoMail1_2/inc/solrclient/tests/Solarium/Client/Adapter/PeclHttpTest.php @ 7576

Revision 7576, 6.0 KB checked in by adir, 11 years ago (diff)

Ticket #000 - Adicionando a integracao de buscas com Solr na base a ser isnerida na comunidade

Line 
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
32class Solarium_Client_Adapter_PeclHttpTest extends PHPUnit_Framework_TestCase
33{
34    /**
35     * @var Solarium_Client_Adapter_PeclHttp
36     */
37    protected $_adapter;
38
39    public function setUp()
40    {
41        if (!function_exists('http_get')) {
42            $this->markTestSkipped('Pecl_http not available, skipping PeclHttp adapter tests');
43        }
44
45        $this->_adapter = new Solarium_Client_Adapter_PeclHttp(array('timeout' => 10));
46    }
47
48    /**
49     * @dataProvider requestProvider
50     */
51    public function testToHttpRequestWithMethod($request, $method, $support)
52    {
53        try {
54            $httpRequest = $this->_adapter->toHttpRequest($request);
55            $this->assertEquals($httpRequest->getMethod(), $method);
56        } catch (Solarium_Exception $e) {
57            if ($support) {
58                $this->fail("Unsupport method: {$request->getMethod()}");
59            }
60        }
61    }
62
63    public function requestProvider()
64    {
65        // prevents undefined constants errors
66        if (function_exists('http_get')) {
67            $methods = array(
68                Solarium_Client_Request::METHOD_GET  => array(
69                    'method' => HTTP_METH_GET,
70                    'support' => true
71                ),
72                Solarium_Client_Request::METHOD_POST => array(
73                    'method' => HTTP_METH_POST,
74                    'support' => true
75                ),
76                Solarium_Client_Request::METHOD_HEAD => array(
77                    'method' => HTTP_METH_HEAD,
78                    'support' => true
79                ),
80                'PUT'                                => array(
81                    'method' => HTTP_METH_PUT,
82                    'support' => false
83                ),
84                'DELETE'                             => array(
85                    'method' => HTTP_METH_DELETE,
86                    'support' => false
87                ),
88            );
89
90            foreach ($methods as $method => $options) {
91                $request = new Solarium_Client_Request;
92                $request->setMethod($method);
93                $data[] = array_merge(array($request), $options);
94            }
95
96            return $data;
97        }
98    }
99
100    public function testToHttpRequestWithHeaders()
101    {
102        $request = new Solarium_Client_Request(array(
103            'header' => array(
104                'Content-Type: application/json',
105                'User-Agent: Foo'
106            )
107        ));
108
109        $httpRequest = $this->_adapter->toHttpRequest($request);
110        $this->assertEquals(array(
111            'timeout' => 10,
112            'headers' => array(
113                'Content-Type' => 'application/json',
114                'User-Agent' => 'Foo'
115            )
116        ), $httpRequest->getOptions());
117    }
118
119    public function testToHttpRequestWithDefaultContentType()
120    {
121        $request = new Solarium_Client_Request;
122        $request->setMethod(Solarium_Client_Request::METHOD_POST);
123
124        $httpRequest = $this->_adapter->toHttpRequest($request);
125        $this->assertEquals(array(
126            'timeout' => 10,
127            'headers' => array(
128                'Content-Type' => 'text/xml; charset=utf-8',
129            )
130        ), $httpRequest->getOptions());
131    }
132
133    public function testExecute()
134    {
135        $statusCode = 200;
136        $statusMessage = 'OK';
137        $body = 'data';
138        $data = <<<EOF
139HTTP/1.1 $statusCode $statusMessage
140X-Foo: test
141
142$body
143EOF;
144        $request = new Solarium_Client_Request();
145
146        $mockHttpRequest = $this->getMock('HttpRequest');
147        $mockHttpRequest->expects($this->once())
148                        ->method('send')
149                        ->will($this->returnValue(HttpMessage::factory($data)));
150        $mock = $this->getMock('Solarium_Client_Adapter_PeclHttp', array('toHttpRequest'));
151        $mock->expects($this->once())
152             ->method('toHttpRequest')
153             ->with($request)
154             ->will($this->returnValue($mockHttpRequest));
155
156        $response = $mock->execute($request);
157        $this->assertEquals($body, $response->getBody());
158        $this->assertEquals($statusCode, $response->getStatusCode());
159        $this->assertEquals($statusMessage, $response->getStatusMessage());
160    }
161
162    /**
163     * @expectedException Solarium_Client_HttpException
164     */
165    public function testExecuteWithException()
166    {
167        $this->_adapter->setPort(-1); // this forces an error
168        $request = new Solarium_Client_Request();
169        $this->_adapter->execute($request);
170    }
171
172}
Note: See TracBrowser for help on using the repository browser.