diff --git a/src/Schema/Criteria.php b/src/Schema/Criteria.php index 4182e94f..7c23804d 100644 --- a/src/Schema/Criteria.php +++ b/src/Schema/Criteria.php @@ -118,196 +118,140 @@ public function getOffset() : int return $this->offset; } - public static function eqIterator() : self + public static function iteratorType(int $iteratorType) : self { $self = new self(); - $self->iteratorType = IteratorTypes::EQ; + $self->iteratorType = $iteratorType; return $self; } - public function andEqIterator() : self + public function andIteratorType(int $iteratorType) : self { $new = clone $this; - $new->iteratorType = IteratorTypes::EQ; + $new->iteratorType = $iteratorType; return $new; } - public static function reqIterator() : self + public static function eqIterator() : self { - $self = new self(); - $self->iteratorType = IteratorTypes::REQ; + return self::iteratorType(IteratorTypes::EQ); + } - return $self; + public function andEqIterator() : self + { + return $this->andIteratorType(IteratorTypes::EQ); } - public function andReqIterator() : self + public static function reqIterator() : self { - $new = clone $this; - $new->iteratorType = IteratorTypes::REQ; + return self::iteratorType(IteratorTypes::REQ); + } - return $new; + public function andReqIterator() : self + { + return $this->andIteratorType(IteratorTypes::REQ); } public static function allIterator() : self { - $self = new self(); - $self->iteratorType = IteratorTypes::ALL; - - return $self; + return self::iteratorType(IteratorTypes::ALL); } public function andAllIterator() : self { - $new = clone $this; - $new->iteratorType = IteratorTypes::ALL; - - return $new; + return $this->andIteratorType(IteratorTypes::ALL); } public static function ltIterator() : self { - $self = new self(); - $self->iteratorType = IteratorTypes::LT; - - return $self; + return self::iteratorType(IteratorTypes::LT); } public function andLtIterator() : self { - $new = clone $this; - $new->iteratorType = IteratorTypes::LT; - - return $new; + return $this->andIteratorType(IteratorTypes::LT); } public static function leIterator() : self { - $self = new self(); - $self->iteratorType = IteratorTypes::LE; - - return $self; + return self::iteratorType(IteratorTypes::LE); } public function andLeIterator() : self { - $new = clone $this; - $new->iteratorType = IteratorTypes::LE; - - return $new; + return $this->andIteratorType(IteratorTypes::LE); } public static function geIterator() : self { - $self = new self(); - $self->iteratorType = IteratorTypes::GE; - - return $self; + return self::iteratorType(IteratorTypes::GE); } public function andGeIterator() : self { - $new = clone $this; - $new->iteratorType = IteratorTypes::GE; - - return $new; + return $this->andIteratorType(IteratorTypes::GE); } public static function gtIterator() : self { - $self = new self(); - $self->iteratorType = IteratorTypes::GT; - - return $self; + return self::iteratorType(IteratorTypes::GT); } public function andGtIterator() : self { - $new = clone $this; - $new->iteratorType = IteratorTypes::GT; - - return $new; + return $this->andIteratorType(IteratorTypes::GT); } public static function bitsAllSetIterator() : self { - $self = new self(); - $self->iteratorType = IteratorTypes::BITS_ALL_SET; - - return $self; + return self::iteratorType(IteratorTypes::BITS_ALL_SET); } public function andBitsAllSetIterator() : self { - $new = clone $this; - $new->iteratorType = IteratorTypes::BITS_ALL_SET; - - return $new; + return $this->andIteratorType(IteratorTypes::BITS_ALL_SET); } public static function bitsAnySetIterator() : self { - $self = new self(); - $self->iteratorType = IteratorTypes::BITS_ANY_SET; - - return $self; + return self::iteratorType(IteratorTypes::BITS_ANY_SET); } public function andBitsAnySetIterator() : self { - $new = clone $this; - $new->iteratorType = IteratorTypes::BITS_ANY_SET; - - return $new; + return $this->andIteratorType(IteratorTypes::BITS_ANY_SET); } public static function bitsAllNotSetIterator() : self { - $self = new self(); - $self->iteratorType = IteratorTypes::BITS_ALL_NOT_SET; - - return $self; + return self::iteratorType(IteratorTypes::BITS_ALL_NOT_SET); } public function andBitsAllNotSetIterator() : self { - $new = clone $this; - $new->iteratorType = IteratorTypes::BITS_ALL_NOT_SET; - - return $new; + return $this->andIteratorType(IteratorTypes::BITS_ALL_NOT_SET); } public static function overlapsIterator() : self { - $self = new self(); - $self->iteratorType = IteratorTypes::OVERLAPS; - - return $self; + return self::iteratorType(IteratorTypes::OVERLAPS); } public function andOverlapsIterator() : self { - $new = clone $this; - $new->iteratorType = IteratorTypes::OVERLAPS; - - return $new; + return $this->andIteratorType(IteratorTypes::OVERLAPS); } public static function neighborIterator() : self { - $self = new self(); - $self->iteratorType = IteratorTypes::NEIGHBOR; - - return $self; + return self::iteratorType(IteratorTypes::NEIGHBOR); } public function andNeighborIterator() : self { - $new = clone $this; - $new->iteratorType = IteratorTypes::NEIGHBOR; - - return $new; + return $this->andIteratorType(IteratorTypes::NEIGHBOR); } public function getIteratorType() : int diff --git a/tests/Unit/Schema/CriteriaTest.php b/tests/Unit/Schema/CriteriaTest.php new file mode 100644 index 00000000..8963a432 --- /dev/null +++ b/tests/Unit/Schema/CriteriaTest.php @@ -0,0 +1,121 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tarantool\Client\Tests\Unit\Schema; + +use PHPUnit\Framework\TestCase; +use Tarantool\Client\Schema\Criteria; +use Tarantool\Client\Schema\IteratorTypes; + +final class CriteriaTest extends TestCase +{ + public function testIndexId() : void + { + self::assertSame(1, Criteria::index(1)->getIndex()); + } + + public function testAndIndexId() : void + { + self::assertSame(2, Criteria::index(1)->andIndex(2)->getIndex()); + } + + public function testIndexName() : void + { + self::assertSame('foo', Criteria::index('foo')->getIndex()); + } + + public function testAndIndexName() : void + { + self::assertSame('bar', Criteria::index('foo')->andIndex('bar')->getIndex()); + } + + public function testKey() : void + { + self::assertSame([1], Criteria::key([1])->getKey()); + } + + public function testAndKey() : void + { + self::assertSame([2], Criteria::key([1])->andKey([2])->getKey()); + } + + public function testLimit() : void + { + self::assertSame(1, Criteria::limit(1)->getLimit()); + } + + public function testAndLimit() : void + { + self::assertSame(2, Criteria::limit(1)->andLimit(2)->getLimit()); + } + + public function testOffset() : void + { + self::assertSame(1, Criteria::offset(1)->getOffset()); + } + + public function testAndOffset() : void + { + self::assertSame(2, Criteria::offset(1)->andOffset(2)->getOffset()); + } + + public function testIteratorType() : void + { + self::assertSame(IteratorTypes::ALL, Criteria::iteratorType(IteratorTypes::ALL)->getIteratorType()); + } + + public function testAndIteratorType() : void + { + self::assertSame(IteratorTypes::GE, Criteria::iteratorType(IteratorTypes::ALL)->andIteratorType(IteratorTypes::GE)->getIteratorType()); + } + + /** + * @dataProvider provideIteratorTypeNames + */ + public function testIteratorTypeByName(string $name) : void + { + $method = str_replace('_', '', $name).'iterator'; + + self::assertSame(constant(IteratorTypes::class.'::'.$name), [Criteria::class, $method]()->getIteratorType()); + } + + /** + * @dataProvider provideIteratorTypeNames + */ + public function testAndIteratorTypeByName(string $name) : void + { + // make sure we don't assign the same iterator twice + $method = 'EQ' === $name ? 'allIterator' : 'eqIterator'; + $andMethod = 'and'.str_replace('_', '', $name).'iterator'; + + self::assertSame(constant(IteratorTypes::class.'::'.$name), [Criteria::class, $method]()->$andMethod()->getIteratorType()); + } + + public function provideIteratorTypeNames() : iterable + { + return [ + ['EQ'], + ['REQ'], + ['ALL'], + ['LT'], + ['LE'], + ['GE'], + ['GT'], + ['BITS_ALL_SET'], + ['BITS_ANY_SET'], + ['BITS_ALL_NOT_SET'], + ['OVERLAPS'], + ['NEIGHBOR'], + ]; + } +}