Passed
Push — master ( 24b8dd...d95a94 )
by noitran
01:52
created

EloquentProcessorTest::itShouldTestExprOr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Noitran\RQL\Tests\Processors;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Noitran\RQL\Expressions\AbstractExpr;
7
use Noitran\RQL\ExprQueue;
8
use Noitran\RQL\Processors\EloquentProcessor;
9
use Noitran\RQL\Tests\Stubs\Models\User;
10
use Noitran\RQL\Tests\TestCase;
11
12
/**
13
 * Class EloquentProcessorTest
14
 */
15
class EloquentProcessorTest extends TestCase
16
{
17
    /**
18
     * @var ExprQueue
19
     */
20
    protected $queue;
21
22
    /**
23
     * @var Builder
24
     */
25
    protected $builder;
26
27
    /**
28
     * Setup the test environment.
29
     */
30
    protected function setUp(): void
31
    {
32
        parent::setUp();
33
34
        $this->builder = User::query();
35
        $this->queue = new ExprQueue();
36
    }
37
38
    public function tearDown(): void
39
    {
40
        $this->builder = null;
41
        $this->queue = null;
42
    }
43
44
    /**
45
     * @param $expression
46
     * @param $column
47
     * @param $value
48
     *
49
     * @return AbstractExpr
50
     */
51
    protected function createExprClass($expression, $column, $value): AbstractExpr
52
    {
53
        $namespace = 'Noitran\RQL\Expressions\\';
54
        $exprClass = $namespace . $expression . 'Expr';
55
56
        return new $exprClass(null, $column, $value);
57
    }
58
59
    /**
60
     * @test
61
     *
62
     * @throws \Noitran\RQL\Exceptions\ExpressionException
63
     */
64
    public function itShouldTestExprBetween(): void
65
    {
66
        $exprClasses = $this->queue->enqueue(
67
            $this->createExprClass('between', 'id', '2,5')
68
        );
69
        $query = (new EloquentProcessor($this->builder))->process($exprClasses);
70
71
        $this->assertEquals(
72
            'select * from "users" where "id" between ? and ?',
73
            $query->toSql()
74
        );
75
    }
76
77
    /**
78
     * @test
79
     *
80
     * @throws \Noitran\RQL\Exceptions\ExpressionException
81
     */
82
    public function itShouldTestExprEq(): void
83
    {
84
        $exprClasses = $this->queue->enqueue(
85
            $this->createExprClass('eq', 'name', 'John')
86
        );
87
        $query = (new EloquentProcessor($this->builder))->process($exprClasses);
88
89
        $this->assertEquals(
90
            'select * from "users" where "name" = ?',
91
            $query->toSql()
92
        );
93
    }
94
95
    /**
96
     * @test
97
     *
98
     * @throws \Noitran\RQL\Exceptions\ExpressionException
99
     */
100
    public function itShouldTestExprGte(): void
101
    {
102
        $exprClasses = $this->queue->enqueue(
103
            $this->createExprClass('gte', 'updated_at', '2019-01-01 14:00:23')
104
        );
105
        $query = (new EloquentProcessor($this->builder))->process($exprClasses);
106
107
        $this->assertEquals(
108
            'select * from "users" where "updated_at" >= ?',
109
            $query->toSql()
110
        );
111
    }
112
113
    /**
114
     * @test
115
     *
116
     * @throws \Noitran\RQL\Exceptions\ExpressionException
117
     */
118
    public function itShouldTestExprGt(): void
119
    {
120
        $exprClasses = $this->queue->enqueue(
121
            $this->createExprClass('gt', 'updated_at', '2019-01-01 14:00:23')
122
        );
123
        $query = (new EloquentProcessor($this->builder))->process($exprClasses);
124
125
        $this->assertEquals(
126
            'select * from "users" where "updated_at" > ?',
127
            $query->toSql()
128
        );
129
    }
130
131
    /**
132
     * @test
133
     *
134
     * @throws \Noitran\RQL\Exceptions\ExpressionException
135
     */
136
    public function itShouldTestExprIn(): void
137
    {
138
        $exprClasses = $this->queue->enqueue(
139
            $this->createExprClass('in', 'id', '2,5,6,227')
140
        );
141
        $query = (new EloquentProcessor($this->builder))->process($exprClasses);
142
143
        $this->assertEquals(
144
            'select * from "users" where "id" in (?, ?, ?, ?)',
145
            $query->toSql()
146
        );
147
    }
148
149
    /**
150
     * @test
151
     *
152
     * @throws \Noitran\RQL\Exceptions\ExpressionException
153
     */
154
    public function itShouldTestExprLike(): void
155
    {
156
        $exprClasses = $this->queue->enqueue(
157
            $this->createExprClass('like', 'name', '%RandomName%')
158
        );
159
        $query = (new EloquentProcessor($this->builder))->process($exprClasses);
160
161
        $this->assertEquals(
162
            'select * from "users" where "name" like ?',
163
            $query->toSql()
164
        );
165
    }
166
167
    /**
168
     * @test
169
     *
170
     * @throws \Noitran\RQL\Exceptions\ExpressionException
171
     */
172
    public function itShouldTestExprLte(): void
173
    {
174
        $exprClasses = $this->queue->enqueue(
175
            $this->createExprClass('lte', 'updated_at', '2019-01-01 14:00:23')
176
        );
177
        $query = (new EloquentProcessor($this->builder))->process($exprClasses);
178
179
        $this->assertEquals(
180
            'select * from "users" where "updated_at" <= ?',
181
            $query->toSql()
182
        );
183
    }
184
185
    /**
186
     * @test
187
     *
188
     * @throws \Noitran\RQL\Exceptions\ExpressionException
189
     */
190
    public function itShouldTestExprLt(): void
191
    {
192
        $exprClasses = $this->queue->enqueue(
193
            $this->createExprClass('lt', 'updated_at', '2019-01-01 14:00:23')
194
        );
195
        $query = (new EloquentProcessor($this->builder))->process($exprClasses);
196
197
        $this->assertEquals(
198
            'select * from "users" where "updated_at" < ?',
199
            $query->toSql()
200
        );
201
    }
202
203
    /**
204
     * @test
205
     *
206
     * @throws \Noitran\RQL\Exceptions\ExpressionException
207
     */
208
    public function itShouldTestExprNotEq(): void
209
    {
210
        $exprClasses = $this->queue->enqueue(
211
            $this->createExprClass('notEq', 'name', 'John')
212
        );
213
        $query = (new EloquentProcessor($this->builder))->process($exprClasses);
214
215
        $this->assertEquals(
216
            'select * from "users" where "name" != ?',
217
            $query->toSql()
218
        );
219
    }
220
221
    /**
222
     * @test
223
     *
224
     * @throws \Noitran\RQL\Exceptions\ExpressionException
225
     */
226
    public function itShouldTestExprNotIn(): void
227
    {
228
        $exprClasses = $this->queue->enqueue(
229
            $this->createExprClass('notIn', 'id', '2,5,6,227')
230
        );
231
        $query = (new EloquentProcessor($this->builder))->process($exprClasses);
232
233
        $this->assertEquals(
234
            'select * from "users" where "id" not in (?, ?, ?, ?)',
235
            $query->toSql()
236
        );
237
    }
238
239
    /**
240
     * @test
241
     *
242
     * @throws \Noitran\RQL\Exceptions\ExpressionException
243
     */
244
    public function itShouldTestExprOr(): void
245
    {
246
        $exprClasses = $this->queue->enqueue(
247
            $this->createExprClass('or', 'id', '2|5|6')
248
        );
249
        $query = (new EloquentProcessor($this->builder))->process($exprClasses);
250
251
        $this->assertEquals(
252
            'select * from "users" where ("id" = ? or "id" = ? or "id" = ?)',
253
            $query->toSql()
254
        );
255
    }
256
}
257