Passed
Pull Request — master (#3)
by Alex
06:48 queued 04:28
created

testCreateWillThrowFactoryExceptionIfProvidedAdapterIsInvalid()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 10
c 1
b 1
f 0
dl 0
loc 17
rs 9.9332
cc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\Container\Factory;
6
7
use Arp\Container\Adapter\ContainerAdapterInterface;
8
use Arp\Container\Factory\ContainerFactory;
9
use Arp\Factory\Exception\FactoryException;
10
use Arp\Factory\FactoryInterface;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use PHPUnit\Framework\TestCase;
13
use Psr\Container\ContainerInterface;
14
use Psr\Log\LoggerInterface;
15
16
/**
17
 * @covers \Arp\Container\Factory\ContainerFactory
18
 *
19
 * @author  Alex Patterson <[email protected]>
20
 * @package ArpTest\Container\Factory
21
 */
22
final class ContainerFactoryTest extends TestCase
23
{
24
    /**
25
     * @var FactoryInterface|MockObject
26
     */
27
    private $adapterFactory;
28
29
    /**
30
     * Create test case dependencies
31
     */
32
    public function setUp(): void
33
    {
34
        $this->adapterFactory = $this->createMock(FactoryInterface::class);
35
    }
36
37
    /**
38
     * Assert that the container factory implements FactoryInterface
39
     */
40
    public function testImplementsFactoryInterface(): void
41
    {
42
        $factory = new ContainerFactory($this->adapterFactory);
43
44
        $this->assertInstanceOf(FactoryInterface::class, $factory);
45
    }
46
47
    /**
48
     * Assert that if the required 'adapter' configuration is not provided then FactoryException is thrown.
49
     *
50
     * @throws FactoryException
51
     */
52
    public function testCreateWillThrowFactoryExceptionIfTheAdapterConfigurationOptionIsNotProvided(): void
53
    {
54
        $factory = new ContainerFactory($this->adapterFactory);
55
56
        $this->expectException(FactoryException::class);
57
        $this->expectExceptionMessage(
58
            sprintf(
59
                'The required \'adapter\' configuration option is missing in \'%s\'',
60
                ContainerFactory::class
61
            )
62
        );
63
64
        $factory->create([]);
65
    }
66
67
    /**
68
     * Assert that a FactoryException is thrown from create if the provided adapter is not of of
69
     * type ContainerAdapterInterface.
70
     *
71
     * @throws FactoryException
72
     */
73
    public function testCreateWillThrowFactoryExceptionIfProvidedAdapterIsInvalid(): void
74
    {
75
        $factory = new ContainerFactory($this->adapterFactory);
76
77
        $adapter = new \stdClass();
78
79
        $this->expectException(FactoryException::class);
80
        $this->expectExceptionMessage(
81
            sprintf(
82
                'The \'adapter\' configuration option must be a object of type \'%s\'; \'%s\' provided in \'%s\'',
83
                ContainerAdapterInterface::class,
84
                (is_object($adapter) ? get_class($adapter) : gettype($adapter)),
85
                ContainerFactory::class
86
            )
87
        );
88
89
        $factory->create(compact('adapter'));
90
    }
91
92
    /**
93
     * Assert that a valid Container is created when providing configuration options to create() that contains
94
     * and already created adapter instance
95
     *
96
     * @throws FactoryException
97
     */
98
    public function testCreateWillReturnContainerWithAdapterInstance(): void
99
    {
100
        $factory = new ContainerFactory($this->adapterFactory);
101
102
        /** @var ContainerAdapterInterface|MockObject $adapter */
103
        $adapter = $this->getMockForAbstractClass(ContainerAdapterInterface::class);
104
105
        $this->assertInstanceOf(ContainerInterface::class, $factory->create(compact('adapter')));
106
    }
107
108
    /**
109
     * Assert that a valid Container is created when providing configuration options to create() that contains
110
     * adapter configuration as an array
111
     *
112
     * @throws FactoryException
113
     */
114
    public function testCreateWillReturnContainerWithAdapterArrayConfiguration(): void
115
    {
116
        $factory = new ContainerFactory($this->adapterFactory);
117
118
        $config = [
119
            'adapter' => [
120
                'foo' => 'bar',
121
                'test' => new \stdClass(),
122
            ]
123
        ];
124
125
        /** @var ContainerAdapterInterface|MockObject $adapter */
126
        $adapter = $this->getMockForAbstractClass(ContainerAdapterInterface::class);
127
128
        $this->adapterFactory->expects($this->once())
129
            ->method('create')
130
            ->with($config['adapter'])
131
            ->willReturn($adapter);
132
133
        $this->assertInstanceOf(ContainerInterface::class, $factory->create($config));
134
    }
135
}
136