AdapterFactoryTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
c 1
b 1
f 0
lcom 1
cbo 2
dl 0
loc 104
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetAdapters() 0 12 1
A testAddAdapter() 0 10 1
A testCreateAdapter() 0 23 1
A testCreateAdapterWithNoName() 0 14 1
A testCreateAdapterWithNoOptions() 0 12 1
A testCreateAdapterWithInvalidAdapter() 0 13 1
1
<?php
2
3
namespace AMQPAL\Adapter;
4
5
class AdapterFactoryTest extends \PHPUnit_Framework_TestCase
6
{
7
    public function testSetAdapters()
8
    {
9
        $adapters = [
10
            'foo' => AdapterStub::class,
11
            'bar' => AdapterStub::class,
12
        ];
13
14
        $factory = new AdapterFactory();
15
        $factory->setAdapters($adapters);
16
17
        static::assertEquals($adapters, $factory->getAdapters());
18
    }
19
20
    public function testAddAdapter()
21
    {
22
        $factory = new AdapterFactory();
23
        $factory->setAdapters([]);
24
25
        static::assertEquals([], $factory->getAdapters());
26
27
        $factory->setAdapter('foo', AdapterStub::class);
28
        static::assertEquals(['foo' => AdapterStub::class], $factory->getAdapters());
29
    }
30
31
    public function testCreateAdapter()
32
    {
33
        $adapters = [
34
            'foo' => AdapterStub::class,
35
            'bar' => AdapterStub::class,
36
        ];
37
38
        $factory = new AdapterFactory();
39
        $factory->setAdapters($adapters);
40
41
        static::assertEquals($adapters, $factory->getAdapters());
42
43
        $options = [
44
            'name' => 'foo',
45
            'options' => [
46
                'host' => 'foo-hostname'
47
            ]
48
        ];
49
        $adapter = $factory->createAdapter($options);
50
51
        static::assertInstanceOf(AdapterStub::class, $adapter);
52
        static::assertEquals($adapter->options, ['host' => 'foo-hostname']);
0 ignored issues
show
Bug introduced by
Accessing options on the interface AMQPAL\Adapter\AdapterInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
53
    }
54
55
    /**
56
     * @expectedException \AMQPAL\Adapter\Exception\InvalidArgumentException
57
     * @expectedExceptionMessage Unable to find 'name' key.
58
     */
59
    public function testCreateAdapterWithNoName()
60
    {
61
        $adapters = [];
62
63
        $factory = new AdapterFactory();
64
        $factory->setAdapters($adapters);
65
66
        $options = [
67
            'options' => [
68
69
            ]
70
        ];
71
        $factory->createAdapter($options);
72
    }
73
74
    /**
75
     * @expectedException \AMQPAL\Adapter\Exception\InvalidArgumentException
76
     * @expectedExceptionMessage Unable to find 'options' key.
77
     */
78
    public function testCreateAdapterWithNoOptions()
79
    {
80
        $adapters = [];
81
82
        $factory = new AdapterFactory();
83
        $factory->setAdapters($adapters);
84
85
        $options = [
86
            'name' => 'foo',
87
        ];
88
        $factory->createAdapter($options);
89
    }
90
91
    /**
92
     * @expectedException \AMQPAL\Adapter\Exception\OutOfBoundsException
93
     * @expectedExceptionMessage Unable to find adapter 'foo'.
94
     */
95
    public function testCreateAdapterWithInvalidAdapter()
96
    {
97
        $adapters = [];
98
99
        $factory = new AdapterFactory();
100
        $factory->setAdapters($adapters);
101
102
        $options = [
103
            'name' => 'foo',
104
            'options' => []
105
        ];
106
        $factory->createAdapter($options);
107
    }
108
}
109