CriteriaTests::atLeastTest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Cubiche\Core\Specification\Tests\Units;
11
12
use Cubiche\Core\Selector\Callback;
13
use Cubiche\Core\Selector\Count;
14
use Cubiche\Core\Selector\Key;
15
use Cubiche\Core\Selector\Method;
16
use Cubiche\Core\Selector\Property;
17
use Cubiche\Core\Selector\This;
18
use Cubiche\Core\Selector\Value;
19
use Cubiche\Core\Specification\Constraint\BinaryConstraintOperator;
20
use Cubiche\Core\Specification\Constraint\Equal;
21
use Cubiche\Core\Specification\Constraint\GreaterThan;
22
use Cubiche\Core\Specification\Constraint\GreaterThanEqual;
23
use Cubiche\Core\Specification\Constraint\LessThan;
24
use Cubiche\Core\Specification\Constraint\LessThanEqual;
25
use Cubiche\Core\Specification\Constraint\NotEqual;
26
use Cubiche\Core\Specification\Constraint\NotSame;
27
use Cubiche\Core\Specification\Constraint\Same;
28
use Cubiche\Core\Specification\Criteria;
29
use Cubiche\Core\Specification\Quantifier\All;
30
use Cubiche\Core\Specification\Quantifier\AtLeast;
31
use Cubiche\Core\Specification\Quantifier\Quantifier;
32
use Cubiche\Core\Specification\Selector;
33
use Cubiche\Core\Specification\SpecificationInterface;
34
use Cubiche\Tests\TestCase;
35
36
/**
37
 * Criteria Tests Class.
38
 *
39
 * @author Ivannis Suárez Jerez <[email protected]>
40
 * @author Karel Osorio Ramírez <[email protected]>
41
 */
42
class CriteriaTests extends TestCase
43
{
44
    /**
45
     * Test false.
46
     */
47
    public function testFalse()
48
    {
49
        $this->valueSelectorTest(Criteria::false(), false);
50
    }
51
52
    /**
53
     * Test true.
54
     */
55
    public function testTrue()
56
    {
57
        $this->valueSelectorTest(Criteria::true(), true);
58
    }
59
60
    /**
61
     * Test null.
62
     */
63
    public function testNull()
64
    {
65
        $this->valueSelectorTest(Criteria::null(), null);
66
    }
67
68
    /**
69
     * Test key.
70
     */
71
    public function testKey()
72
    {
73
        $this->fieldSelectorTest(Criteria::key('foo'), Key::class, 'foo');
74
    }
75
76
    /**
77
     * Test property.
78
     */
79
    public function testProperty()
80
    {
81
        $this->fieldSelectorTest(Criteria::property('foo'), Property::class, 'foo');
82
    }
83
84
    /**
85
     * Test method.
86
     */
87
    public function testMethod()
88
    {
89
        $this->fieldSelectorTest(Criteria::method('foo'), Method::class, 'foo');
90
    }
91
92
    /**
93
     * Test this.
94
     */
95 View Code Duplication
    public function testThis()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $this
98
            ->given($criteria = Criteria::this())
99
            ->then()
100
                ->object($criteria)
101
                    ->isInstanceOf(Selector::class)
102
                ->object($criteria->selector())
103
                    ->isInstanceOf(This::class)
104
        ;
105
    }
106
107
    /**
108
     * Test callback.
109
     */
110 View Code Duplication
    public function testCallback()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $this
113
            ->given($criteria = Criteria::callback(function () {
114
115
            }))
116
            ->then()
117
                ->object($criteria)
118
                    ->isInstanceOf(Selector::class)
119
                ->object($criteria->selector())
120
                    ->isInstanceOf(Callback::class)
121
        ;
122
    }
123
124
    /**
125
     * Test count.
126
     */
127 View Code Duplication
    public function testCount()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
    {
129
        $this
130
            ->given($criteria = Criteria::count())
131
            ->then()
132
                ->object($criteria)
133
                    ->isInstanceOf(Selector::class)
134
                ->object($criteria->selector())
135
                    ->isInstanceOf(Count::class)
136
        ;
137
    }
138
139
    /**
140
     * Test gt.
141
     */
142
    public function testGt()
143
    {
144
        $this->binaryConstraintTest(Criteria::gt(5), GreaterThan::class, 5);
145
    }
146
147
    /**
148
     * Test gte.
149
     */
150
    public function testGte()
151
    {
152
        $this->binaryConstraintTest(Criteria::gte(5), GreaterThanEqual::class, 5);
153
    }
154
155
    /**
156
     * Test lt.
157
     */
158
    public function testLt()
159
    {
160
        $this->binaryConstraintTest(Criteria::lt(5), LessThan::class, 5);
161
    }
162
163
    /**
164
     * Test lte.
165
     */
166
    public function testLte()
167
    {
168
        $this->binaryConstraintTest(Criteria::lte(5), LessThanEqual::class, 5);
169
    }
170
171
    /**
172
     * Test eq.
173
     */
174
    public function testEq()
175
    {
176
        $this->binaryConstraintTest(Criteria::eq(5), Equal::class, 5);
177
    }
178
179
    /**
180
     * Test neq.
181
     */
182
    public function testNeq()
183
    {
184
        $this->binaryConstraintTest(Criteria::neq(5), NotEqual::class, 5);
185
    }
186
187
    /**
188
     * Test same.
189
     */
190
    public function testSame()
191
    {
192
        $this->binaryConstraintTest(Criteria::same(5), Same::class, 5);
193
    }
194
195
    /**
196
     * Test notsame.
197
     */
198
    public function testNotsame()
199
    {
200
        $this->binaryConstraintTest(Criteria::notSame(5), NotSame::class, 5);
201
    }
202
203
    /**
204
     * Test isNull.
205
     */
206
    public function testIsNull()
207
    {
208
        $this->binaryConstraintTest(Criteria::isNull(), Same::class, null);
209
    }
210
211
    /**
212
     * Test notNull.
213
     */
214
    public function testNotNull()
215
    {
216
        $this->binaryConstraintTest(Criteria::notNull(), NotSame::class, null);
217
    }
218
219
    /**
220
     * Test isTrue.
221
     */
222
    public function testIsTrue()
223
    {
224
        $this->binaryConstraintTest(Criteria::isTrue(), Same::class, true);
225
    }
226
227
    /**
228
     * Test isFalse.
229
     */
230
    public function testIsFalse()
231
    {
232
        $this->binaryConstraintTest(Criteria::isFalse(), Same::class, false);
233
    }
234
235
    /**
236
     * Test all.
237
     */
238
    public function testAll()
239
    {
240
        $this->quantifierTest(Criteria::all($specification = Criteria::gt(5)), All::class, $specification);
241
    }
242
243
    /**
244
     * Test atLeast.
245
     */
246
    public function testAtLeast()
247
    {
248
        $this->atLeastTest(Criteria::atLeast(2, $specification = Criteria::gt(5)), 2, $specification);
249
    }
250
251
    /**
252
     * Test any.
253
     */
254
    public function testAny()
255
    {
256
        $this->atLeastTest(Criteria::any($specification = Criteria::gt(5)), 1, $specification);
257
    }
258
259
    /**
260
     * Test not.
261
     */
262
    public function testNot()
263
    {
264
        $this
265
            ->given($specification = Criteria::gt(5))
266
            ->given($criteria = Criteria::not($specification))
267
            ->then()
268
                ->variable($criteria)
269
                    ->isEqualTo($specification->not())
270
        ;
271
    }
272
273
    /**
274
     * @param Selector $criteria
275
     * @param mixed    $value
276
     */
277 View Code Duplication
    protected function valueSelectorTest($criteria, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
278
    {
279
        $this
280
            ->given($criteria, $value)
281
            ->then()
282
                ->object($criteria)
283
                    ->isInstanceOf(Selector::class)
284
                ->object($selector = $criteria->selector())
285
                    ->isInstanceOf(Value::class)
286
                ->variable($selector->value())
287
                    ->isEqualTo($value)
288
            ;
289
    }
290
291
    /**
292
     * @param Selector $criteria
293
     * @param string   $class
294
     * @param string   $name
295
     */
296 View Code Duplication
    protected function fieldSelectorTest($criteria, $class, $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
297
    {
298
        $this
299
            ->given($criteria, $class, $name)
300
            ->then()
301
                ->object($criteria)
302
                    ->isInstanceOf(Selector::class)
303
                /* @var \Cubiche\Core\Selector\Field $selector */
304
                ->object($selector = $criteria->selector())
305
                    ->isInstanceOf($class)
306
                ->string($selector->name())
307
                    ->isEqualTo($name)
308
        ;
309
    }
310
311
    /**
312
     * @param BinaryConstraintOperator $constraint
313
     * @param string                   $class
314
     * @param mixed                    $value
315
     */
316
    protected function binaryConstraintTest($constraint, $class, $value)
317
    {
318
        $this
319
            ->given($constraint, $class, $value)
320
            ->then()
321
                ->object($constraint)
322
                    ->isInstanceOf($class)
323
                ->object($constraint->left())
324
                    ->isInstanceOf(This::class)
325
                /* @var \Cubiche\Core\Selector\Value $rightSelector */
326
                ->object($rightSelector = $constraint->right())
327
                    ->isInstanceOf(Value::class)
328
                ->variable($rightSelector->value())
329
                    ->isEqualTo($value)
330
        ;
331
    }
332
333
    /**
334
     * @param Quantifier             $quantifier
335
     * @param string                 $class
336
     * @param SpecificationInterface $specification
337
     */
338
    protected function quantifierTest($quantifier, $class, $specification)
339
    {
340
        $this
341
            ->given($quantifier, $class, $specification)
342
            ->then()
343
                ->object($quantifier)
344
                    ->isInstanceOf($class)
345
                ->object($quantifier->selector())
346
                    ->isInstanceOf(This::class)
347
                ->object($quantifier->specification())
348
                    ->isIdenticalTo($specification)
349
        ;
350
    }
351
352
    /**
353
     * @param AtLeast                $atLeast
354
     * @param int                    $count
355
     * @param SpecificationInterface $specification
356
     */
357
    protected function atLeastTest($atLeast, $count, $specification)
358
    {
359
        $this->quantifierTest($atLeast, AtLeast::class, $specification);
360
361
        $this
362
            ->given($atLeast, $count)
363
        ->then()
364
            ->integer($atLeast->count())
365
                ->isEqualTo($count)
366
        ;
367
    }
368
}
369