Passed
Pull Request — master (#9)
by Alex
07:50
created

php$2 ➔ testWillThrowServiceNotCreatedExceptionIfTheReturnedOptionsServiceIsMissingOptionsKey()   A

Complexity

Conditions 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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