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

RoundRobinTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 51.33 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 58
loc 113
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testOldStrategySet() 0 7 1
A testConnection() 0 10 1
A testFailConnection() 11 11 1
A testWithOneFailConnection() 23 23 1
A testWithNoValidConnection() 23 23 2
A _checkStrategy() 0 6 1
A _checkResponse() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Elastica\Test\Connection\Strategy;
4
5
use Elastica\Client;
6
use Elastica\Connection;
7
use Elastica\Connection\Strategy\RoundRobin;
8
use Elastica\Exception\ConnectionException;
9
use Elastica\Response;
10
use Elastica\Test\Base;
11
12
/**
13
 * Description of RoundRobinTest.
14
 *
15
 * @author chabior
16
 *
17
 * @internal
18
 */
19
class RoundRobinTest 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
        $config = ['connectionStrategy' => 'RoundRobin'];
32
        $client = $this->_getClient($config);
33
        $response = $client->request('_aliases');
34
35
        $this->_checkResponse($response);
36
37
        $this->_checkStrategy($client);
38
    }
39
40
    /**
41
     * @group unit
42
     */
43
    public function testOldStrategySet(): void
44
    {
45
        $config = ['roundRobin' => true];
46
        $client = $this->_getClient($config);
47
48
        $this->_checkStrategy($client);
49
    }
50
51
    /**
52
     * @group functional
53
     */
54 View Code Duplication
    public function testFailConnection(): 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...
55
    {
56
        $this->expectException(\Elastica\Exception\ConnectionException::class);
57
58
        $config = ['connectionStrategy' => 'RoundRobin', 'host' => '255.255.255.0', 'timeout' => $this->_timeout];
59
        $client = $this->_getClient($config);
60
61
        $this->_checkStrategy($client);
62
63
        $client->request('_aliases');
64
    }
65
66
    /**
67
     * @group functional
68
     */
69 View Code Duplication
    public function testWithOneFailConnection(): 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...
70
    {
71
        $connections = [
72
            new Connection(['host' => '255.255.255.0', 'timeout' => $this->_timeout]),
73
            new Connection(['host' => $this->_getHost(), 'timeout' => $this->_timeout]),
74
        ];
75
76
        $count = 0;
77
        $callback = static function ($connection, $exception, $client) use (&$count): void {
0 ignored issues
show
Unused Code introduced by
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...
Unused Code introduced by
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...
Unused Code introduced by
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...
78
            ++$count;
79
        };
80
81
        $client = $this->_getClient(['connectionStrategy' => 'RoundRobin'], $callback);
82
        $client->setConnections($connections);
83
84
        $response = $client->request('_aliases');
85
86
        $this->_checkResponse($response);
87
88
        $this->_checkStrategy($client);
89
90
        $this->assertLessThan(\count($connections), $count);
91
    }
92
93
    /**
94
     * @group functional
95
     */
96 View Code Duplication
    public function testWithNoValidConnection(): 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...
97
    {
98
        $connections = [
99
            new Connection(['host' => '255.255.255.0', 'timeout' => $this->_timeout]),
100
            new Connection(['host' => '45.45.45.45', 'port' => '80', 'timeout' => $this->_timeout]),
101
            new Connection(['host' => '10.123.213.123', 'timeout' => $this->_timeout]),
102
        ];
103
104
        $count = 0;
105
        $client = $this->_getClient(['roundRobin' => true], static function () use (&$count): void {
106
            ++$count;
107
        });
108
109
        $client->setConnections($connections);
110
111
        try {
112
            $client->request('_aliases');
113
            $this->fail('Should throw exception as no connection valid');
114
        } catch (ConnectionException $e) {
115
            $this->assertEquals(\count($connections), $count);
116
            $this->_checkStrategy($client);
117
        }
118
    }
119
120
    protected function _checkStrategy(Client $client): void
121
    {
122
        $strategy = $client->getConnectionStrategy();
123
124
        $this->assertInstanceOf(RoundRobin::class, $strategy);
125
    }
126
127
    protected function _checkResponse(Response $response): void
128
    {
129
        $this->assertTrue($response->isOk());
130
    }
131
}
132