Completed
Push — master ( 0d45ed...ea3502 )
by Nicolas
02:38
created

tests/Connection/Strategy/SimpleTest.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 = function ($connection, $exception, $client) use (&$count): void {
0 ignored issues
show
The parameter $connection is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $exception is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $client is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
            ++$count;
67
        };
68
69
        $client = $this->_getClient([], $callback);
70
        $client->setConnections($connections);
71
72
        $response = $client->request('_aliases');
73
        /* @var $response Response */
74
75
        $this->_checkResponse($response);
76
77
        $this->_checkStrategy($client);
78
79
        $this->assertLessThan(\count($connections), $count);
80
    }
81
82
    /**
83
     * @group functional
84
     */
85 View Code Duplication
    public function testWithNoValidConnection(): void
86
    {
87
        $connections = [
88
            new Connection(['host' => '255.255.255.0', 'timeout' => $this->_timeout]),
89
            new Connection(['host' => '45.45.45.45', 'port' => '80', 'timeout' => $this->_timeout]),
90
            new Connection(['host' => '10.123.213.123', 'timeout' => $this->_timeout]),
91
        ];
92
93
        $count = 0;
94
        $client = $this->_getClient([], function () use (&$count): void {
95
            ++$count;
96
        });
97
98
        $client->setConnections($connections);
99
100
        try {
101
            $client->request('_aliases');
102
            $this->fail('Should throw exception as no connection valid');
103
        } catch (ConnectionException $e) {
104
            $this->assertEquals(\count($connections), $count);
105
        }
106
    }
107
108
    protected function _checkStrategy(Client $client): void
109
    {
110
        $strategy = $client->getConnectionStrategy();
111
112
        $this->assertInstanceOf(Simple::class, $strategy);
113
    }
114
115
    protected function _checkResponse(Response $response): void
116
    {
117
        $this->assertTrue($response->isOk());
118
    }
119
}
120