1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Connection\Strategy; |
4
|
|
|
|
5
|
|
|
use Elastica\Client; |
6
|
|
|
use Elastica\Connection; |
7
|
|
|
use Elastica\Connection\Strategy\Simple; |
8
|
|
|
use Elastica\Exception\ConnectionException; |
9
|
|
|
use Elastica\Response; |
10
|
|
|
use Elastica\Test\Base; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Description of SimplyTest. |
14
|
|
|
* |
15
|
|
|
* @author chabior |
16
|
|
|
* |
17
|
|
|
* @internal |
18
|
|
|
*/ |
19
|
|
|
class SimpleTest extends Base |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int Number of seconds to wait before timeout is called. Is set low for tests to have fast tests. |
23
|
|
|
*/ |
24
|
|
|
protected $_timeout = 1; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @group functional |
28
|
|
|
*/ |
29
|
|
|
public function testConnection(): void |
30
|
|
|
{ |
31
|
|
|
$client = $this->_getClient(); |
32
|
|
|
$response = $client->request('_aliases'); |
33
|
|
|
|
34
|
|
|
$this->_checkResponse($response); |
35
|
|
|
|
36
|
|
|
$this->_checkStrategy($client); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @group functional |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function testFailConnection(): void |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$this->expectException(\Elastica\Exception\ConnectionException::class); |
45
|
|
|
|
46
|
|
|
$config = ['host' => '255.255.255.0', 'timeout' => $this->_timeout]; |
47
|
|
|
$client = $this->_getClient($config); |
48
|
|
|
|
49
|
|
|
$this->_checkStrategy($client); |
50
|
|
|
|
51
|
|
|
$client->request('_aliases'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @group functional |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function testWithOneFailConnection(): void |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$connections = [ |
60
|
|
|
new Connection(['host' => '255.255.255.0', 'timeout' => $this->_timeout]), |
61
|
|
|
new Connection(['host' => $this->_getHost(), 'timeout' => $this->_timeout]), |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
$count = 0; |
65
|
|
|
$callback = static function ($connection, $exception, $client) use (&$count): void { |
|
|
|
|
66
|
|
|
++$count; |
67
|
|
|
}; |
68
|
|
|
|
69
|
|
|
$client = $this->_getClient([], $callback); |
70
|
|
|
$client->setConnections($connections); |
71
|
|
|
|
72
|
|
|
$response = $client->request('_aliases'); |
73
|
|
|
|
74
|
|
|
$this->_checkResponse($response); |
75
|
|
|
|
76
|
|
|
$this->_checkStrategy($client); |
77
|
|
|
|
78
|
|
|
$this->assertLessThan(\count($connections), $count); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @group functional |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function testWithNoValidConnection(): void |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$connections = [ |
87
|
|
|
new Connection(['host' => '255.255.255.0', 'timeout' => $this->_timeout]), |
88
|
|
|
new Connection(['host' => '45.45.45.45', 'port' => '80', 'timeout' => $this->_timeout]), |
89
|
|
|
new Connection(['host' => '10.123.213.123', 'timeout' => $this->_timeout]), |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$count = 0; |
93
|
|
|
$client = $this->_getClient([], static function () use (&$count): void { |
94
|
|
|
++$count; |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
$client->setConnections($connections); |
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
$client->request('_aliases'); |
101
|
|
|
$this->fail('Should throw exception as no connection valid'); |
102
|
|
|
} catch (ConnectionException $e) { |
103
|
|
|
$this->assertEquals(\count($connections), $count); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function _checkStrategy(Client $client): void |
108
|
|
|
{ |
109
|
|
|
$strategy = $client->getConnectionStrategy(); |
110
|
|
|
|
111
|
|
|
$this->assertInstanceOf(Simple::class, $strategy); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function _checkResponse(Response $response): void |
115
|
|
|
{ |
116
|
|
|
$this->assertTrue($response->isOk()); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.