Skip to content
Permalink
Browse files

Relaxing usage for Query::bind() and adding unit test for it

  • Loading branch information...
lorenzo committed Aug 10, 2013
1 parent 6cdf9ce commit ed49d0063c2b936eda74504df2dcb5e60295aa06
Showing with 29 additions and 3 deletions.
  1. +1 −1 lib/Cake/Database/Query.php
  2. +1 −1 lib/Cake/Database/ValueBinder.php
  3. +27 −1 lib/Cake/Test/TestCase/Database/QueryTest.php
@@ -1518,7 +1518,7 @@ public function defaultTypes(array $types = null) {
* to database
* @return Query
*/
public function bind($param, $value, $type) {
public function bind($param, $value, $type = 'string') {
$this->valueBinder()->bind($param, $value, $type);
return $this;
}
@@ -44,7 +44,7 @@ class ValueBinder {
* to database
* @return void
*/
public function bind($param, $value, $type) {
public function bind($param, $value, $type = 'string') {
$this->_bindings[$param] = compact('value', 'type') + [
'placeholder' => is_numeric($param) ? $param : substr($param, 1)
];
@@ -1094,7 +1094,7 @@ public function testSelectHaving() {
$this->assertEquals($expected, $result->fetchAll('assoc'));
$result = $query->having(function($e) {
return $e->add('count(author_id) = 1 + 1');
return $e->add('count(author_id) = 1 + 1');
}, [], true)
->execute();
$expected = [['total' => 2, 'author_id' => 1]];
@@ -1907,6 +1907,32 @@ public function testDefaultTypes() {
$this->assertCount(6, $results, 'All 6 rows should match.');
}
/**
* Tests parameter binding
*
* @return void
*/
public function testBind() {
$query = new Query($this->connection);
$results = $query->select(['id', 'comment'])
->from('comments')
->where(['created BETWEEN :foo AND :bar'])
->bind(':foo', new \DateTime('2007-03-18 10:50:00'), 'datetime')
->bind(':bar', new \DateTime('2007-03-18 10:52:00'), 'datetime')
->execute();
$expected = [['id' => '4', 'comment' => 'Fourth Comment for First Article']];
$this->assertEquals($expected, $results->fetchAll('assoc'));
$query = new Query($this->connection);
$results = $query->select(['id', 'comment'])
->from('comments')
->where(['created BETWEEN :foo AND :bar'])
->bind(':foo', '2007-03-18 10:50:00')
->bind(':bar','2007-03-18 10:52:00')
->execute();
$this->assertEquals($expected, $results->fetchAll('assoc'));
}
/**
* Assertion for comparing a table's contents with what is in it.
*

0 comments on commit ed49d00

Please sign in to comment.
You can’t perform that action at this time.