浏览代码

Merge pull request #10737 from cakephp/issue-10734

Don't emit errors when clauses are undefined.
José Lorenzo Rodríguez 8 年之前
父节点
当前提交
d88f3aae16
共有 2 个文件被更改,包括 20 次插入0 次删除
  1. 6 0
      src/Database/Query.php
  2. 14 0
      tests/TestCase/Database/QueryTest.php

+ 6 - 0
src/Database/Query.php

@@ -1695,9 +1695,15 @@ class Query implements ExpressionInterface, IteratorAggregate
      *
      * @param string $name name of the clause to be returned
      * @return mixed
+     * @throws InvalidArgumentException When the named clause does not exist.
      */
     public function clause($name)
     {
+        if (!array_key_exists($name, $this->_parts)) {
+            $clauses = implode(', ', array_keys($this->_parts));
+            throw new InvalidArgumentException("The '$name' clause is not defined. Valid clauses are: $clauses");
+        }
+
         return $this->_parts[$name];
     }
 

+ 14 - 0
tests/TestCase/Database/QueryTest.php

@@ -4330,6 +4330,20 @@ class QueryTest extends TestCase
     }
 
     /**
+     * Test that reading an undefined clause does not emit an error.
+     *
+     * @expectedException \InvalidArgumentException
+     * @expectedExceptionMessage The 'nope' clause is not defined. Valid clauses are: delete, update
+     * @return void
+     */
+    public function testClauseUndefined()
+    {
+        $query = new Query($this->connection);
+        $this->assertEmpty($query->clause('where'));
+        $query->clause('nope');
+    }
+
+    /**
      * Assertion for comparing a table's contents with what is in it.
      *
      * @param string $table