Passed
Push — master ( 3bddf7...37f0e3 )
by Max
07:30
created

SimpleTest::testClassExistence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace EasyDictionary\Dictionary;
4
5
use EasyDictionary\Interfaces\DataProviderInterface;
6
use PHPUnit\Framework\TestCase;
7
use Psr\SimpleCache\CacheInterface;
8
use Psr\SimpleCache\InvalidArgumentException;
9
10
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
11
 * @coversDefaultClass \EasyDictionary\Dictionary\Simple
12
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
13
class SimpleTest extends TestCase
14
{
15
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
16
     * @covers \EasyDictionary\Dictionary\Simple
17
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
18
    public function testClassExistence()
19
    {
20
        self::assertTrue(class_exists('\EasyDictionary\Dictionary\Simple'));
21
    }
22
23
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
     * @covers ::__construct
25
     * @covers ::setName
26
     * @covers ::setDataProvider
27
     * @covers ::getDataProvider
28
     * @covers ::getCache
29
     * @covers ::getData
30
     * @covers ::loadData
31
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
32
    public function testGetDataWithDataProvider()
33
    {
34
        $data = [0, 1, 2, 3];
35
        $dataProviderMock = self::createMock(DataProviderInterface::class);
36
        $dataProviderMock->method('getData')->willReturn($data);
37
        $dataProviderMock->expects(self::once())->method('getData');
38
39
        $dictionary = new Simple();
40
        $dictionary->setDataProvider($dataProviderMock);
0 ignored issues
show
Documentation introduced by
$dataProviderMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<EasyDictionary\In...\DataProviderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
        self::assertEquals($data, $dictionary->getData());
42
    }
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @covers ::__construct
46
     * @covers ::setName
47
     * @covers ::getDataProvider
48
     * @covers ::getCache
49
     * @covers ::getData
50
     * @covers ::loadData
51
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
52
    public function testGetDataWithoutDataProvider()
53
    {
54
        $dictionary = new Simple();
55
        self::assertEquals([], $dictionary->getData());
56
    }
57
58
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
59
     * @covers ::__construct
60
     * @covers ::setName
61
     * @covers ::getName
62
     * @covers ::setCache
63
     * @covers ::setDataProvider
64
     * @covers ::getDataProvider
65
     * @covers ::getCache
66
     * @covers ::getData
67
     * @covers ::loadData
68
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69 View Code Duplication
    public function testSaveDataToCache()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $data = [0, 1, 2, 3];
72
        $dataProviderMock = self::createMock(DataProviderInterface::class);
73
        $dataProviderMock->method('getData')->willReturn($data);
74
        $dataProviderMock->expects(self::once())->method('getData');
75
76
        $cacheMock = self::createMock(CacheInterface::class);
77
        $cacheMock->expects(self::once())->method('get');
78
        $cacheMock->expects(self::once())->method('set')->with(Simple::class . '_test', $data, 3600);
79
80
        $dictionary = new Simple('test');
81
        $dictionary->setDataProvider($dataProviderMock);
0 ignored issues
show
Documentation introduced by
$dataProviderMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<EasyDictionary\In...\DataProviderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
        $dictionary->setCache($cacheMock);
0 ignored issues
show
Documentation introduced by
$cacheMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\SimpleCache\CacheInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
84
        self::assertEquals($data, $dictionary->getData());
85
    }
86
87
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
88
     * @covers ::__construct
89
     * @covers ::setName
90
     * @covers ::getName
91
     * @covers ::setCache
92
     * @covers ::setDataProvider
93
     * @covers ::getDataProvider
94
     * @covers ::getCache
95
     * @covers ::getData
96
     * @covers ::loadData
97
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
98 View Code Duplication
    public function testSaveDataToCacheThrowError()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        $data = [0, 1, 2, 3];
101
        $dataProviderMock = self::createMock(DataProviderInterface::class);
102
        $dataProviderMock->method('getData')->willReturn($data);
103
        $dataProviderMock->expects(self::once())->method('getData');
104
105
        $cacheMock = self::createMock(CacheInterface::class);
106
        $cacheMock->expects(self::once())->method('get');
107
        $cacheMock->expects(self::once())->method('set')
108
            ->with(Simple::class . '_test', $data, 3600)
109
            ->willThrowException(new InvalidArgException);
110
111
        $dictionary = new Simple('test');
112
        $dictionary->setDataProvider($dataProviderMock);
0 ignored issues
show
Documentation introduced by
$dataProviderMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<EasyDictionary\In...\DataProviderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113
        $dictionary->setCache($cacheMock);
0 ignored issues
show
Documentation introduced by
$cacheMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\SimpleCache\CacheInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
115
        self::assertEquals([], $dictionary->getData());
116
    }
117
118
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
119
     * @covers ::__construct
120
     * @covers ::setName
121
     * @covers ::count
122
     * @covers ::setDataProvider
123
     * @covers ::getDataProvider
124
     * @covers ::getCache
125
     * @covers ::getData
126
     * @covers ::loadData
127
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
128
    public function testDataCount()
129
    {
130
        $data = [0, 1, 2, 3];
131
        $dataProviderMock = self::createMock(DataProviderInterface::class);
132
        $dataProviderMock->method('getData')->willReturn($data);
133
        $dataProviderMock->expects(self::once())->method('getData');
134
135
        $dictionary = new Simple('test');
136
        $dictionary->setDataProvider($dataProviderMock);
0 ignored issues
show
Documentation introduced by
$dataProviderMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<EasyDictionary\In...\DataProviderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
137
138
        self::assertTrue($dictionary instanceof \Countable);
139
        self::assertEquals(4, count($dictionary));
140
    }
141
142
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
143
     * @covers ::__construct
144
     * @covers ::setName
145
     * @covers ::getCache
146
     * @covers ::getData
147
     * @covers ::getDataProvider
148
     * @covers ::getDefaultView
149
     * @covers ::getIterator
150
     * @covers ::setDataProvider
151
     * @covers ::loadData
152
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
153 View Code Duplication
    public function testDataIteratorWithoutView()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        $data = ['a' => 0, 'b' => 1, 'c' => 2];
156
        $dataProviderMock = self::createMock(DataProviderInterface::class);
157
        $dataProviderMock->method('getData')->willReturn($data);
158
        $dataProviderMock->expects(self::once())->method('getData');
159
160
        $dictionary = new Simple('test');
161
        $dictionary->setDataProvider($dataProviderMock);
0 ignored issues
show
Documentation introduced by
$dataProviderMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<EasyDictionary\In...\DataProviderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
162
163
        $loadedData = [];
164
        foreach ($dictionary as $key => $value) {
165
            $loadedData[$key] = $value;
166
        }
167
168
        self::assertEquals($data, $loadedData);
169
    }
170
171
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
172
     * @covers ::__construct
173
     * @covers ::setName
174
     * @covers ::getCache
175
     * @covers ::getData
176
     * @covers ::getDataProvider
177
     * @covers ::setDataProvider
178
     * @covers ::getDefaultView
179
     * @covers ::setDefaultView
180
     * @covers ::getIterator
181
     * @covers ::loadData
182
     * @covers ::withView
183
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
184
    public function testDataIteratorWithGoodView()
185
    {
186
        $data = ['a' => 0, 'b' => 1, 'c' => 2];
187
        $dataProviderMock = self::createMock(DataProviderInterface::class);
188
        $dataProviderMock->method('getData')->willReturn($data);
189
        $dataProviderMock->expects(self::once())->method('getData');
190
191
        $dictionary = new Simple('test');
192
        $dictionary->setDataProvider($dataProviderMock);
0 ignored issues
show
Documentation introduced by
$dataProviderMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<EasyDictionary\In...\DataProviderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
193
        $dictionary->setDefaultView(function ($rows) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
194
            foreach ($rows as $key => $row) {
195
                yield $key . ' ' . $row;
196
            }
197
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
198
199
        $loadedData = [];
200
        foreach ($dictionary as $value) {
201
            $loadedData[] = $value;
202
        }
203
204
        self::assertEquals(['a 0', 'b 1', 'c 2'], $loadedData);
205
    }
206
207
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
208
     * @covers ::__construct
209
     * @covers ::setName
210
     * @covers ::getCache
211
     * @covers ::getData
212
     * @covers ::getDataProvider
213
     * @covers ::setDataProvider
214
     * @covers ::getDefaultView
215
     * @covers ::getIterator
216
     * @covers ::loadData
217
     * @covers ::withView
218
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
219 View Code Duplication
    public function testDataIteratorWithBadView()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
    {
221
        $data = ['a' => 0, 'b' => 1, 'c' => 2];
222
        $dataProviderMock = self::createMock(DataProviderInterface::class);
223
        $dataProviderMock->method('getData')->willReturn($data);
224
        $dataProviderMock->expects(self::once())->method('getData');
225
226
        $dictionary = new Simple('test');
227
        $dictionary->setDataProvider($dataProviderMock);
0 ignored issues
show
Documentation introduced by
$dataProviderMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<EasyDictionary\In...\DataProviderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
228
229
        $loadedData = [];
230
        foreach ($dictionary->withView(null) as $key => $value) {
231
            $loadedData[$key] = $value;
232
        }
233
234
        self::assertEquals($data, $loadedData);
235
    }
236
237
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
238
     * @covers ::__construct
239
     * @covers ::setName
240
     * @covers ::getCache
241
     * @covers ::getData
242
     * @covers ::getDataProvider
243
     * @covers ::setDataProvider
244
     * @covers ::getDefaultView
245
     * @covers ::getIterator
246
     * @covers ::loadData
247
     * @covers ::setSearchFields
248
     * @covers ::search
249
     * @covers ::getSearchFields
250
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
251
    public function testDataIteratorSearchInFlatData()
252
    {
253
        $data = ['a' => 0, 'b' => 1, 'c' => 2];
254
        $dataProviderMock = self::createMock(DataProviderInterface::class);
255
        $dataProviderMock->method('getData')->willReturn($data);
256
        $dataProviderMock->expects(self::once())->method('getData');
257
258
        $dictionary = new Simple('test');
259
        $dictionary->setDataProvider($dataProviderMock);
0 ignored issues
show
Documentation introduced by
$dataProviderMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<EasyDictionary\In...\DataProviderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
260
261
        $loadedData = [];
262
        foreach ($dictionary->search('/a|c/') as $key => $value) {
263
            $loadedData[$key] = $value;
264
        }
265
266
        self::assertEquals(['a' => 0, 'c' => 2], $loadedData);
267
    }
268
269
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
270
     * @covers ::__construct
271
     * @covers ::setName
272
     * @covers ::getCache
273
     * @covers ::getData
274
     * @covers ::getDataProvider
275
     * @covers ::setDataProvider
276
     * @covers ::getDefaultView
277
     * @covers ::getIterator
278
     * @covers ::loadData
279
     * @covers ::setSearchFields
280
     * @covers ::search
281
     * @covers ::setSearchFields
282
     * @covers ::getSearchFields
283
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
284
    public function testDataIteratorSearchInArrayData()
285
    {
286
        $data = ['a' => ['code' => 33, 'code2' => 44], 'b' => ['code' => 44]];
287
        $dataProviderMock = self::createMock(DataProviderInterface::class);
288
        $dataProviderMock->method('getData')->willReturn($data);
289
        $dataProviderMock->expects(self::once())->method('getData');
290
291
        $dictionary = new Simple('test');
292
        $dictionary->setDataProvider($dataProviderMock);
0 ignored issues
show
Documentation introduced by
$dataProviderMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<EasyDictionary\In...\DataProviderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
293
        $dictionary->setSearchFields([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
294
            'code' => 1,
295
            'code2' => 0
296
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
297
298
        self::assertEquals(['b' => ['code' => 44]], $dictionary->search('/44/'));
299
    }
300
}
301
302
class InvalidArgException extends \Exception implements InvalidArgumentException
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class InvalidArgException
Loading history...
303
{
304
305
}
306