Skip to content
Permalink
Browse files

Throw exception if marshaller encounter missing/invalid associations …

…during entity marshalling
  • Loading branch information...
Antoniossss committed May 14, 2016
1 parent 6189452 commit 456c6fc4cbecdd1085584ebc858ab048def810f1
@@ -0,0 +1,24 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM\Exception;
use Cake\Core\Exception\Exception;
/**
* Exception raised when marshaller hits nonexistend association while marshalling entity
*/
class MissingAssociationException extends Exception
{
protected $_messageTemplate = '%s is not associated with %s.';
}
@@ -20,6 +20,7 @@
use Cake\Database\Type;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\InvalidPropertyInterface;
use Cake\ORM\Exception\MissingAssociationException;
use RuntimeException;
/**
@@ -77,6 +78,11 @@ protected function _buildPropertyMap($options)
$assoc = $this->_table->association($key);
if ($assoc) {
$map[$assoc->property()] = ['association' => $assoc] + $nested + ['associated' => []];
} else {
if (substr($key, 0, 1) !== "_") { // if $key is a special underscored field eg _ids or _joinData
throw new MissingAssociationException([$this->_table->alias(), $key]);
}
}
}
return $map;
@@ -17,6 +17,7 @@
use Cake\Database\Expression\IdentifierExpression;
use Cake\I18n\Time;
use Cake\ORM\Entity;
use Cake\ORM\Exception\MissingAssociationException;
use Cake\ORM\Marshaller;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
@@ -1131,6 +1132,33 @@ public function testManyAssociations()
);
}
/**
* Test if exceptin is raised when called with [associated=>NonExistingAssociation]
* Previously such association has been simply ignored
*/
public function testManyInvalidAssociation()
{
$this->expectException(MissingAssociationException::class);
$data = [
[
'comment' => 'First post',
'user_id' => 2,
'user' => [
'username' => 'mark',
],
],
[
'comment' => 'Second post',
'user_id' => 2,
'user' => [
'username' => 'jose',
],
],
];
$marshall = new Marshaller($this->comments);
$result = $marshall->many($data, ['associated' => ['Users','People']]);
}
/**
* Test generating a list of entities from a list of ids.
*

0 comments on commit 456c6fc

Please sign in to comment.
You can’t perform that action at this time.