php$0 ➔ testWillThrowServiceNotFoundExceptionIfTheOptionsServiceCannotBeFound()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
cc 1
rs 9.568
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
final class ApplicationOptionsProviderTraitTest extends TestCase
18
{
19
    /**
20
     * @var ContainerInterface&MockObject
21
     */
22
    private ContainerInterface $container;
23
24
    private string $optionsKey = 'arp';
25
26
    private string $optionsService = 'config';
27
28
    public function setUp(): void
29
    {
30
        $this->container = $this->createMock(ContainerInterface::class);
31
    }
32
33
    /**
34
     * @throws ServiceNotCreatedException
35
     * @throws ServiceNotFoundException
36
     */
37
    public function testWillThrowServiceNotFoundExceptionIfTheOptionsServiceCannotBeFound(): void
38
    {
39
        $subject = new class () {
40
            use ApplicationOptionsProviderTrait;
41
        };
42
43
        $this->container->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Container\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $this->container->/** @scrutinizer ignore-call */ 
44
                          expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
            ->method('has')
45
            ->with($this->optionsService)
46
            ->willReturn(false);
47
48
        $this->expectException(ServiceNotFoundException::class);
49
        $this->expectExceptionMessage(
50
            sprintf(
51
                'The required application options service \'%s\' could not be found in \'%s::%s\'.',
52
                $this->optionsService,
53
                ApplicationOptionsProviderTrait::class,
54
                'getApplicationOptions'
55
            )
56
        );
57
58
        $subject->getApplicationOptions($this->container);
59
    }
60
61
    /**
62
     * @throws ServiceNotCreatedException
63
     * @throws ServiceNotFoundException
64
     */
65
    public function testWillThrowServiceNotCreatedExceptionIfTheReturnedOptionsServiceIsNotAnArray(): void
66
    {
67
        $subject = new class () {
68
            use ApplicationOptionsProviderTrait;
69
        };
70
71
        $this->container->expects($this->once())
72
            ->method('has')
73
            ->with($this->optionsService)
74
            ->willReturn(true);
75
76
        $this->container->expects($this->once())
77
            ->method('get')
78
            ->with($this->optionsService)
79
            ->willReturn(false);
80
81
        $this->expectException(ServiceNotCreatedException::class);
82
        $this->expectExceptionMessage(
83
            sprintf(
84
                'The application key \'%s\' could not be found within the application options service \'%s\'.',
85
                $this->optionsKey,
86
                $this->optionsService
87
            )
88
        );
89
90
        $subject->getApplicationOptions($this->container);
91
    }
92
93
    /**
94
     * @throws ServiceNotCreatedException
95
     * @throws ServiceNotFoundException
96
     */
97
    public function testWillThrowServiceNotCreatedExceptionIfTheReturnedOptionsServiceIsMissingOptionsKey(): void
98
    {
99
        $subject = new class () {
100
            use ApplicationOptionsProviderTrait;
101
        };
102
103
        $this->container->expects($this->once())
104
            ->method('has')
105
            ->with($this->optionsService)
106
            ->willReturn(true);
107
108
        $invalid = [
109
            'foo' => 123,
110
        ];
111
112
        $this->container->expects($this->once())
113
            ->method('get')
114
            ->with($this->optionsService)
115
            ->willReturn($invalid);
116
117
        $this->expectException(ServiceNotCreatedException::class);
118
        $this->expectExceptionMessage(
119
            sprintf(
120
                'The application key \'%s\' could not be found within the application options service \'%s\'.',
121
                $this->optionsKey,
122
                $this->optionsService
123
            )
124
        );
125
126
        $subject->getApplicationOptions($this->container);
127
    }
128
129
    /**
130
     * @throws ServiceNotCreatedException
131
     * @throws ServiceNotFoundException
132
     */
133
    public function testWillThrowServiceNotCreatedExceptionIfTheServiceOptionsAreNotOfTypeArray(): void
134
    {
135
        $subject = new class () {
136
            use ApplicationOptionsProviderTrait;
137
        };
138
139
        $this->container->expects($this->once())
140
            ->method('has')
141
            ->with($this->optionsService)
142
            ->willReturn(true);
143
144
        $invalid = [
145
            $this->optionsKey => false, // invalid options type!
146
        ];
147
148
        $this->container->expects($this->once())
149
            ->method('get')
150
            ->with($this->optionsService)
151
            ->willReturn($invalid);
152
153
        $this->expectException(ServiceNotCreatedException::class);
154
        $this->expectExceptionMessage(
155
            sprintf(
156
                'The application options must be an \'array\' or object of type \'%s\'; \'%s\' provided in \'%s::%s\'.',
157
                \Traversable::class,
158
                gettype($invalid[$this->optionsKey]),
159
                ApplicationOptionsProviderTrait::class,
160
                'getApplicationOptions'
161
            )
162
        );
163
164
        $subject->getApplicationOptions($this->container);
165
    }
166
}
167