Completed
Pull Request — master (#1936)
by François-Xavier
02:45
created

GuzzleTest::checkProxy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Elastica\Test\Transport;
4
5
use Elastica\Document;
6
use Elastica\Exception\Connection\GuzzleException;
7
use Elastica\Query;
8
use Elastica\ResultSet\DefaultBuilder;
9
use Elastica\Test\Base as BaseTest;
10
11
/**
12
 * @internal
13
 */
14
class GuzzleTest extends BaseTest
15
{
16
    public static function setUpbeforeClass(): void
17
    {
18
        if (!\class_exists('GuzzleHttp\\Client')) {
19
            self::markTestSkipped('guzzlehttp/guzzle package should be installed to run guzzle transport tests');
20
        }
21
    }
22
23
    protected function setUp(): void
24
    {
25
        \putenv('http_proxy=');
26
    }
27
28
    protected function tearDown(): void
29
    {
30
        parent::tearDown();
31
        \putenv('http_proxy=');
32
    }
33
34
    /**
35
     * @group functional
36
     */
37
    public function testWithEnvironmentalProxy(): void
38
    {
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
        \putenv('http_proxy='.$this->_getProxyUrl403().'/');
58
59
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
60
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
61
        $this->assertEquals(403, $transferInfo['http_code']);
62
63
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
64
        $client->getConnection()->setProxy('');
65
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
66
        $this->assertEquals(200, $transferInfo['http_code']);
67
68
        \putenv('http_proxy=');
69
    }
70
71
    /**
72
     * @group functional
73
     */
74 View Code Duplication
    public function testWithProxy(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
77
        $client->getConnection()->setProxy($this->_getProxyUrl());
78
79
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
80
        $this->assertEquals(200, $transferInfo['http_code']);
81
    }
82
83
    /**
84
     * @group functional
85
     */
86 View Code Duplication
    public function testWithoutProxy(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
89
        $client->getConnection()->setProxy('');
90
91
        $transferInfo = $client->request('/_nodes')->getTransferInfo();
92
        $this->assertEquals(200, $transferInfo['http_code']);
93
    }
94
95
    /**
96
     * @group functional
97
     */
98
    public function testBodyReuse(): void
99
    {
100
        $client = $this->_getClient(['transport' => 'Guzzle', 'persistent' => false]);
101
        $index = $client->getIndex('elastica_body_reuse_test');
102
        $index->create([], [
103
            'recreate' => true,
104
        ]);
105
        $this->_waitForAllocation($index);
106
107
        $index->addDocument(new Document(1, ['test' => 'test']));
108
109
        $index->refresh();
110
111
        $resultSet = $index->search([
112
            'query' => [
113
                'query_string' => [
114
                    'query' => 'pew pew pew',
115
                ],
116
            ],
117
        ]);
118
119
        $this->assertEquals(0, $resultSet->getTotalHits());
120
121
        $response = $index->request('/_search', 'POST');
122
123
        $builder = new DefaultBuilder();
124
        $resultSet = $builder->buildResultSet($response, Query::create([]));
125
126
        $this->assertEquals(1, $resultSet->getTotalHits());
127
    }
128
129
    /**
130
     * @group unit
131
     */
132
    public function testInvalidConnection(): void
133
    {
134
        $this->expectException(GuzzleException::class);
135
136
        $client = $this->_getClient(['transport' => 'Guzzle', 'port' => 4500, 'persistent' => false]);
137
        $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...
138
        $client->request('_status', 'GET');
139
    }
140
}
141