Completed
Push — master ( 4cd93a...e8f4ec )
by Thomas Mauro
10:51
created

AdapterFactoryTest::testSetAdapters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
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