Passed
Push — master ( b7d08e...c579b1 )
by Max
02:14
created

ManagerTest::testClassExistence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EasyDictionary;
6
7
use EasyDictionary\DataProvider\Simple;
8
use EasyDictionary\Dictionary\Simple as SimpleDictionary;
9
use EasyDictionary\Interfaces\ConfigInterface;
10
use EasyDictionary\Interfaces\DataProviderInterface;
11
use EasyDictionary\Interfaces\DictionaryInterface;
12
use PHPUnit\Framework\TestCase;
13
use Psr\SimpleCache\CacheInterface;
14
15
/**
16
 * @coversDefaultClass \EasyDictionary\Manager
17
 */
18
class ManagerTest extends TestCase
19
{
20
    /**
21
     * @covers \EasyDictionary\Manager
22
     */
23
    public function testClassExistence()
24
    {
25
        self::assertTrue(class_exists('\EasyDictionary\Manager'));
26
    }
27
28
    /**
29
     * @covers ::__construct
30
     * @covers ::setConfig
31
     * @covers ::getConfig
32
     */
33
    public function testSetConfigFromConstructor()
34
    {
35
        $config = $this->createMock(ConfigInterface::class);
36
        $manager = new Manager($config);
37
38
        self::assertEquals($config, $manager->getConfig());
39
    }
40
41
    /**
42
     * @covers ::__construct
43
     * @covers ::getConfig
44
     * @covers ::get
45
     *
46
     * @expectedException \EasyDictionary\Exception\RuntimeException
47
     * @expectedExceptionMessage Config not found
48
     */
49
    public function testGetDictionaryWithoutConfig()
50
    {
51
        $manager = new Manager();
52
        $manager->get('test');
53
    }
54
55
    /**
56
     * @covers ::__construct
57
     * @covers ::setConfig
58
     * @covers ::getConfig
59
     * @covers ::get
60
     *
61
     * @expectedException \EasyDictionary\Exception\RuntimeException
62
     * @expectedExceptionMessage  Dictionary with key "test" not found
63
     */
64
    public function testGetANonExistentDictionary()
65
    {
66
        $config = $this->createMock(ConfigInterface::class);
67
        $manager = new Manager($config);
68
        $manager->get('test');
69
    }
70
71
    /**
72
     * @covers ::__construct
73
     * @covers ::add
74
     * @covers ::get
75
     */
76
    public function testAddNewDictionary()
77
    {
78
        $dictionaryName = 'test';
79
        $dictionary = $this->createMock(DictionaryInterface::class);
80
        $dictionary->method('getName')->willReturn($dictionaryName);
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

80
        $dictionary->/** @scrutinizer ignore-call */ 
81
                     method('getName')->willReturn($dictionaryName);

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...
81
82
        $manager = new Manager();
83
        $manager->add($dictionary);
84
85
        self::assertEquals($dictionary, $manager->get($dictionaryName));
86
    }
87
88
    /**
89
     * @covers ::__construct
90
     * @covers ::add
91
     * @covers ::get
92
     *
93
     * @expectedException \EasyDictionary\Exception\RuntimeException
94
     * @expectedExceptionMessage The dictionary with key "test" already exists
95
     */
96
    public function testAddDuplicatedDictionary()
97
    {
98
        $dictionaryName = 'test';
99
        $dictionary = $this->createMock(DictionaryInterface::class);
100
        $dictionary->method('getName')->willReturn($dictionaryName);
101
102
        $manager = new Manager();
103
        $manager->add($dictionary);
104
        $manager->add($dictionary);
105
106
        self::assertEquals($dictionary, $manager->get($dictionaryName));
107
    }
108
109
    /**
110
     * @covers ::get
111
     * @covers ::add
112
     * @covers ::create
113
     */
114
    public function testCreateSimpleDictionary()
115
    {
116
        $cacheMock = $this->createMock(CacheInterface::class);
117
        $dataProviderMock = $this->createMock(DataProviderInterface::class);
118
119
        $dictionaryMock = $this->createMock(DictionaryInterface::class);
120
        $dictionaryMock->method('getName')->willReturn('test');
121
        $dictionaryMock->expects(self::once())->method('getName');
122
        $dictionaryMock->expects(self::once())->method('setDataProvider')->with($dataProviderMock);
123
        $dictionaryMock->expects(self::once())->method('setName');
124
        $dictionaryMock->expects(self::once())->method('setDefaultView')->with(null);
125
        $dictionaryMock->expects(self::once())->method('setSearchFields')->with([]);
126
        $dictionaryMock->expects(self::once())->method('setCache')->with($cacheMock, 1);
127
128
        $config = $this->createMock(ConfigInterface::class);
129
        $config->method('getCache')->with('testCache')->willReturn($cacheMock);
130
        $config->expects(self::once())->method('getDefaultDataProviderClass');
131
        $config->expects(self::once())->method('getDefaultDictionaryClass');
132
        $config->method('getDictionaryConfig')->willReturn([
133
            'test' => [
134
                'cache' => 'testCache',
135
                'cacheTTL' => 1,
136
                'data' => []
137
            ]
138
        ]);
139
140
        $manager = $this->createPartialMock(Manager::class, [
141
            'createDictionary',
142
            'createDataProvider',
143
            'getConfig',
144
        ]);
145
146
        $manager->method('createDictionary')->willReturn($dictionaryMock);
147
        $manager->method('createDataProvider')->willReturn($dataProviderMock);
148
        $manager->method('getConfig')->willReturn($config);
149
150
        self::assertEquals($manager->get('test'), $manager->get('test'));
0 ignored issues
show
Bug introduced by
The method get() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

150
        self::assertEquals($manager->/** @scrutinizer ignore-call */ get('test'), $manager->get('test'));

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...
151
    }
152
153
    /**
154
     * @covers \EasyDictionary\AbstractDictionary
155
     * @covers \EasyDictionary\DataProvider\Simple
156
     * @covers ::<public>
157
     * @covers ::create
158
     * @covers ::createDataProvider
159
     */
160
    public function testCreateRealDictionary()
161
    {
162
        $config = $this->createMock(ConfigInterface::class);
163
        $config->expects(self::once())->method('getDefaultDataProviderClass')
164
            ->willReturn(Simple::class);
165
        $config->expects(self::once())->method('getDefaultDictionaryClass')
166
            ->willReturn(SimpleDictionary::class);
167
168
        $config->method('getDictionaryConfig')->willReturn([
169
            'test' => [
170
                'data' => []
171
            ]
172
        ]);
173
174
        $manager = new Manager();
175
        $manager->setConfig($config);
176
177
        self::assertEquals($manager->get('test'), $manager->get('test'));
178
    }
179
180
    /**
181
     * @covers ::<public>
182
     * @covers ::create
183
     * @covers ::createDataProvider
184
     * @expectedException \EasyDictionary\Exception\InvalidConfigurationException
185
     * @expectedExceptionMessage Class "/bad/dataprovider/class" not found
186
     */
187
    public function testGetExceptionOfUndefinedDataProviderClass()
188
    {
189
        $config = $this->createMock(ConfigInterface::class);
190
        $config->method('getDictionaryConfig')->willReturn([
191
            'test' => [
192
                'data' => [
193
                    'class' => '/bad/dataprovider/class',
194
                ]
195
            ]
196
        ]);
197
198
        $manager = new Manager();
199
        $manager->setConfig($config);
200
        $manager->get('test');
201
    }
202
203
    /**
204
     * @covers ::<public>
205
     * @covers ::create
206
     * @covers ::createDataProvider
207
     * @expectedException \EasyDictionary\Exception\InvalidConfigurationException
208
     * @expectedExceptionMessage Class "stdClass" is not implement required interface
209
     */
210
    public function testGetExceptionOfBadDataProviderClass()
211
    {
212
        $config = $this->createMock(ConfigInterface::class);
213
        $config->method('getDictionaryConfig')->willReturn([
214
            'test' => [
215
                'data' => [
216
                    'class' => \stdClass::class,
217
                ]
218
            ]
219
        ]);
220
221
        $manager = new Manager();
222
        $manager->setConfig($config);
223
        $manager->get('test');
224
    }
225
226
    /**
227
     * @covers \EasyDictionary\DataProvider\Simple
228
     * @covers ::<public>
229
     * @covers ::create
230
     * @covers ::createDataProvider
231
     * @expectedException \EasyDictionary\Exception\InvalidConfigurationException
232
     * @expectedExceptionMessage Class "/bad/dictionary/class" not found
233
     */
234
    public function testGetExceptionOfUndefinedDictionaryClass()
235
    {
236
        $config = $this->createMock(ConfigInterface::class);
237
        $config->method('getDictionaryConfig')->willReturn([
238
            'test' => [
239
                'class' => '/bad/dictionary/class',
240
                'data' => [
241
                    'class' => Simple::class,
242
                ]
243
            ]
244
        ]);
245
246
        $manager = new Manager();
247
        $manager->setConfig($config);
248
        $manager->get('test');
249
    }
250
251
    /**
252
     * @covers \EasyDictionary\DataProvider\Simple
253
     * @covers ::<public>
254
     * @covers ::create
255
     * @covers ::createDataProvider
256
     * @expectedException \EasyDictionary\Exception\InvalidConfigurationException
257
     * @expectedExceptionMessage Class "stdClass" not found
258
     */
259
    public function testGetExceptionOfBadDictionaryClass()
260
    {
261
        $config = $this->createMock(ConfigInterface::class);
262
        $config->method('getDictionaryConfig')->willReturn([
263
            'test' => [
264
                'class' => \stdClass::class,
265
                'data' => [
266
                    'class' => Simple::class,
267
                ]
268
            ]
269
        ]);
270
271
        $manager = new Manager();
272
        $manager->setConfig($config);
273
        $manager->get('test');
274
    }
275
276
    /**
277
     * @covers \EasyDictionary\DataProvider\Simple
278
     * @covers \EasyDictionary\AbstractDictionary
279
     * @covers ::<public>
280
     * @covers ::create
281
     * @covers ::createDataProvider
282
     * @expectedException \EasyDictionary\Exception\InvalidConfigurationException
283
     * @expectedExceptionMessage Cache "no-cache" not found
284
     */
285
    public function testGetExceptionOfBadCache()
286
    {
287
        $config = $this->createMock(ConfigInterface::class);
288
        $config->method('getDictionaryConfig')->willReturn([
289
            'test' => [
290
                'cache' => "no-cache",
291
                'class' => SimpleDictionary::class,
292
                'data' => [
293
                    'class' => Simple::class,
294
                ]
295
            ]
296
        ]);
297
298
        $manager = new Manager();
299
        $manager->setConfig($config);
300
        $manager->get('test');
301
    }
302
}
303