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

RowTest::testRowWithDateEqualLogic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkQL\IntegrationTest\Query;
5
6
use TBolier\RethinkQL\Query\RowInterface;
7
use TBolier\RethinkQL\Response\ResponseInterface;
8
use TBolier\RethinkQL\Types\Term\TermType;
9
10
class RowTest extends AbstractTableTest
11
{
12
    /**
13
     * @throws \Exception
14
     */
15
    public function testRowWithDateGreaterThanLogic(): void
16
    {
17
        $this->insertDocumentWithDate(1, new \DateTime('-1 days'));
18
        $this->insertDocumentWithDate(2, new \DateTime('+1 days'));
19
20
        /** @var ResponseInterface $res */
21
        $dateTime = (new \DateTime('now'))->format(\DateTime::ATOM);
22
        $row = $this->r()->row('date')->gt($dateTime);
23
24
        $this->assertInstanceOf(RowInterface::class, $row);
25
        $this->assertArraySubset($this->equalsParsedQuery(TermType::GT, $dateTime), $row->toArray());
26
    }
27
28
    /**
29
     * @throws \Exception
30
     */
31
    public function testRowWithDateEqualLogic(): void
32
    {
33
        $this->insertDocumentWithDate(1, new \DateTime('-1 days'));
34
        $this->insertDocumentWithDate(2, new \DateTime('+1 days'));
35
36
        /** @var ResponseInterface $res */
37
        $dateTime = (new \DateTime('now'))->format(\DateTime::ATOM);
38
        $row = $this->r()->row('date')->eq($dateTime);
39
40
        $this->assertInstanceOf(RowInterface::class, $row);
41
        $this->assertArraySubset($this->equalsParsedQuery(TermType::EQ, $dateTime), $row->toArray());
42
    }
43
44
    /**
45
     * @throws \Exception
46
     */
47
    public function testRowWithDateNotEqualLogic(): void
48
    {
49
        $this->insertDocumentWithDate(1, new \DateTime('-1 days'));
50
        $this->insertDocumentWithDate(2, new \DateTime('+1 days'));
51
52
        /** @var ResponseInterface $res */
53
        $dateTime = (new \DateTime('now'))->format(\DateTime::ATOM);
54
        $row = $this->r()->row('date')->ne($dateTime);
55
56
        $this->assertInstanceOf(RowInterface::class, $row);
57
        $this->assertArraySubset($this->equalsParsedQuery(TermType::NE, $dateTime), $row->toArray());
58
    }
59
60
    /**
61
     * @throws \Exception
62
     */
63
    public function testRowWithDateMultipleAndLogic(): void
64
    {
65
        $yesterday = new \DateTime('-1 days');
66
        $tomorrow = new \DateTime('+1 days');
67
        $this->insertDocumentWithDate(1, $yesterday);
68
        $this->insertDocumentWithDate(2, $tomorrow);
69
70
        /** @var ResponseInterface $res */
71
        $row = $this->r()->row('date')->eq($yesterday->format(\DateTime::ATOM))->and(
72
            $this->r()->row('date')->ne($tomorrow->format(\DateTime::ATOM))->and(
73
                $this->r()->row('id')->gt(0)->and(
74
                    $this->r()->row('id')->lt(3)
75
                )
76
            )
77
        );
78
79
        $this->assertInstanceOf(RowInterface::class, $row);
80
        $this->assertArraySubset(
81
            [
82
                TermType::FUNC,
83
                [
84
                    [TermType::MAKE_ARRAY, [TermType::DATUM]],
85
                    [
86
                        TermType::AND,
87
                        [
88
                            [
89
                                TermType::EQ,
90
                                [
91
                                    [TermType::GET_FIELD, [[TermType::IMPLICIT_VAR, []], 'date']],
92
                                    $yesterday->format(\DateTime::ATOM),
93
                                ],
94
                            ],
95
                            [
96
                                TermType::AND,
97
                                [
98
                                    [
99
                                        TermType::NE,
100
                                        [
101
                                            [TermType::GET_FIELD, [[TermType::IMPLICIT_VAR, []], 'date']],
102
                                            $tomorrow->format(\DateTime::ATOM),
103
                                        ],
104
                                    ],
105
                                    [
106
                                        TermType::AND,
107
                                        [
108
                                            [
109
                                                TermType::GT,
110
                                                [
111
                                                    [TermType::GET_FIELD, [[TermType::IMPLICIT_VAR, []], 'id']],
112
                                                    0,
113
                                                ],
114
                                            ],
115
                                            [
116
                                                TermType::LT,
117
                                                [
118
                                                    [TermType::GET_FIELD, [[TermType::IMPLICIT_VAR, []], 'id']],
119
                                                    3,
120
                                                ],
121
                                            ],
122
                                        ],
123
                                    ],
124
                                ],
125
                            ],
126
                        ],
127
                    ],
128
                ],
129
            ],
130
            $row->toArray()
131
        );
132
    }
133
134
    /**
135
     * @throws \Exception
136
     */
137
    public function testRowWithDateMultipleOrLogic(): void
138
    {
139
        $yesterday = new \DateTime('-1 days');
140
        $tomorrow = new \DateTime('+1 days');
141
        $this->insertDocumentWithDate(1, $yesterday);
142
        $this->insertDocumentWithDate(2, $tomorrow);
143
144
        /** @var ResponseInterface $res */
145
        $row = $this->r()->row('date')->eq($yesterday->format(\DateTime::ATOM))->or(
146
            $this->r()->row('date')->ne($tomorrow->format(\DateTime::ATOM))->or(
147
                $this->r()->row('id')->lt(3)->or(
148
                    $this->r()->row('id')->gt(0)
149
                )
150
            )
151
        );
152
153
        $this->assertInstanceOf(RowInterface::class, $row);
154
        $this->assertArraySubset(
155
            [
156
                TermType::FUNC,
157
                [
158
                    [TermType::MAKE_ARRAY, [TermType::DATUM]],
159
                    [
160
                        TermType::OR,
161
                        [
162
                            [
163
                                TermType::EQ,
164
                                [
165
                                    [TermType::GET_FIELD, [[TermType::IMPLICIT_VAR, []], 'date']],
166
                                    $yesterday->format(\DateTime::ATOM),
167
                                ],
168
                            ],
169
                            [
170
                                TermType::OR,
171
                                [
172
                                    [
173
                                        TermType::NE,
174
                                        [
175
                                            [TermType::GET_FIELD, [[TermType::IMPLICIT_VAR, []], 'date']],
176
                                            $tomorrow->format(\DateTime::ATOM),
177
                                        ],
178
                                    ],
179
                                    [
180
                                        TermType::OR,
181
                                        [
182
                                            [
183
                                                TermType::LT,
184
                                                [
185
                                                    [TermType::GET_FIELD, [[TermType::IMPLICIT_VAR, []], 'id']],
186
                                                    3,
187
                                                ],
188
                                            ],
189
                                            [
190
                                                TermType::GT,
191
                                                [
192
                                                    [TermType::GET_FIELD, [[TermType::IMPLICIT_VAR, []], 'id']],
193
                                                    0,
194
                                                ],
195
                                            ],
196
                                        ],
197
                                    ],
198
                                ],
199
                            ],
200
                        ],
201
                    ],
202
                ],
203
            ],
204
            $row->toArray()
205
        );
206
    }
207
208
    /**
209
     * @param int $funcType
210
     * @param $dateTime
211
     * @return array
212
     */
213
    private function equalsParsedQuery(int $funcType, $dateTime): array
214
    {
215
        return [
216
            TermType::FUNC,
217
            [
218
                [
219
                    TermType::MAKE_ARRAY,
220
                    [
221
                        TermType::DATUM,
222
                    ],
223
                ],
224
                [
225
                    $funcType,
226
                    [
227
                        [
228
                            TermType::GET_FIELD,
229
                            [
230
                                [
231
                                    TermType::IMPLICIT_VAR,
232
                                    [],
233
                                ],
234
                                'date',
235
                            ],
236
                        ],
237
                        $dateTime,
238
                    ],
239
                ],
240
            ],
241
        ];
242
    }
243
}
244