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 |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
$client->request('_stats', 'GET'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
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.