Skip to content

Commit

Permalink
Always throw a 404
Browse files Browse the repository at this point in the history
Account for the way that (array)null results in [] and not [null]
  • Loading branch information
AD7six committed Dec 16, 2014
1 parent 5cdc7c0 commit 09b25c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
17 changes: 12 additions & 5 deletions src/ORM/Table.php
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -944,7 +945,8 @@ protected function _setFieldMatchers($options, $keys) {
/**
* {@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();
Expand All @@ -954,10 +956,15 @@ public function get($primaryKey, $options = []) {
}
$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);
Expand Down
22 changes: 19 additions & 3 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -3534,11 +3534,11 @@ public function testGetNotFoundException() {
/**
* 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,
Expand All @@ -3547,6 +3547,22 @@ public function testGetExceptionOnIncorrectData() {
$table->get(null);
}

/**
* 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
*
Expand Down

0 comments on commit 09b25c9

Please sign in to comment.