Browse Source

Squashed query update type check

Ross Chater 9 years ago
parent
commit
0fdae8e457
2 changed files with 22 additions and 1 deletions
  1. 8 1
      src/Database/Query.php
  2. 14 0
      tests/TestCase/Database/QueryTest.php

+ 8 - 1
src/Database/Query.php

@@ -19,6 +19,7 @@ use Cake\Database\Expression\OrderClauseExpression;
 use Cake\Database\Expression\QueryExpression;
 use Cake\Database\Expression\ValuesExpression;
 use Cake\Database\Statement\CallbackStatement;
+use InvalidArgumentException;
 use IteratorAggregate;
 use RuntimeException;
 
@@ -1412,11 +1413,17 @@ class Query implements ExpressionInterface, IteratorAggregate
      *
      * Can be combined with set() and where() methods to create update queries.
      *
-     * @param string $table The table you want to update.
+     * @param string|\Cake\Database\ExpressionInterface $table The table you want to update.
      * @return $this
      */
     public function update($table)
     {
+        if (!is_string($table) && !($table instanceof ExpressionInterface)) {
+            $text = 'Table must be of type string or "%s", got "%s"';
+            $message = sprintf($text, ExpressionInterface::class, gettype($table));
+            throw new InvalidArgumentException($message);
+        }
+
         $this->_dirty();
         $this->_type = 'update';
         $this->_parts['update'][0] = $table;

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

@@ -2732,6 +2732,20 @@ class QueryTest extends TestCase
     }
 
     /**
+     * Test update with type checking
+     * by passing an array as table arg
+     *
+     * @expectedException \InvalidArgumentException
+     *
+     * @return void
+     */
+    public function testUpdateArgTypeChecking()
+    {
+        $query = new Query($this->connection);
+        $query->update(['Articles']);
+    }
+
+    /**
      * Test update with multiple fields.
      *
      * @return void