Passed
Pull Request — master (#36)
by Timon
03:18
created

FilterTest::testFilterWithDateGreaterThanLogic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkQL\IntegrationTest\Query;
5
6
use TBolier\RethinkQL\Response\ResponseInterface;
7
8
class FilterTest extends AbstractTableTest
9
{
10
    /**
11
     * @throws \Exception
12
     */
13
    public function testFilter()
14
    {
15
        $this->insertDocument(1);
16
17
        /** @var Cursor $cursor */
18
        $cursor = $this->r()
19
            ->table('tabletest')
20
            ->filter(['title' => 'Test document 1'])
21
            ->run();
22
23
        /** @var array $array */
24
        $array = $cursor->current();
25
26
        $this->assertInstanceOf(\Iterator::class, $cursor);
27
        $this->assertArraySubset(['title' => 'Test document 1'], $array);
28
    }
29
30
    /**
31
     * @throws \Exception
32
     */
33
    public function testFilterOnMultipleDocuments()
34
    {
35
        $this->insertDocument(1);
36
        $this->insertDocument(2);
37
        $this->insertDocument(3);
38
        $this->insertDocument(4);
39
        $this->insertDocument(5);
40
41
        /** @var Cursor $cursor */
42
        $cursor = $this->r()
43
            ->table('tabletest')
44
            ->filter(['title' => 'Test document 1'])
45
            ->run();
46
47
        $this->assertCount(1, $cursor);
48
    }
49
50
    /**
51
     * @return void
52
     * @throws \Exception
53
     */
54
    public function testFilterAndIsEmpty(): void
55
    {
56
        $this->insertDocument(1);
57
        $this->insertDocument('stringId');
58
59
        /** @var ResponseInterface $res */
60
        $res = $this->r()
61
            ->table('tabletest')
62
            ->filter(['id' => 1])
63
            ->isEmpty()
64
            ->run();
65
66
        $this->assertFalse($res->getData());
67
    }
68
69
    /**
70
     * @return void
71
     * @throws \Exception
72
     */
73
    public function testFilterAndCount(): void
74
    {
75
        $this->insertDocument(1);
76
        $this->insertDocument('stringId');
77
78
        /** @var ResponseInterface $res */
79
        $res = $this->r()
80
            ->table('tabletest')
81
            ->filter(['description' => 'A document description.'])
82
            ->count()
83
            ->run();
84
85
        $this->assertEquals(2, $res->getData());
86
    }
87
88
    /**
89
     * @return void
90
     * @throws \Exception
91
     */
92
    public function testFilterAndAvg(): void
93
    {
94
        $this->insertDocumentWithNumber(1, 50);
95
        $this->insertDocumentWithNumber(2, 100);
96
97
        /** @var ResponseInterface $res */
98
        $res = $this->r()
99
            ->table('tabletest')
100
            ->filter(['description' => 'A document description.'])
101
            ->avg('number')
102
            ->run();
103
104
        $this->assertEquals(75, $res->getData());
105
    }
106
107
    /**
108
     * @return void
109
     * @throws \Exception
110
     */
111
    public function testFilterAndSum(): void
112
    {
113
        $this->insertDocumentWithNumber(1, 50);
114
        $this->insertDocumentWithNumber(2, 100);
115
116
        /** @var ResponseInterface $res */
117
        $res = $this->r()
118
            ->table('tabletest')
119
            ->filter(['description' => 'A document description.'])
120
            ->sum('number')
121
            ->run();
122
123
        $this->assertEquals(150, $res->getData());
124
    }
125
126
    /**
127
     * @return void
128
     * @throws \Exception
129
     */
130
    public function testFilterAndLimit(): void
131
    {
132
        $this->insertDocument(1);
133
        $this->insertDocument('stringId');
134
135
        /** @var ResponseInterface $res */
136
        $cursor = $this->r()
137
            ->table('tabletest')
138
            ->filter(['description' => 'A document description.'])
139
            ->limit(1)
140
            ->run();
141
142
        $this->assertCount(1, $cursor);
1 ignored issue
show
Bug introduced by
It seems like $cursor can also be of type TBolier\RethinkQL\Response\ResponseInterface; however, parameter $haystack of PHPUnit\Framework\Assert::assertCount() does only seem to accept Countable|iterable, 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

142
        $this->assertCount(1, /** @scrutinizer ignore-type */ $cursor);
Loading history...
143
    }
144
145
    /**
146
     * @return void
147
     * @throws \Exception
148
     */
149
    public function testFilterAndSkip(): void
150
    {
151
        $this->insertDocument(1);
152
        $this->insertDocument('stringId');
153
154
        /** @var ResponseInterface $res */
155
        $cursor = $this->r()
156
            ->table('tabletest')
157
            ->filter(['description' => 'A document description.'])
158
            ->skip(1)
159
            ->run();
160
161
        $this->assertCount(1, $cursor);
1 ignored issue
show
Bug introduced by
It seems like $cursor can also be of type TBolier\RethinkQL\Response\ResponseInterface; however, parameter $haystack of PHPUnit\Framework\Assert::assertCount() does only seem to accept Countable|iterable, 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

161
        $this->assertCount(1, /** @scrutinizer ignore-type */ $cursor);
Loading history...
162
    }
163
164
    /**
165
     * @return void
166
     * @throws \Exception
167
     */
168
    public function testFilterAndOrderBy(): void
169
    {
170
        $this->insertDocument(1);
171
        $this->insertDocument('stringId');
172
173
        /** @var ResponseInterface $res */
174
        $res = $this->r()
175
            ->table('tabletest')
176
            ->filter(['description' => 'A document description.'])
177
            ->orderBy($this->r()->desc('id'))
178
            ->run();
179
180
        $this->assertArraySubset(['id' => 'stringId'], $res->getData()[0]);
181
    }
182
183
    /**
184
     * @return void
185
     * @throws \Exception
186
     */
187
    public function testFilterAndMin(): void
188
    {
189
        $this->insertDocumentWithNumber(1, 77);
190
        $this->insertDocumentWithNumber(2, 99);
191
192
        /** @var ResponseInterface $res */
193
        $res = $this->r()
194
            ->table('tabletest')
195
            ->filter(['description' => 'A document description.'])
196
            ->min('number')
197
            ->run();
198
199
        $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

199
        $this->assertArraySubset(['number' => 77], /** @scrutinizer ignore-type */ $res->getData());
Loading history...
200
    }
201
202
    /**
203
     * @return void
204
     * @throws \Exception
205
     */
206
    public function testFilterAndMax(): void
207
    {
208
        $this->insertDocumentWithNumber(1, 77);
209
        $this->insertDocumentWithNumber(2, 99);
210
211
        /** @var ResponseInterface $res */
212
        $res = $this->r()
213
            ->table('tabletest')
214
            ->filter(['description' => 'A document description.'])
215
            ->max('number')
216
            ->run();
217
218
        $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

218
        $this->assertArraySubset(['number' => 99], /** @scrutinizer ignore-type */ $res->getData());
Loading history...
219
    }
220
221
    /**
222
     * @throws \Exception
223
     */
224
    public function testFilterWithDateGreaterThanLogic(): void
225
    {
226
        $this->insertDocumentWithDate(1, new \DateTime('-1 days'));
227
        $this->insertDocumentWithDate(2, new \DateTime('+1 days'));
228
229
        /** @var ResponseInterface $res */
230
        $row = $this->r()->row('date')->gt((new \DateTime('now'))->format(\DateTime::ATOM));
231
        $cursor = $this->r()
232
            ->table('tabletest')
233
            ->filter($row)
234
            ->run();
235
236
        $this->assertCount(1, $cursor);
1 ignored issue
show
Bug introduced by
It seems like $cursor can also be of type TBolier\RethinkQL\Response\ResponseInterface; however, parameter $haystack of PHPUnit\Framework\Assert::assertCount() does only seem to accept Countable|iterable, 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

236
        $this->assertCount(1, /** @scrutinizer ignore-type */ $cursor);
Loading history...
237
    }
238
239
    /**
240
     * @throws \Exception
241
     */
242
    public function testFilterWithDateLowerThanLogic(): void
243
    {
244
        $this->insertDocumentWithDate(1, new \DateTime('-1 days'));
245
        $this->insertDocumentWithDate(2, new \DateTime('+1 days'));
246
247
        /** @var ResponseInterface $res */
248
        $row = $this->r()->row('date')->lt((new \DateTime('now'))->format(\DateTime::ATOM));
249
        $cursor = $this->r()
250
            ->table('tabletest')
251
            ->filter($row)
252
            ->run();
253
254
        $this->assertCount(1, $cursor);
1 ignored issue
show
Bug introduced by
It seems like $cursor can also be of type TBolier\RethinkQL\Response\ResponseInterface; however, parameter $haystack of PHPUnit\Framework\Assert::assertCount() does only seem to accept Countable|iterable, 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

254
        $this->assertCount(1, /** @scrutinizer ignore-type */ $cursor);
Loading history...
255
    }
256
257
    /**
258
     * @throws \Exception
259
     */
260
    public function testFilterWithDateEqualLogic(): void
261
    {
262
        $this->insertDocumentWithDate(1, $equalDateTime = new \DateTime('-1 days'));
263
        $this->insertDocumentWithDate(2, new \DateTime('+1 days'));
264
265
        /** @var ResponseInterface $res */
266
        $row = $this->r()->row('date')->eq($equalDateTime->format(\DateTime::ATOM));
267
        $cursor = $this->r()
268
            ->table('tabletest')
269
            ->filter($row)
270
            ->run();
271
272
        $this->assertCount(1, $cursor);
1 ignored issue
show
Bug introduced by
It seems like $cursor can also be of type TBolier\RethinkQL\Response\ResponseInterface; however, parameter $haystack of PHPUnit\Framework\Assert::assertCount() does only seem to accept Countable|iterable, 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

272
        $this->assertCount(1, /** @scrutinizer ignore-type */ $cursor);
Loading history...
273
    }
274
275
    /**
276
     * @throws \Exception
277
     */
278
    public function testFilterWithDateNotEqualLogic(): void
279
    {
280
        $this->insertDocumentWithDate(1, $notEqualDateTime = new \DateTime('-1 days'));
281
        $this->insertDocumentWithDate(2, new \DateTime('+1 days'));
282
283
        /** @var ResponseInterface $res */
284
        $row = $this->r()->row('date')->ne($notEqualDateTime->format(\DateTime::ATOM));
285
        $cursor = $this->r()
286
            ->table('tabletest')
287
            ->filter($row)
288
            ->run();
289
290
        $this->assertCount(1, $cursor);
1 ignored issue
show
Bug introduced by
It seems like $cursor can also be of type TBolier\RethinkQL\Response\ResponseInterface; however, parameter $haystack of PHPUnit\Framework\Assert::assertCount() does only seem to accept Countable|iterable, 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

290
        $this->assertCount(1, /** @scrutinizer ignore-type */ $cursor);
Loading history...
291
    }
292
}
293