|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the tarantool/client package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Tests\Unit\Schema; |
|
15
|
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
use Tarantool\Client\Schema\Criteria; |
|
18
|
|
|
use Tarantool\Client\Schema\IteratorTypes; |
|
19
|
|
|
|
|
20
|
|
|
final class CriteriaTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function testIndexId() : void |
|
23
|
|
|
{ |
|
24
|
|
|
self::assertSame(1, Criteria::index(1)->getIndex()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testAndIndexId() : void |
|
28
|
|
|
{ |
|
29
|
|
|
self::assertSame(2, Criteria::index(1)->andIndex(2)->getIndex()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testIndexName() : void |
|
33
|
|
|
{ |
|
34
|
|
|
self::assertSame('foo', Criteria::index('foo')->getIndex()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testAndIndexName() : void |
|
38
|
|
|
{ |
|
39
|
|
|
self::assertSame('bar', Criteria::index('foo')->andIndex('bar')->getIndex()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testKey() : void |
|
43
|
|
|
{ |
|
44
|
|
|
self::assertSame([1], Criteria::key([1])->getKey()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testAndKey() : void |
|
48
|
|
|
{ |
|
49
|
|
|
self::assertSame([2], Criteria::key([1])->andKey([2])->getKey()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testLimit() : void |
|
53
|
|
|
{ |
|
54
|
|
|
self::assertSame(1, Criteria::limit(1)->getLimit()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testAndLimit() : void |
|
58
|
|
|
{ |
|
59
|
|
|
self::assertSame(2, Criteria::limit(1)->andLimit(2)->getLimit()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testOffset() : void |
|
63
|
|
|
{ |
|
64
|
|
|
self::assertSame(1, Criteria::offset(1)->getOffset()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testAndOffset() : void |
|
68
|
|
|
{ |
|
69
|
|
|
self::assertSame(2, Criteria::offset(1)->andOffset(2)->getOffset()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testIterator() : void |
|
73
|
|
|
{ |
|
74
|
|
|
self::assertSame(IteratorTypes::ALL, Criteria::iterator(IteratorTypes::ALL)->getIterator()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testAndIterator() : void |
|
78
|
|
|
{ |
|
79
|
|
|
self::assertSame(IteratorTypes::GE, Criteria::iterator(IteratorTypes::ALL)->andIterator(IteratorTypes::GE)->getIterator()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @dataProvider provideIteratorTypes |
|
84
|
|
|
*/ |
|
85
|
|
|
public function testIteratorTypeByName(string $name) : void |
|
86
|
|
|
{ |
|
87
|
|
|
$method = str_replace('_', '', $name).'iterator'; |
|
88
|
|
|
$criteria = ([Criteria::class, $method])(); |
|
89
|
|
|
|
|
90
|
|
|
self::assertSame(constant(IteratorTypes::class.'::'.$name), $criteria->getIterator()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @dataProvider provideIteratorTypes |
|
95
|
|
|
*/ |
|
96
|
|
|
public function testAndIteratorTypeByName(string $name) : void |
|
97
|
|
|
{ |
|
98
|
|
|
// Make sure we don't assign the same iterator twice |
|
99
|
|
|
$method = 'EQ' === $name ? 'allIterator' : 'eqIterator'; |
|
100
|
|
|
$andMethod = 'and'.str_replace('_', '', $name).'iterator'; |
|
101
|
|
|
$criteria = ([Criteria::class, $method])(); |
|
102
|
|
|
|
|
103
|
|
|
self::assertSame(constant(IteratorTypes::class.'::'.$name), $criteria->$andMethod()->getIterator()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function provideIteratorTypes() : iterable |
|
107
|
|
|
{ |
|
108
|
|
|
return [ |
|
109
|
|
|
['EQ'], |
|
110
|
|
|
['REQ'], |
|
111
|
|
|
['ALL'], |
|
112
|
|
|
['LT'], |
|
113
|
|
|
['LE'], |
|
114
|
|
|
['GE'], |
|
115
|
|
|
['GT'], |
|
116
|
|
|
['BITS_ALL_SET'], |
|
117
|
|
|
['BITS_ANY_SET'], |
|
118
|
|
|
['BITS_ALL_NOT_SET'], |
|
119
|
|
|
['OVERLAPS'], |
|
120
|
|
|
['NEIGHBOR'], |
|
121
|
|
|
]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function testDefaultIteratorTypeIsChosenAutomaticallyBasedOnKeyValue() : void |
|
125
|
|
|
{ |
|
126
|
|
|
self::assertSame(IteratorTypes::ALL, Criteria::index(1)->getIterator()); |
|
127
|
|
|
self::assertSame(IteratorTypes::ALL, Criteria::key([])->getIterator()); |
|
128
|
|
|
self::assertSame(IteratorTypes::EQ, Criteria::key([3])->getIterator()); |
|
129
|
|
|
|
|
130
|
|
|
$criteria = Criteria::key([]); |
|
131
|
|
|
$criteria->getIterator(); |
|
132
|
|
|
$criteria->getKey(); |
|
133
|
|
|
|
|
134
|
|
|
self::assertSame(IteratorTypes::EQ, $criteria->andKey([3])->getIterator()); |
|
135
|
|
|
|
|
136
|
|
|
$criteria = Criteria::key([3]); |
|
137
|
|
|
$criteria->getIterator(); |
|
138
|
|
|
$criteria->getKey(); |
|
139
|
|
|
|
|
140
|
|
|
self::assertSame(IteratorTypes::ALL, $criteria->andKey([])->getIterator()); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|