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

SimpleTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 57 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 57
loc 100
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testConnection() 0 9 1
A testFailConnection() 11 11 1
A testWithOneFailConnection() 23 23 1
A testWithNoValidConnection() 22 22 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\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
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...
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
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...
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 {
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...
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
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...
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