ArrayProcessorTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 141
c 1
b 0
f 0
dl 0
loc 223
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testProcess() 0 18 1
B processProvider() 0 196 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lampager\Cake\Test\TestCase\Model;
6
7
use Cake\I18n\DateTime;
0 ignored issues
show
Bug introduced by
The type Cake\I18n\DateTime was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Cake\ORM\Entity;
9
use Cake\ORM\Table;
10
use Generator;
11
use Lampager\Cake\ArrayProcessor;
12
use Lampager\Cake\ORM\Query;
13
use Lampager\Cake\PaginationResult;
14
use Lampager\Cake\Paginator;
15
use Lampager\Cake\Test\TestCase\TestCase;
16
use PHPUnit\Framework\Attributes\DataProvider;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Attributes\DataProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use PHPUnit\Framework\MockObject\MockObject;
18
19
class ArrayProcessorTest extends TestCase
20
{
21
    /**
22
     * @param mixed[]  $options
23
     * @param ?mixed[] $cursor
24
     * @param Entity[] $rows
25
     */
26
    #[DataProvider('processProvider')]
27
    public function testProcess(array $options, ?array $cursor, array $rows, PaginationResult $expected): void
28
    {
29
        /** @var MockObject&Table $repository */
30
        $repository = $this->createMock(Table::class);
31
        $repository->method('getAlias')->willReturn('Posts');
32
33
        /** @var MockObject&Query $builder */
34
        $builder = $this->createMock(Query::class);
35
        $builder->method('getRepository')->willReturn($repository);
36
37
        $paginator = new Paginator($builder);
38
        $paginator->fromArray($options);
39
        $query = $paginator->configure($cursor);
40
41
        $processor = new ArrayProcessor();
42
        $actual = $processor->process($query, $rows);
43
        $this->assertEquals($expected, $actual);
44
    }
45
46
    public static function processProvider(): Generator
47
    {
48
        yield 'Option has prefix but entity does not have prefix' => [
49
            [
50
                'forward' => true,
51
                'seekable' => true,
52
                'limit' => 3,
53
                'orders' => [
54
                    ['Posts.modified', 'ASC'],
55
                    ['Posts.id', 'ASC'],
56
                ],
57
            ],
58
            [
59
                'Posts.id' => 3,
60
                'Posts.modified' => new DateTime('2017-01-01 10:00:00'),
61
            ],
62
            [
63
                new Entity([
64
                    'id' => 1,
65
                    'modified' => new DateTime('2017-01-01 10:00:00'),
66
                ]),
67
                new Entity([
68
                    'id' => 3,
69
                    'modified' => new DateTime('2017-01-01 10:00:00'),
70
                ]),
71
                new Entity([
72
                    'id' => 5,
73
                    'modified' => new DateTime('2017-01-01 10:00:00'),
74
                ]),
75
                new Entity([
76
                    'id' => 2,
77
                    'modified' => new DateTime('2017-01-01 11:00:00'),
78
                ]),
79
                new Entity([
80
                    'id' => 4,
81
                    'modified' => new DateTime('2017-01-01 11:00:00'),
82
                ]),
83
            ],
84
            new PaginationResult(
85
                [
86
                    new Entity([
87
                        'id' => 3,
88
                        'modified' => new DateTime('2017-01-01 10:00:00'),
89
                    ]),
90
                    new Entity([
91
                        'id' => 5,
92
                        'modified' => new DateTime('2017-01-01 10:00:00'),
93
                    ]),
94
                    new Entity([
95
                        'id' => 2,
96
                        'modified' => new DateTime('2017-01-01 11:00:00'),
97
                    ]),
98
                ], [
99
                    'hasPrevious' => true,
100
                    'previousCursor' => [
101
                        'Posts.id' => 1,
102
                        'Posts.modified' => new DateTime('2017-01-01 10:00:00'),
103
                    ],
104
                    'hasNext' => true,
105
                    'nextCursor' => [
106
                        'Posts.id' => 4,
107
                        'Posts.modified' => new DateTime('2017-01-01 11:00:00'),
108
                    ],
109
                    'limit' => 3,
110
                ]
111
            ),
112
        ];
113
114
        yield 'Option and entity both do not have prefix' => [
115
            [
116
                'forward' => true,
117
                'seekable' => true,
118
                'limit' => 3,
119
                'orders' => [
120
                    ['modified', 'ASC'],
121
                    ['id', 'ASC'],
122
                ],
123
            ],
124
            [
125
                'id' => 3,
126
                'modified' => new DateTime('2017-01-01 10:00:00'),
127
            ],
128
            [
129
                new Entity([
130
                    'id' => 1,
131
                    'modified' => new DateTime('2017-01-01 10:00:00'),
132
                ]),
133
                new Entity([
134
                    'id' => 3,
135
                    'modified' => new DateTime('2017-01-01 10:00:00'),
136
                ]),
137
                new Entity([
138
                    'id' => 5,
139
                    'modified' => new DateTime('2017-01-01 10:00:00'),
140
                ]),
141
                new Entity([
142
                    'id' => 2,
143
                    'modified' => new DateTime('2017-01-01 11:00:00'),
144
                ]),
145
                new Entity([
146
                    'id' => 4,
147
                    'modified' => new DateTime('2017-01-01 11:00:00'),
148
                ]),
149
            ],
150
            new PaginationResult(
151
                [
152
                    new Entity([
153
                        'id' => 3,
154
                        'modified' => new DateTime('2017-01-01 10:00:00'),
155
                    ]),
156
                    new Entity([
157
                        'id' => 5,
158
                        'modified' => new DateTime('2017-01-01 10:00:00'),
159
                    ]),
160
                    new Entity([
161
                        'id' => 2,
162
                        'modified' => new DateTime('2017-01-01 11:00:00'),
163
                    ]),
164
                ], [
165
                    'hasPrevious' => true,
166
                    'previousCursor' => [
167
                        'id' => 1,
168
                        'modified' => new DateTime('2017-01-01 10:00:00'),
169
                    ],
170
                    'hasNext' => true,
171
                    'nextCursor' => [
172
                        'id' => 4,
173
                        'modified' => new DateTime('2017-01-01 11:00:00'),
174
                    ],
175
                    'limit' => 3,
176
                ]
177
            ),
178
        ];
179
180
        yield 'Option with prefix and without prefix exist and entity does not have prefix' => [
181
            [
182
                'forward' => true,
183
                'seekable' => true,
184
                'limit' => 3,
185
                'orders' => [
186
                    ['Posts.modified', 'ASC'],
187
                    ['id', 'ASC'],
188
                ],
189
            ],
190
            [
191
                'id' => 3,
192
                'Posts.modified' => new DateTime('2017-01-01 10:00:00'),
193
            ],
194
            [
195
                new Entity([
196
                    'id' => 1,
197
                    'modified' => new DateTime('2017-01-01 10:00:00'),
198
                ]),
199
                new Entity([
200
                    'id' => 3,
201
                    'modified' => new DateTime('2017-01-01 10:00:00'),
202
                ]),
203
                new Entity([
204
                    'id' => 5,
205
                    'modified' => new DateTime('2017-01-01 10:00:00'),
206
                ]),
207
                new Entity([
208
                    'id' => 2,
209
                    'modified' => new DateTime('2017-01-01 11:00:00'),
210
                ]),
211
                new Entity([
212
                    'id' => 4,
213
                    'modified' => new DateTime('2017-01-01 11:00:00'),
214
                ]),
215
            ],
216
            new PaginationResult(
217
                [
218
                    new Entity([
219
                        'id' => 3,
220
                        'modified' => new DateTime('2017-01-01 10:00:00'),
221
                    ]),
222
                    new Entity([
223
                        'id' => 5,
224
                        'modified' => new DateTime('2017-01-01 10:00:00'),
225
                    ]),
226
                    new Entity([
227
                        'id' => 2,
228
                        'modified' => new DateTime('2017-01-01 11:00:00'),
229
                    ]),
230
                ], [
231
                    'hasPrevious' => true,
232
                    'previousCursor' => [
233
                        'id' => 1,
234
                        'Posts.modified' => new DateTime('2017-01-01 10:00:00'),
235
                    ],
236
                    'hasNext' => true,
237
                    'nextCursor' => [
238
                        'id' => 4,
239
                        'Posts.modified' => new DateTime('2017-01-01 11:00:00'),
240
                    ],
241
                    'limit' => 3,
242
                ]
243
            ),
244
        ];
245
    }
246
}
247