Completed
Pull Request — master (#1911)
by Sam
03:08
created

GuzzleTest::testBodyReuse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
cc 1
nc 1
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
/**
11
 * @internal
12
 */
13
class GuzzleTest extends BaseTest
14
{
15
    public static function setUpbeforeClass(): void
16
    {
17
        if (!\class_exists('GuzzleHttp\\Client')) {
18
            self::markTestSkipped('guzzlehttp/guzzle package should be installed to run guzzle transport tests');
19
        }
20
    }
21
22
    protected function setUp(): void
23
    {
24
        \putenv('http_proxy=');
25
    }
26
27
    protected function tearDown(): void
28
    {
29
        parent::tearDown();
30
        \putenv('http_proxy=');
31
    }
32
33
    /**
34
     * @group functional
35
     */
36
    public function testWithEnvironmentalProxy(): void
37
    {
38
        $this->checkProxy($this->_getProxyUrl());
39
        \putenv('http_proxy='.$this->_getProxyUrl().'/');
40
41
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
42
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
43
        $this->assertEquals(200, $transferInfo['http_code']);
44
45
        $client->getConnection()->setProxy(null); // will not change anything
46
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
47
        $this->assertEquals(200, $transferInfo['http_code']);
48
49
        \putenv('http_proxy=');
50
    }
51
52
    /**
53
     * @group functional
54
     */
55
    public function testWithEnabledEnvironmentalProxy(): void
56
    {
57
        $this->checkProxy($this->_getProxyUrl403());
58
        \putenv('http_proxy='.$this->_getProxyUrl403().'/');
59
60
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
61
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
62
        $this->assertEquals(403, $transferInfo['http_code']);
63
64
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
65
        $client->getConnection()->setProxy('');
66
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
67
        $this->assertEquals(200, $transferInfo['http_code']);
68
69
        \putenv('http_proxy=');
70
    }
71
72
    /**
73
     * @group functional
74
     */
75
    public function testWithProxy(): void
76
    {
77
        $this->checkProxy($this->_getProxyUrl());
78
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
79
        $client->getConnection()->setProxy($this->_getProxyUrl());
80
81
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
82
        $this->assertEquals(200, $transferInfo['http_code']);
83
    }
84
85
    /**
86
     * @group functional
87
     */
88
    public function testWithoutProxy(): void
89
    {
90
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
91
        $client->getConnection()->setProxy('');
92
93
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
94
        $this->assertEquals(200, $transferInfo['http_code']);
95
    }
96
97
    /**
98
     * @group functional
99
     */
100
    public function testBodyReuse(): void
101
    {
102
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
103
        $index = $client->getIndex('elastica_body_reuse_test');
104
        $index->create([], [
105
            'recreate' => true,
106
        ]);
107
        $this->_waitForAllocation($index);
108
109
        $index->addDocument(new Document(1, ['test' => 'test']));
110
111
        $index->refresh();
112
113
        $resultSet = $index->search([
114
            'query' => [
115
                'query_string' => [
116
                    'query' => 'pew pew pew',
117
                ],
118
            ],
119
        ]);
120
121
        $this->assertEquals(0, $resultSet->getTotalHits());
122
123
        $response = $index->request('/_search', 'POST');
124
125
        $builder = new DefaultBuilder();
126
        $resultSet = $builder->buildResultSet($response, Query::create([]));
127
128
        $this->assertEquals(1, $resultSet->getTotalHits());
129
    }
130
131
    /**
132
     * @group unit
133
     */
134
    public function testInvalidConnection(): void
135
    {
136
        $this->expectException(\Elastica\Exception\Connection\GuzzleException::class);
137
138
        $client = $this->_getClient(['transport' => 'Guzzle', 'port' => 4500, 'persistent' => false]);
139
        $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...
140
        $client->request('_status', 'GET');
141
    }
142
143
    protected function checkProxy($url): void
144
    {
145
        $url = \parse_url($url);
146
        $this->_checkConnection($url['host'], $url['port']);
147
    }
148
}
149