Completed
Push — master ( 9146c8...39ecb4 )
by Nicolas
04:52
created

ConnectionPoolTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 89
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstruct() 0 6 1
A testSetConnections() 0 12 1
A testAddConnection() 0 15 2
A testHasConnection() 0 6 1
A testFailHasConnections() 0 8 1
A getConnections() 0 11 2
A createPool() 0 7 1
1
<?php
2
3
namespace Elastica\Test\Connection;
4
5
use Elastica\Connection;
6
use Elastica\Connection\ConnectionPool;
7
use Elastica\Connection\Strategy\StrategyFactory;
8
use Elastica\Test\Base as BaseTest;
9
10
/**
11
 * @author chabior
12
 *
13
 * @internal
14
 */
15
class ConnectionPoolTest extends BaseTest
16
{
17
    /**
18
     * @group unit
19
     */
20
    public function testConstruct(): void
21
    {
22
        $pool = $this->createPool();
23
24
        $this->assertEquals($this->getConnections(), $pool->getConnections());
25
    }
26
27
    /**
28
     * @group unit
29
     */
30
    public function testSetConnections(): void
31
    {
32
        $pool = $this->createPool();
33
34
        $connections = $this->getConnections(5);
35
36
        $pool->setConnections($connections);
37
38
        $this->assertEquals($connections, $pool->getConnections());
39
40
        $this->assertInstanceOf(ConnectionPool::class, $pool->setConnections($connections));
41
    }
42
43
    /**
44
     * @group unit
45
     */
46
    public function testAddConnection(): void
47
    {
48
        $pool = $this->createPool();
49
        $pool->setConnections([]);
50
51
        $connections = $this->getConnections(5);
52
53
        foreach ($connections as $connection) {
54
            $pool->addConnection($connection);
55
        }
56
57
        $this->assertEquals($connections, $pool->getConnections());
58
59
        $this->assertInstanceOf(ConnectionPool::class, $pool->addConnection($connections[0]));
60
    }
61
62
    /**
63
     * @group unit
64
     */
65
    public function testHasConnection(): void
66
    {
67
        $pool = $this->createPool();
68
69
        $this->assertTrue($pool->hasConnection());
70
    }
71
72
    /**
73
     * @group unit
74
     */
75
    public function testFailHasConnections(): void
76
    {
77
        $pool = $this->createPool();
78
79
        $pool->setConnections([]);
80
81
        $this->assertFalse($pool->hasConnection());
82
    }
83
84
    protected function getConnections(int $quantity = 1): array
85
    {
86
        $params = [];
87
        $connections = [];
88
89
        for ($i = 0; $i < $quantity; ++$i) {
90
            $connections[] = new Connection($params);
91
        }
92
93
        return $connections;
94
    }
95
96
    protected function createPool(): ConnectionPool
97
    {
98
        $connections = $this->getConnections();
99
        $strategy = StrategyFactory::create('Simple');
100
101
        return new ConnectionPool($connections, $strategy);
102
    }
103
}
104