-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add between operator * Add strict type on between operator * Add strict type for $options * Add strict type for $options var * Fix auto completion for between
- Loading branch information
Showing
3 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace TBolier\RethinkQL\Query\Operation; | ||
|
||
use TBolier\RethinkQL\Query\AbstractQuery; | ||
use TBolier\RethinkQL\Query\Aggregation\AggregationTrait; | ||
use TBolier\RethinkQL\Query\QueryInterface; | ||
use TBolier\RethinkQL\Query\Transformation\TransformationTrait; | ||
use TBolier\RethinkQL\RethinkInterface; | ||
use TBolier\RethinkQL\Types\Term\TermType; | ||
|
||
class Between extends AbstractQuery | ||
{ | ||
use AggregationTrait; | ||
use OperationTrait; | ||
use TransformationTrait; | ||
|
||
/** | ||
* @var QueryInterface | ||
*/ | ||
private $query; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $min; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $max; | ||
|
||
/** | ||
* @var array|null | ||
*/ | ||
private $options; | ||
|
||
public function __construct( | ||
RethinkInterface $rethink, | ||
QueryInterface $query, | ||
$min, | ||
$max, | ||
array $options = null | ||
) { | ||
parent::__construct($rethink); | ||
|
||
$this->rethink = $rethink; | ||
$this->query = $query; | ||
$this->min = $min; | ||
$this->max = $max; | ||
$this->options = $options; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$args = [ | ||
$this->query->toArray(), | ||
$this->min, | ||
$this->max, | ||
]; | ||
|
||
if ($this->options !== null) { | ||
$args = array_merge($args, [$this->options]); | ||
} | ||
|
||
return [ | ||
TermType::BETWEEN, | ||
$args, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TBolier\RethinkQL\IntegrationTest\Query; | ||
|
||
use TBolier\RethinkQL\Response\Cursor; | ||
use TBolier\RethinkQL\Response\ResponseInterface; | ||
use TBolier\RethinkQL\Types\Term\TermType; | ||
|
||
class BetweenTest extends AbstractTableTest | ||
{ | ||
/** | ||
* @throws \Exception | ||
*/ | ||
public function testBetweenMin() | ||
{ | ||
$this->insertDocument(1); | ||
$this->insertDocument(3); | ||
|
||
/** @var Cursor $cursor */ | ||
$cursor = $this->r() | ||
->table('tabletest') | ||
->between(1, 2) | ||
->run(); | ||
|
||
/** @var array $array */ | ||
$array = $cursor->current(); | ||
|
||
$this->assertInstanceOf(\Iterator::class, $cursor); | ||
$this->assertEquals(1, $cursor->count()); | ||
$this->assertArraySubset(['title' => 'Test document 1'], $array); | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function testBetweenMax() | ||
{ | ||
$this->insertDocument(1); | ||
$this->insertDocument(3); | ||
|
||
/** @var Cursor $cursor */ | ||
$cursor = $this->r() | ||
->table('tabletest') | ||
->between(2, 3) | ||
->run(); | ||
|
||
$this->assertInstanceOf(\Iterator::class, $cursor); | ||
$this->assertCount(0, $cursor); | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function testBetweenMultiple() | ||
{ | ||
$this->insertDocument(1); | ||
$this->insertDocument(2); | ||
$this->insertDocument(3); | ||
$this->insertDocument(4); | ||
$this->insertDocument(5); | ||
|
||
/** @var Cursor $cursor */ | ||
$cursor = $this->r() | ||
->table('tabletest') | ||
->between(2, 4) | ||
->run(); | ||
|
||
$this->assertInstanceOf(\Iterator::class, $cursor); | ||
$this->assertCount(2, $cursor); | ||
} | ||
|
||
/** | ||
* @return void | ||
* @throws \Exception | ||
*/ | ||
public function testBetweenNoResult(): void | ||
{ | ||
$this->insertDocument('stringId'); | ||
|
||
/** @var Cursor $cursor */ | ||
$cursor = $this->r() | ||
->table('tabletest') | ||
->between(2, 4) | ||
->run(); | ||
|
||
$this->assertInstanceOf(\Iterator::class, $cursor); | ||
$this->assertCount(0, $cursor); | ||
} | ||
} |