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

CallbackStrategyTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testInvoke() 0 16 1
A testConnection() 0 21 1
1
<?php
2
3
namespace Elastica\Test\Connection\Strategy;
4
5
use Elastica\Connection;
6
use Elastica\Connection\Strategy\CallbackStrategy;
7
use Elastica\Test\Base;
8
9
/**
10
 * Description of CallbackStrategyTest.
11
 *
12
 * @author chabior
13
 *
14
 * @internal
15
 */
16
class CallbackStrategyTest extends Base
17
{
18
    /**
19
     * @group unit
20
     */
21
    public function testInvoke(): void
22
    {
23
        $count = 0;
24
25
        $callback = static function ($connections) use (&$count): Connection {
26
            ++$count;
27
28
            return \current($connections);
29
        };
30
31
        $mock = $this->createMock(Connection::class);
32
        $strategy = new CallbackStrategy($callback);
33
        $strategy->getConnection([$mock]);
34
35
        $this->assertEquals(1, $count);
36
    }
37
38
    /**
39
     * @group functional
40
     */
41
    public function testConnection(): void
42
    {
43
        $count = 0;
44
45
        $config = ['connectionStrategy' => static function ($connections) use (&$count): Connection {
46
            ++$count;
47
48
            return \current($connections);
49
        }];
50
51
        $client = $this->_getClient($config);
52
        $response = $client->request('_aliases');
53
54
        $this->assertEquals(1, $count);
55
56
        $this->assertTrue($response->isOk());
57
58
        $strategy = $client->getConnectionStrategy();
59
60
        $this->assertInstanceOf(CallbackStrategy::class, $strategy);
61
    }
62
}
63