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

StrategyFactoryTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 72
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testNoCollisionWithGlobalNamespace() 0 9 2
A testCreateCallbackStrategy() 0 9 1
A testCreateByName() 0 8 1
A testCreateByClass() 0 6 1
A testCreateByClassName() 0 6 1
A testFailCreate() 0 8 1
1
<?php
2
3
namespace Elastica\Test\Connection\Strategy;
4
5
use Elastica\Connection\Strategy\CallbackStrategy;
6
use Elastica\Connection\Strategy\Simple;
7
use Elastica\Connection\Strategy\StrategyFactory;
8
use Elastica\Test\Base;
9
10
/**
11
 * Description of StrategyFactoryTest.
12
 *
13
 * @author chabior
14
 *
15
 * @internal
16
 */
17
class StrategyFactoryTest extends Base
18
{
19
    /**
20
     * @group unit
21
     */
22
    public function testCreateCallbackStrategy(): void
23
    {
24
        $callback = static function ($connections): void {
0 ignored issues
show
Unused Code introduced by
The parameter $connections 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...
25
        };
26
27
        $strategy = StrategyFactory::create($callback);
28
29
        $this->assertInstanceOf(CallbackStrategy::class, $strategy);
30
    }
31
32
    /**
33
     * @group unit
34
     */
35
    public function testCreateByName(): void
36
    {
37
        $strategyName = 'Simple';
38
39
        $strategy = StrategyFactory::create($strategyName);
40
41
        $this->assertInstanceOf(Simple::class, $strategy);
42
    }
43
44
    /**
45
     * @group unit
46
     */
47
    public function testCreateByClass(): void
48
    {
49
        $strategy = new EmptyStrategy();
50
51
        $this->assertEquals($strategy, StrategyFactory::create($strategy));
52
    }
53
54
    /**
55
     * @group unit
56
     */
57
    public function testCreateByClassName(): void
58
    {
59
        $strategy = StrategyFactory::create(EmptyStrategy::class);
60
61
        $this->assertInstanceOf(EmptyStrategy::class, $strategy);
62
    }
63
64
    /**
65
     * @group unit
66
     */
67
    public function testFailCreate(): void
68
    {
69
        $this->expectException(\InvalidArgumentException::class);
70
71
        $strategy = new \stdClass();
72
73
        StrategyFactory::create($strategy);
74
    }
75
76
    /**
77
     * @group unit
78
     */
79
    public function testNoCollisionWithGlobalNamespace(): void
80
    {
81
        // create collision
82
        if (!\class_exists('Simple')) {
83
            \class_alias('Elastica\Util', 'Simple');
84
        }
85
        $strategy = StrategyFactory::create('Simple');
86
        $this->assertInstanceOf(Simple::class, $strategy);
87
    }
88
}
89