Completed
Push — master ( 129cf7...e70161 )
by Ema
02:11
created

GuzzleTest::setUpbeforeClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Elastica\Test\Transport;
4
5
use Elastica\Document;
6
use Elastica\Query;
7
use Elastica\ResultSet\DefaultBuilder;
8
use Elastica\Test\Base as BaseTest;
9
10
class GuzzleTest extends BaseTest
11
{
12
    public static function setUpbeforeClass(): void
13
    {
14
        if (!\class_exists('GuzzleHttp\\Client')) {
15
            self::markTestSkipped('guzzlehttp/guzzle package should be installed to run guzzle transport tests');
16
        }
17
    }
18
19
    /**
20
     * @group functional
21
     */
22
    public function testWithEnvironmentalProxy()
23
    {
24
        $this->checkProxy($this->_getProxyUrl());
25
        \putenv('http_proxy='.$this->_getProxyUrl().'/');
26
27
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
28
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
29
        $this->assertEquals(200, $transferInfo['http_code']);
30
31
        $client->getConnection()->setProxy(null); // will not change anything
32
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
33
        $this->assertEquals(200, $transferInfo['http_code']);
34
35
        \putenv('http_proxy=');
36
    }
37
38
    /**
39
     * @group functional
40
     */
41
    public function testWithEnabledEnvironmentalProxy()
42
    {
43
        $this->checkProxy($this->_getProxyUrl403());
44
        \putenv('http_proxy='.$this->_getProxyUrl403().'/');
45
46
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
47
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
48
        $this->assertEquals(403, $transferInfo['http_code']);
49
50
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
51
        $client->getConnection()->setProxy('');
52
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
53
        $this->assertEquals(200, $transferInfo['http_code']);
54
55
        \putenv('http_proxy=');
56
    }
57
58
    /**
59
     * @group functional
60
     */
61
    public function testWithProxy()
62
    {
63
        $this->checkProxy($this->_getProxyUrl());
64
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
65
        $client->getConnection()->setProxy($this->_getProxyUrl());
66
67
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
68
        $this->assertEquals(200, $transferInfo['http_code']);
69
    }
70
71
    /**
72
     * @group functional
73
     */
74
    public function testWithoutProxy()
75
    {
76
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
77
        $client->getConnection()->setProxy('');
78
79
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
80
        $this->assertEquals(200, $transferInfo['http_code']);
81
    }
82
83
    /**
84
     * @group functional
85
     */
86
    public function testBodyReuse()
87
    {
88
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
89
        $index = $client->getIndex('elastica_body_reuse_test');
90
        $index->create([], true);
91
        $this->_waitForAllocation($index);
92
93
        $index->addDocument(new Document(1, ['test' => 'test']));
94
95
        $index->refresh();
96
97
        $resultSet = $index->search([
98
            'query' => [
99
                'query_string' => [
100
                    'query' => 'pew pew pew',
101
                ],
102
            ],
103
        ]);
104
105
        $this->assertEquals(0, $resultSet->getTotalHits());
106
107
        $response = $index->request('/_search', 'POST');
108
109
        $builder = new DefaultBuilder();
110
        $resultSet = $builder->buildResultSet($response, Query::create([]));
111
112
        $this->assertEquals(1, $resultSet->getTotalHits());
113
    }
114
115
    /**
116
     * @group unit
117
     */
118
    public function testInvalidConnection()
119
    {
120
        $this->expectException(\Elastica\Exception\Connection\GuzzleException::class);
121
122
        $client = $this->_getClient(['transport' => 'Guzzle', 'port' => 4500, 'persistent' => false]);
123
        $response = $client->request('_stats', 'GET');
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
124
        $client->request('_status', 'GET');
125
    }
126
127
    protected function checkProxy($url)
128
    {
129
        $url = \parse_url($url);
130
        $this->_checkConnection($url['host'], $url['port']);
131
    }
132
133
    protected function setUp(): void
134
    {
135
        \putenv('http_proxy=');
136
    }
137
138
    protected function tearDown(): void
139
    {
140
        parent::tearDown();
141
        \putenv('http_proxy=');
142
    }
143
}
144