Skip to content

Commit

Permalink
Fixing code and adding test for the Between expression
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Oct 26, 2014
1 parent 76cc182 commit 3cecdc7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Database/Expression/BetweenExpression.php
Expand Up @@ -45,6 +45,13 @@ class BetweenExpression implements ExpressionInterface {
*/
protected $_to;

/**
* The data type for the from and to arguments
*
* @var mixed
*/
protected $_type;

/**
* Constructor
*
Expand All @@ -55,7 +62,7 @@ public function __construct($field, $from, $to, $type = null) {
$this->_field = $field;
$this->_from = $from;
$this->_to = $to;
$this->$type = $type;
$this->_type = $type;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Database/Expression/QueryExpression.php
Expand Up @@ -298,6 +298,10 @@ public function notIn($field, $values, $type = null) {
return $this->add(new Comparison($field, $values, $type, 'NOT IN'));
}

public function between($field, $from, $to, $type = null) {
return $this->add(new BetweenExpression($field, $from, $to, $type));
}

// @codingStandardsIgnoreStart
/**
* Returns a new QueryExpression object containing all the conditions passed
Expand Down
50 changes: 50 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -1122,6 +1122,56 @@ public function testInValueCast2() {
$this->assertCount(5, $result);
}

/**
* Tests that it is possible to use a between expression
* in a where condition
*
* @return void
*/
public function testWhereWithBetween() {
$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('comments')
->where(function($exp) {
return $exp->between('id', 5, 6, 'integer');
})
->execute();

$this->assertCount(2, $result);
$first = $result->fetch('assoc');
$this->assertEquals(5, $first['id']);

$second = $result->fetch('assoc');
$this->assertEquals(6, $second['id']);
}

/**
* Tests that it is possible to use a between expression
* in a where condition with a complex data type
*
* @return void
*/
public function testWhereWithBetweenComplex() {
$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('comments')
->where(function($exp) {
$from = new \DateTime('2007-03-18 10:51:00');
$to = new \DateTime('2007-03-18 10:54:00');
return $exp->between('created', $from, $to, 'datetime');
})
->execute();

$this->assertCount(2, $result);
$first = $result->fetch('assoc');
$this->assertEquals(4, $first['id']);

$second = $result->fetch('assoc');
$this->assertEquals(5, $second['id']);
}

/**
* Tests nesting query expressions both using arrays and closures
*
Expand Down

0 comments on commit 3cecdc7

Please sign in to comment.