Passed
Pull Request — master (#3)
by Alex
02:09
created

AbstractPsrBridgeAdapterTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 5
eloc 44
c 1
b 1
f 0
dl 0
loc 109
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testImplementsContainerAdapterInterface() 0 6 1
A testHasServiceWillThrowAnAdapterException() 0 21 1
A testGetServiceWillThrowAdapterException() 0 21 1
A testGetServiceWillThrowNotFoundExceptionForUnknownServiceName() 0 21 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\Container\Adapter;
6
7
use Arp\Container\Adapter\AbstractPsrAdapter;
8
use Arp\Container\Adapter\ContainerAdapterInterface;
9
use Arp\Container\Adapter\Exception\AdapterException;
10
use Arp\Container\Adapter\Exception\NotFoundException as AdapterNotFoundException;
11
use Arp\Container\Exception\ContainerException;
12
use Arp\Container\Exception\NotFoundException;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use PHPUnit\Framework\TestCase;
15
use Psr\Container\ContainerInterface;
16
17
/**
18
 * @covers \Arp\Container\Adapter\AbstractPsrAdapter
19
 *
20
 * @author  Alex Patterson <[email protected]>
21
 * @package ArpTest\Container\Adapter
22
 */
23
final class AbstractPsrBridgeAdapterTest extends TestCase
24
{
25
    /**
26
     * @var ContainerInterface|MockObject
27
     */
28
    private $container;
29
30
    /**
31
     * Set up the test case dependencies
32
     */
33
    public function setUp(): void
34
    {
35
        $this->container = $this->getMockForAbstractClass(ContainerInterface::class);
36
    }
37
38
    /**
39
     * Assert that the class extends ContainerAdapterInterface
40
     */
41
    public function testImplementsContainerAdapterInterface(): void
42
    {
43
        /** @var AbstractPsrAdapter|MockObject $adapter */
44
        $adapter = $this->getMockForAbstractClass(AbstractPsrAdapter::class, [$this->container]);
45
46
        $this->assertInstanceOf(ContainerAdapterInterface::class, $adapter);
47
    }
48
49
    /**
50
     * Assert that and AdapterException will be thrown if the call to hasService() cannot be completed
51
     *
52
     * @throws AdapterException
53
     */
54
    public function testHasServiceWillThrowAnAdapterException(): void
55
    {
56
        /** @var AbstractPsrAdapter|MockObject $adapter */
57
        $adapter = $this->getMockForAbstractClass(AbstractPsrAdapter::class, [$this->container]);
58
59
        $name = 'FooService';
60
61
        $exceptionMessage = 'This is a test exception message';
62
        $exceptionCode = 456;
63
        $exception = new ContainerException($exceptionMessage, $exceptionCode);
64
65
        $this->container->expects($this->once())
66
            ->method('has')
67
            ->with($name)
68
            ->willThrowException($exception);
69
70
        $this->expectException(AdapterException::class);
71
        $this->expectExceptionMessage($exceptionMessage);
72
        $this->expectExceptionCode($exceptionCode);
73
74
        $adapter->hasService($name);
75
    }
76
77
    /**
78
     * Assert that the getService() method will throw a NotFoundException.
79
     *
80
     * @throws AdapterException
81
     * @throws AdapterNotFoundException
82
     */
83
    public function testGetServiceWillThrowNotFoundExceptionForUnknownServiceName(): void
84
    {
85
        /** @var AbstractPsrAdapter|MockObject $adapter */
86
        $adapter = $this->getMockForAbstractClass(AbstractPsrAdapter::class, [$this->container]);
87
88
        $name = 'FooService';
89
90
        $exceptionMessage = 'This is a test exception message';
91
        $exceptionCode = 123;
92
        $exception = new NotFoundException($exceptionMessage, $exceptionCode);
93
94
        $this->container->expects($this->once())
95
            ->method('get')
96
            ->with($name)
97
            ->willThrowException($exception);
98
99
        $this->expectException(AdapterNotFoundException::class);
100
        $this->expectExceptionMessage($exceptionMessage);
101
        $this->expectExceptionCode($exceptionCode);
102
103
        $adapter->getService($name);
104
    }
105
106
    /**
107
     * Assert that a AdapterException is thrown on error in method getService()
108
     *
109
     * @throws AdapterException
110
     */
111
    public function testGetServiceWillThrowAdapterException(): void
112
    {
113
        /** @var AbstractPsrAdapter|MockObject $adapter */
114
        $adapter = $this->getMockForAbstractClass(AbstractPsrAdapter::class, [$this->container]);
115
116
        $name = 'FooService';
117
118
        $exceptionMessage = 'This is a test exception message';
119
        $exceptionCode = 987;
120
        $exception = new ContainerException($exceptionMessage, $exceptionCode);
121
122
        $this->container->expects($this->once())
123
            ->method('get')
124
            ->with($name)
125
            ->willThrowException($exception);
126
127
        $this->expectException(AdapterException::class);
128
        $this->expectExceptionMessage($exceptionMessage);
129
        $this->expectExceptionCode($exceptionCode);
130
131
        $adapter->getService($name);
132
    }
133
}
134