Skip to content
Permalink
Browse files

Make missing associations fail more loudly.

When a developer asks for an association that doesn't exist, we should
let them know. Silent errors make the ORM harder to debug and
understand.

Refs #9166
  • Loading branch information...
markstory committed Jul 27, 2016
1 parent adf7320 commit 26fc35d661ae173af835592e5ca278fdf0fc2005
Showing with 61 additions and 2 deletions.
  1. +11 −2 src/ORM/Marshaller.php
  2. +50 −0 tests/TestCase/ORM/MarshallerTest.php
@@ -58,6 +58,7 @@ public function __construct(Table $table)
* Build the map of property => association names.
*
* @param array $options List of options containing the 'associated' key.
* @throws \RuntimeException When associations do not exist.
* @return array
*/
protected function _buildPropertyMap($options)
@@ -74,10 +75,18 @@ protected function _buildPropertyMap($options)
$key = $nested;
$nested = [];
}
if ($key === '_joinData') {
continue;
}
$assoc = $this->_table->association($key);
if ($assoc) {
$map[$assoc->property()] = ['association' => $assoc] + $nested + ['associated' => []];
if (!$assoc) {
throw new RuntimeException(sprintf(
'Cannot marshal data for "%s" association. It is not associated with "%s".',
$key,
$this->_table->alias()
));
}
$map[$assoc->property()] = ['association' => $assoc] + $nested + ['associated' => []];
}
return $map;
@@ -322,6 +322,29 @@ public function testOneAccessibleFieldsOption()
$this->assertTrue($result->not_in_schema);
}
/**
* Test one() with an invalid association
*
* @expectedException RuntimeException
* @expectedExceptionMessage Cannot marshal data for "Derp" association. It is not associated with "Articles".
* @return void
*/
public function testOneInvalidAssociation()
{
$data = [
'title' => 'My title',
'body' => 'My content',
'derp' => [
'id' => 1,
'username' => 'mark',
]
];
$marshall = new Marshaller($this->articles);
$marshall->one($data, [
'associated' => ['Derp']
]);
}
/**
* Test one() supports accessibleFields option for associations
*
@@ -1304,6 +1327,33 @@ public function testMergeWhitelist()
$this->assertEquals($expected, $result->toArray());
}
/**
* Test merge() with an invalid association
*
* @expectedException RuntimeException
* @expectedExceptionMessage Cannot marshal data for "Derp" association. It is not associated with "Articles".
* @return void
*/
public function testMergeInvalidAssociation()
{
$data = [
'title' => 'My title',
'body' => 'My content',
'derp' => [
'id' => 1,
'username' => 'mark',
]
];
$article = new Entity([
'title' => 'title for post',
'body' => 'body',
]);
$marshall = new Marshaller($this->articles);
$marshall->merge($article, $data, [
'associated' => ['Derp']
]);
}
/**
* Test merge when fieldList contains an association.
*

0 comments on commit 26fc35d

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.