Browse Source

Always throw a 404

Account for the way that (array)null results in [] and not [null]
AD7six 11 years ago
parent
commit
09b25c9537
2 changed files with 31 additions and 8 deletions
  1. 12 5
      src/ORM/Table.php
  2. 19 3
      tests/TestCase/ORM/TableTest.php

+ 12 - 5
src/ORM/Table.php

@@ -19,6 +19,7 @@ use Cake\Core\App;
 use Cake\Database\Schema\Table as Schema;
 use Cake\Database\Type;
 use Cake\Datasource\EntityInterface;
+use Cake\Datasource\Exception\RecordNotFoundException;
 use Cake\Datasource\RepositoryInterface;
 use Cake\Event\EventListenerInterface;
 use Cake\Event\EventManager;
@@ -944,7 +945,8 @@ class Table implements RepositoryInterface, EventListenerInterface {
 /**
  * {@inheritDoc}
  *
- * @throws \InvalidArgumentException When $primaryKey has an incorrect number of elements.
+ * @throws Cake\Datasource\Exception\RecordNotFoundException When $primaryKey has an
+ * 		incorrect number of elements.
  */
 	public function get($primaryKey, $options = []) {
 		$key = (array)$this->primaryKey();
@@ -954,10 +956,15 @@ class Table implements RepositoryInterface, EventListenerInterface {
 		}
 		$primaryKey = (array)$primaryKey;
 		if (count($key) !== count($primaryKey)) {
-			throw new \InvalidArgumentException(sprintf(
-				"Incorrect number of primary key values. Expected %d got %d.",
-				count($key),
-				count($primaryKey)
+			$primaryKey = $primaryKey ?: [null];
+			$primaryKey = array_map(function($key) {
+				return var_export($key, true);
+			}, $primaryKey);
+
+			throw new RecordNotFoundException(sprintf(
+				'Invalid primary key, record not found in table "%s" with primary key [%s]',
+				$this->table(),
+				implode($primaryKey, ', ')
 			));
 		}
 		$conditions = array_combine($key, $primaryKey);

+ 19 - 3
tests/TestCase/ORM/TableTest.php

@@ -3534,11 +3534,11 @@ class TableTest extends TestCase {
 /**
  * Test that an exception is raised when there are not enough keys.
  *
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Incorrect number of primary key values. Expected 1 got 0.
+ * @expectedException Cake\Datasource\Exception\RecordNotFoundException
+ * @expectedExceptionMessage Invalid primary key, record not found in table "articles" with primary key [NULL]
  * @return void
  */
-	public function testGetExceptionOnIncorrectData() {
+	public function testGetExceptionOnNoData() {
 		$table = new Table([
 			'name' => 'Articles',
 			'connection' => $this->connection,
@@ -3548,6 +3548,22 @@ class TableTest extends TestCase {
 	}
 
 /**
+ * Test that an exception is raised when there are too many keys.
+ *
+ * @expectedException Cake\Datasource\Exception\RecordNotFoundException
+ * @expectedExceptionMessage Invalid primary key, record not found in table "articles" with primary key [1, 'two']
+ * @return void
+ */
+	public function testGetExceptionOnTooMuchData() {
+		$table = new Table([
+			'name' => 'Articles',
+			'connection' => $this->connection,
+			'table' => 'articles',
+		]);
+		$table->get([1, 'two']);
+	}
+
+/**
  * Tests entityValidator
  *
  * @return void