Passed
Push — master ( 139da0...112cee )
by Michel
03:17
created

FilterTest::testFilterWithDateMultipleAndLogic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkQL\IntegrationTest\Query;
5
6
use TBolier\RethinkQL\Response\Cursor;
7
use TBolier\RethinkQL\Response\ResponseInterface;
8
9
class FilterTest extends AbstractTableTest
10
{
11
    /**
12
     * @throws \Exception
13
     */
14
    public function testFilter()
15
    {
16
        $this->insertDocument(1);
17
18
        /** @var Cursor $cursor */
19
        $cursor = $this->r()
20
            ->table('tabletest')
21
            ->filter(['title' => 'Test document 1'])
22
            ->run();
23
24
        /** @var array $array */
25
        $array = $cursor->current();
26
27
        $this->assertInstanceOf(\Iterator::class, $cursor);
28
        $this->assertArraySubset(['title' => 'Test document 1'], $array);
29
    }
30
31
    /**
32
     * @throws \Exception
33
     */
34
    public function testFilterOnMultipleDocuments()
35
    {
36
        $this->insertDocument(1);
37
        $this->insertDocument(2);
38
        $this->insertDocument(3);
39
        $this->insertDocument(4);
40
        $this->insertDocument(5);
41
42
        /** @var Cursor $cursor */
43
        $cursor = $this->r()
44
            ->table('tabletest')
45
            ->filter(['title' => 'Test document 1'])
46
            ->run();
47
48
        $this->assertCount(1, $cursor);
49
    }
50
51
    /**
52
     * @return void
53
     * @throws \Exception
54
     */
55
    public function testFilterAndIsEmpty(): void
56
    {
57
        $this->insertDocument(1);
58
        $this->insertDocument('stringId');
59
60
        /** @var ResponseInterface $res */
61
        $res = $this->r()
62
            ->table('tabletest')
63
            ->filter(['id' => 1])
64
            ->isEmpty()
65
            ->run();
66
67
        $this->assertFalse($res->getData());
68
    }
69
70
    /**
71
     * @return void
72
     * @throws \Exception
73
     */
74
    public function testFilterAndCount(): void
75
    {
76
        $this->insertDocument(1);
77
        $this->insertDocument('stringId');
78
79
        /** @var ResponseInterface $res */
80
        $res = $this->r()
81
            ->table('tabletest')
82
            ->filter(['description' => 'A document description.'])
83
            ->count()
84
            ->run();
85
86
        $this->assertEquals(2, $res->getData());
87
    }
88
89
    /**
90
     * @return void
91
     * @throws \Exception
92
     */
93
    public function testFilterAndAvg(): void
94
    {
95
        $this->insertDocumentWithNumber(1, 50);
96
        $this->insertDocumentWithNumber(2, 100);
97
98
        /** @var ResponseInterface $res */
99
        $res = $this->r()
100
            ->table('tabletest')
101
            ->filter(['description' => 'A document description.'])
102
            ->avg('number')
103
            ->run();
104
105
        $this->assertEquals(75, $res->getData());
106
    }
107
108
    /**
109
     * @return void
110
     * @throws \Exception
111
     */
112
    public function testFilterAndSum(): void
113
    {
114
        $this->insertDocumentWithNumber(1, 50);
115
        $this->insertDocumentWithNumber(2, 100);
116
117
        /** @var ResponseInterface $res */
118
        $res = $this->r()
119
            ->table('tabletest')
120
            ->filter(['description' => 'A document description.'])
121
            ->sum('number')
122
            ->run();
123
124
        $this->assertEquals(150, $res->getData());
125
    }
126
127
    /**
128
     * @return void
129
     * @throws \Exception
130
     */
131
    public function testFilterAndLimit(): void
132
    {
133
        $this->insertDocument(1);
134
        $this->insertDocument('stringId');
135
136
        /** @var ResponseInterface $res */
137
        $cursor = $this->r()
138
            ->table('tabletest')
139
            ->filter(['description' => 'A document description.'])
140
            ->limit(1)
141
            ->run();
142
143
        $this->assertCount(1, $cursor);
144
    }
145
146
    /**
147
     * @return void
148
     * @throws \Exception
149
     */
150
    public function testFilterAndSkip(): void
151
    {
152
        $this->insertDocument(1);
153
        $this->insertDocument('stringId');
154
155
        /** @var ResponseInterface $res */
156
        $cursor = $this->r()
157
            ->table('tabletest')
158
            ->filter(['description' => 'A document description.'])
159
            ->skip(1)
160
            ->run();
161
162
        $this->assertCount(1, $cursor);
163
    }
164
165
    /**
166
     * @return void
167
     * @throws \Exception
168
     */
169
    public function testFilterAndOrderBy(): void
170
    {
171
        $this->insertDocument(1);
172
        $this->insertDocument('stringId');
173
174
        /** @var ResponseInterface $res */
175
        $res = $this->r()
176
            ->table('tabletest')
177
            ->filter(['description' => 'A document description.'])
178
            ->orderBy($this->r()->desc('id'))
179
            ->run();
180
181
        $this->assertArraySubset(['id' => 'stringId'], $res->getData()[0]);
182
    }
183
184
    /**
185
     * @return void
186
     * @throws \Exception
187
     */
188
    public function testFilterAndMin(): void
189
    {
190
        $this->insertDocumentWithNumber(1, 77);
191
        $this->insertDocumentWithNumber(2, 99);
192
193
        /** @var ResponseInterface $res */
194
        $res = $this->r()
195
            ->table('tabletest')
196
            ->filter(['description' => 'A document description.'])
197
            ->min('number')
198
            ->run();
199
200
        $this->assertArraySubset(['number' => 77], $res->getData());
1 ignored issue
show
Bug introduced by
It seems like $res->getData() can also be of type string; however, parameter $array of PHPUnit\Framework\Assert::assertArraySubset() does only seem to accept ArrayAccess|array, maybe add an additional type check? ( Ignorable by Annotation )

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

200
        $this->assertArraySubset(['number' => 77], /** @scrutinizer ignore-type */ $res->getData());
Loading history...
201
    }
202
203
    /**
204
     * @return void
205
     * @throws \Exception
206
     */
207
    public function testFilterAndMax(): void
208
    {
209
        $this->insertDocumentWithNumber(1, 77);
210
        $this->insertDocumentWithNumber(2, 99);
211
212
        /** @var ResponseInterface $res */
213
        $res = $this->r()
214
            ->table('tabletest')
215
            ->filter(['description' => 'A document description.'])
216
            ->max('number')
217
            ->run();
218
219
        $this->assertArraySubset(['number' => 99], $res->getData());
1 ignored issue
show
Bug introduced by
It seems like $res->getData() can also be of type string; however, parameter $array of PHPUnit\Framework\Assert::assertArraySubset() does only seem to accept ArrayAccess|array, maybe add an additional type check? ( Ignorable by Annotation )

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

219
        $this->assertArraySubset(['number' => 99], /** @scrutinizer ignore-type */ $res->getData());
Loading history...
220
    }
221
222
    /**
223
     * @throws \Exception
224
     */
225
    public function testFilterWithDateGreaterThanLogic(): void
226
    {
227
        $this->insertDocumentWithDate(1, new \DateTime('-1 days'));
228
        $this->insertDocumentWithDate(2, new \DateTime('+1 days'));
229
230
        /** @var ResponseInterface $res */
231
        $row = $this->r()->row('date')->gt((new \DateTime('now'))->format(\DateTime::ATOM));
232
        $cursor = $this->r()
233
            ->table('tabletest')
234
            ->filter($row)
235
            ->run();
236
237
        $this->assertCount(1, $cursor);
238
    }
239
240
    /**
241
     * @throws \Exception
242
     */
243
    public function testFilterWithDateLowerThanLogic(): void
244
    {
245
        $this->insertDocumentWithDate(1, new \DateTime('-1 days'));
246
        $this->insertDocumentWithDate(2, new \DateTime('+1 days'));
247
248
        /** @var ResponseInterface $res */
249
        $row = $this->r()->row('date')->lt((new \DateTime('now'))->format(\DateTime::ATOM));
250
        $cursor = $this->r()
251
            ->table('tabletest')
252
            ->filter($row)
253
            ->run();
254
255
        $this->assertCount(1, $cursor);
256
    }
257
258
    /**
259
     * @throws \Exception
260
     */
261
    public function testFilterWithDateEqualLogic(): void
262
    {
263
        $this->insertDocumentWithDate(1, $yesterday = new \DateTime('-1 days'));
264
        $this->insertDocumentWithDate(2, new \DateTime('+1 days'));
265
266
        /** @var ResponseInterface $res */
267
        $row = $this->r()->row('date')->eq($yesterday->format(\DateTime::ATOM));
268
        $cursor = $this->r()
269
            ->table('tabletest')
270
            ->filter($row)
271
            ->run();
272
273
        $this->assertCount(1, $cursor);
274
    }
275
276
    /**
277
     * @throws \Exception
278
     */
279
    public function testFilterWithDateNotEqualLogic(): void
280
    {
281
        $this->insertDocumentWithDate(1, $yesterday = new \DateTime('-1 days'));
282
        $this->insertDocumentWithDate(2, new \DateTime('+1 days'));
283
284
        $row = $this->r()->row('date')->ne($yesterday->format(\DateTime::ATOM));
285
        $cursor = $this->r()
286
            ->table('tabletest')
287
            ->filter($row)
288
            ->run();
289
290
        $this->assertCount(1, $cursor);
291
    }
292
293
    /**
294
     * @throws \Exception
295
     */
296
    public function testFilterWithDateMultipleAndLogic(): void
297
    {
298
        $this->insertDocumentWithDate(1, $yesterday = new \DateTime('-1 days'));
299
        $this->insertDocumentWithDate(2, $tomorrow = new \DateTime('+1 days'));
300
301
        /** @var ResponseInterface $res */
302
        $row = $this->r()->row('date')->eq($yesterday->format(\DateTime::ATOM))->and(
303
            $this->r()->row('date')->ne($tomorrow->format(\DateTime::ATOM))->and(
304
                $this->r()->row('id')->eq(1)->and(
305
                    $this->r()->row('id')->ne(2)
306
                )
307
            )
308
        );
309
310
        $cursor = $this->r()
311
            ->table('tabletest')
312
            ->filter($row)
313
            ->run();
314
315
        $this->assertCount(1, $cursor);
316
    }
317
318
    /**
319
     * @throws \Exception
320
     */
321
    public function testFilterWithDateMultipleOrLogic(): void
322
    {
323
        $this->insertDocumentWithDate(1, $yesterday = new \DateTime('-1 days'));
324
        $this->insertDocumentWithDate(2, $tomorrow = new \DateTime('+1 days'));
325
326
        /** @var ResponseInterface $res */
327
        $row = $this->r()->row('date')->eq($yesterday->format(\DateTime::ATOM))->or(
328
            $this->r()->row('date')->eq($tomorrow->format(\DateTime::ATOM))->and(
329
                $this->r()->row('id')->gt(0)->or(
330
                    $this->r()->row('id')->lt(3)
331
                )
332
            )
333
        );
334
335
        $cursor = $this->r()
336
            ->table('tabletest')
337
            ->filter($row)
338
            ->run();
339
340
        $this->assertCount(2, $cursor);
341
    }
342
}
343