Permalink
Browse files

Updated to throw exception when using predicate with string keys

  • Loading branch information...
1 parent 36145cf commit 29d9dd016f32851a55f1f0d8f1e2f9e91d4aa9b1 @moura137 committed Feb 16, 2013
Showing with 11 additions and 19 deletions.
  1. +9 −4 library/Zend/Db/Sql/Select.php
  2. +2 −15 tests/ZendTest/Db/Sql/SelectTest.php
@@ -260,6 +260,7 @@ public function join($name, $on, $columns = self::SQL_STAR, $type = self::JOIN_I
*
* @param Where|\Closure|string|array|Predicate\PredicateInterface $predicate
* @param string $combination One of the OP_* constants from Predicate\PredicateSet
+ * @throws Exception\InvalidArgumentException
* @return Select
*/
public function where($predicate, $combination = Predicate\PredicateSet::OP_AND)
@@ -286,10 +287,6 @@ public function where($predicate, $combination = Predicate\PredicateSet::OP_AND)
// as an Expression predicate
$predicate = new Predicate\Expression($pkey, $pvalue);
- } elseif ($pvalue instanceof Predicate\PredicateInterface) {
- // Predicate type is ok
- $predicate = $pvalue;
-
} elseif (is_string($pkey)) {
// Otherwise, if still a string, do something intelligent with the PHP type provided
@@ -299,10 +296,18 @@ public function where($predicate, $combination = Predicate\PredicateSet::OP_AND)
} elseif (is_array($pvalue)) {
// if the value is an array, assume IN() is desired
$predicate = new Predicate\In($pkey, $pvalue);
+ } elseif ($pvalue instanceof Predicate\PredicateInterface) {
+ //
+ throw new Exception\InvalidArgumentException(
+ 'Using Predicate must not use string keys'
+ );
} else {
// otherwise assume that array('foo' => 'bar') means "foo" = 'bar'
$predicate = new Predicate\Operator($pkey, Predicate\Operator::OP_EQ, $pvalue);
}
+ } elseif ($pvalue instanceof Predicate\PredicateInterface) {
+ // Predicate type is ok
+ $predicate = $pvalue;
} else {
// must be an array of expressions (with int-indexed array)
$predicate = (strpos($pvalue, Expression::PLACEHOLDER) !== false)
@@ -241,8 +241,9 @@ public function testWhereArgument1IsAssociativeArrayNotContainingReplacementChar
}
/**
- * @testdox unit test: Test where() will accept any array with string key (without ?) with Predicate object as-is
+ * @testdox unit test: Test where() will accept any array with string key (without ?) with Predicate throw Exception
* @covers Zend\Db\Sql\Select::where
+ * @expectedException Zend\Db\Sql\Exception\InvalidArgumentException
*/
public function testWhereArgument1IsAssociativeArrayIsPredicate()
{
@@ -252,20 +253,6 @@ public function testWhereArgument1IsAssociativeArrayIsPredicate()
'age' => new Predicate\Expression('age = ?', 33),
);
$select->where($where);
-
- /** @var $where Where */
- $where = $select->getRawState('where');
- $predicates = $where->getPredicates();
- $this->assertEquals(2, count($predicates));
-
- $this->assertInstanceOf('Zend\Db\Sql\Predicate\Literal', $predicates[0][1]);
- $this->assertEquals(Where::OP_AND, $predicates[0][0]);
- $this->assertEquals("name = 'Ralph'", $predicates[0][1]->getLiteral());
-
- $this->assertInstanceOf('Zend\Db\Sql\Predicate\Expression', $predicates[1][1]);
- $this->assertEquals(Where::OP_AND, $predicates[1][0]);
- $this->assertEquals('age = ?', $predicates[1][1]->getExpression());
- $this->assertEquals(array(33), $predicates[1][1]->getParameters());
}
/**

0 comments on commit 29d9dd0

Please sign in to comment.