From 3cecdc729219da4977fe0f595aaaf3192d1ea3fa Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sun, 26 Oct 2014 21:28:53 +0100 Subject: [PATCH] Fixing code and adding test for the Between expression --- src/Database/Expression/BetweenExpression.php | 9 +++- src/Database/Expression/QueryExpression.php | 4 ++ tests/TestCase/Database/QueryTest.php | 50 +++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/Database/Expression/BetweenExpression.php b/src/Database/Expression/BetweenExpression.php index f3e70b83b80..a4385449691 100644 --- a/src/Database/Expression/BetweenExpression.php +++ b/src/Database/Expression/BetweenExpression.php @@ -45,6 +45,13 @@ class BetweenExpression implements ExpressionInterface { */ protected $_to; +/** + * The data type for the from and to arguments + * + * @var mixed + */ + protected $_type; + /** * Constructor * @@ -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; } /** diff --git a/src/Database/Expression/QueryExpression.php b/src/Database/Expression/QueryExpression.php index 13d7b1e58d5..cf6128c5832 100644 --- a/src/Database/Expression/QueryExpression.php +++ b/src/Database/Expression/QueryExpression.php @@ -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 diff --git a/tests/TestCase/Database/QueryTest.php b/tests/TestCase/Database/QueryTest.php index 67cf0643b5f..8c537f2b372 100644 --- a/tests/TestCase/Database/QueryTest.php +++ b/tests/TestCase/Database/QueryTest.php @@ -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 *