Completed
Push — master ( 153a9a...119967 )
by Alex
14s queued 12s
created

testWillThrowServiceNotFoundExceptionIfTheOptionsServiceCannotBeFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 21
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\LaminasFactory;
6
7
use Arp\LaminasFactory\ApplicationOptionsProviderTrait;
8
use Arp\LaminasFactory\Exception\ServiceNotCreatedException;
9
use Arp\LaminasFactory\Exception\ServiceNotFoundException;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use PHPUnit\Framework\TestCase;
12
use Psr\Container\ContainerInterface;
13
14
/**
15
 * @author  Alex Patterson <[email protected]>
16
 * @package ArpTest\LaminasFactory
17
 */
18
final class ApplicationOptionsProviderTraitTest extends TestCase
19
{
20
    /**
21
     * @var ContainerInterface|MockObject
22
     */
23
    private $container;
24
25
    /**
26
     * @var string
27
     */
28
    private $optionsKey = 'arp';
29
30
    /**
31
     * @var string
32
     */
33
    private $optionsService = 'config';
34
35
    /**
36
     * Setup the test dependencies.
37
     */
38
    public function setUp(): void
39
    {
40
        $this->container = $this->getMockForAbstractClass(ContainerInterface::class);
41
    }
42
43
    /**
44
     * Assert that the getApplicationOptions() method will throw a ServiceNotFoundException if the configured
45
     * application service cannot be found within the container.
46
     */
47
    public function testWillThrowServiceNotFoundExceptionIfTheOptionsServiceCannotBeFound(): void
48
    {
49
        /** @var ApplicationOptionsProviderTrait|MockObject $subject */
50
        $subject = $this->getMockForTrait(ApplicationOptionsProviderTrait::class);
51
52
        $this->container->expects($this->once())
53
            ->method('has')
54
            ->with($this->optionsService)
55
            ->willReturn(false);
56
57
        $this->expectException(ServiceNotFoundException::class);
58
        $this->expectExceptionMessage(
59
            sprintf(
60
                'The required application options service \'%s\' could not be found in \'%s::%s\'.',
61
                $this->optionsService,
62
                ApplicationOptionsProviderTrait::class,
63
                'getApplicationOptions'
64
            )
65
        );
66
67
        $subject->getApplicationOptions($this->container);
68
    }
69
70
    /**
71
     * Assert that a ServiceNotCreatedException is thrown when the returned ApplicationOptionsService is not of
72
     * type array.
73
     */
74
    public function testWillThrowServiceNotCreatedExceptionIfTheReturnedOptionsServiceIsNotAnArray(): void
75
    {
76
        /** @var ApplicationOptionsProviderTrait|MockObject $subject */
77
        $subject = $this->getMockForTrait(ApplicationOptionsProviderTrait::class);
78
79
        $this->container->expects($this->once())
80
            ->method('has')
81
            ->with($this->optionsService)
82
            ->willReturn(true);
83
84
        $invalid = false; // non-array value
85
86
        $this->container->expects($this->once())
87
            ->method('get')
88
            ->with($this->optionsService)
89
            ->willReturn($invalid);
90
91
        $this->expectException(ServiceNotCreatedException::class);
92
        $this->expectExceptionMessage(
93
            sprintf(
94
                'The application key \'%s\' could not be found within the application options service \'%s\'.',
95
                $this->optionsKey,
96
                $this->optionsService
97
            )
98
        );
99
100
        $subject->getApplicationOptions($this->container);
101
    }
102
103
    /**
104
     * Assert that a ServiceNotCreatedException is thrown when the returned ApplicationOptionsService does not
105
     * container a array key matching the options key.
106
     */
107
    public function testWillThrowServiceNotCreatedExceptionIfTheReturnedOptionsServiceIsMissingOptionsKey(): void
108
    {
109
        /** @var ApplicationOptionsProviderTrait|MockObject $subject */
110
        $subject = $this->getMockForTrait(ApplicationOptionsProviderTrait::class);
111
112
        $this->container->expects($this->once())
113
            ->method('has')
114
            ->with($this->optionsService)
115
            ->willReturn(true);
116
117
        $invalid = [
118
            'foo' => 123,
119
        ];
120
121
        $this->container->expects($this->once())
122
            ->method('get')
123
            ->with($this->optionsService)
124
            ->willReturn($invalid);
125
126
        $this->expectException(ServiceNotCreatedException::class);
127
        $this->expectExceptionMessage(
128
            sprintf(
129
                'The application key \'%s\' could not be found within the application options service \'%s\'.',
130
                $this->optionsKey,
131
                $this->optionsService
132
            )
133
        );
134
135
        $subject->getApplicationOptions($this->container);
136
    }
137
138
    /**
139
     * Assert that a ServiceNotCreatedException is thrown when the resolved service options are not of type array.
140
     */
141
    public function testWillThrowServiceNotCreatedExceptionIfTheServiceOptionsAreNotOfTypeArray(): void
142
    {
143
        /** @var ApplicationOptionsProviderTrait|MockObject $subject */
144
        $subject = $this->getMockForTrait(ApplicationOptionsProviderTrait::class);
145
146
        $this->container->expects($this->once())
147
            ->method('has')
148
            ->with($this->optionsService)
149
            ->willReturn(true);
150
151
        $invalid = [
152
            $this->optionsKey => false, // invalid options type!
153
        ];
154
155
        $this->container->expects($this->once())
156
            ->method('get')
157
            ->with($this->optionsService)
158
            ->willReturn($invalid);
159
160
        $this->expectException(ServiceNotCreatedException::class);
161
        $this->expectExceptionMessage(
162
            sprintf(
163
                'The application options must be an \'array\' or object of type \'%s\'; \'%s\' provided in \'%s::%s\'.',
164
                \Traversable::class,
165
                gettype($invalid[$this->optionsKey]),
166
                ApplicationOptionsProviderTrait::class,
167
                'getApplicationOptions'
168
            )
169
        );
170
171
        $subject->getApplicationOptions($this->container);
172
    }
173
}
174