Skip to content

Commit

Permalink
Fix reading of nested errros
Browse files Browse the repository at this point in the history
Testing the objects with `instanceof static` will not work for
different classes that extend `Entity`, as `static` will refer to
the extending class, ie the objects would get tested against the
class on which the `errors()` method is being called.
  • Loading branch information
ndm2 committed Dec 11, 2014
1 parent e6762a6 commit 0fbf8ba
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/Datasource/EntityTrait.php
Expand Up @@ -674,7 +674,7 @@ protected function _nestedErrors($field) {
while ($len) {
$part = array_shift($path);
$len = count($path);
if ($entity instanceof static) {
if ($entity instanceof self) {
$val = $entity->get($part);
} elseif (is_array($entity)) {
$val = isset($entity[$part]) ? $entity[$part] : false;
Expand All @@ -683,7 +683,7 @@ protected function _nestedErrors($field) {
if (
is_array($val) ||
$val instanceof Traversable ||
$val instanceof static
$val instanceof self
) {
$entity = $val;
} else {
Expand All @@ -705,12 +705,12 @@ protected function _nestedErrors($field) {
* @return array
*/
protected function _readError($object, $path = null) {
if ($object instanceof static) {
if ($object instanceof self) {
return $object->errors($path);
}
if (is_array($object)) {
$array = array_map(function ($val) {
if ($val instanceof static) {
if ($val instanceof self) {
return $val->errors();
}
}, $object);
Expand Down
31 changes: 19 additions & 12 deletions tests/TestCase/ORM/EntityTest.php
Expand Up @@ -17,6 +17,9 @@
use Cake\ORM\Entity;
use Cake\TestSuite\TestCase;
use Cake\Validation\Validator;
use TestApp\Model\Entity\Author;
use TestApp\Model\Entity\Owner;
use TestApp\Model\Entity\User;

/**
* Entity test case.
Expand Down Expand Up @@ -822,30 +825,34 @@ public function testErrors() {
* @return void
*/
public function testErrorsDeep() {
$entity2 = new Entity;
$entity3 = new Entity;
$entity = new Entity([
$user = new User();
$owner = new Owner();
$author = new Author([
'foo' => 'bar',
'thing' => 'baz',
'user' => $entity2,
'owner' => $entity3
'user' => $user,
'owner' => $owner
]);
$entity->errors('thing', ['this is a mistake']);
$entity2->errors(['a' => ['error1'], 'b' => ['error2']]);
$entity3->errors(['c' => ['error3'], 'd' => ['error4']]);
$author->errors('thing', ['this is a mistake']);
$user->errors(['a' => ['error1'], 'b' => ['error2']]);
$owner->errors(['c' => ['error3'], 'd' => ['error4']]);

$expected = ['a' => ['error1'], 'b' => ['error2']];
$this->assertEquals($expected, $entity->errors('user'));
$this->assertEquals($expected, $author->errors('user'));

$expected = ['c' => ['error3'], 'd' => ['error4']];
$this->assertEquals($expected, $entity->errors('owner'));
$this->assertEquals($expected, $author->errors('owner'));

$entity->set('multiple', [$entity2, $entity3]);
$author->set('multiple', [$user, $owner]);
$expected = [
['a' => ['error1'], 'b' => ['error2']],
['c' => ['error3'], 'd' => ['error4']]
];
$this->assertEquals($expected, $entity->errors('multiple'));
$this->assertEquals($expected, $author->errors('multiple'));

$user->set('author', $author);
$expected = ['thing' => ['this is a mistake']];
$this->assertEquals($expected, $author->errors('multiple.0.author'));
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/test_app/TestApp/Model/Entity/Owner.php
@@ -0,0 +1,13 @@
<?php

namespace TestApp\Model\Entity;

use Cake\ORM\Entity;

/**
* Tests entity class used for asserting correct loading
*
*/
class Owner extends Entity {

}
13 changes: 13 additions & 0 deletions tests/test_app/TestApp/Model/Entity/User.php
@@ -0,0 +1,13 @@
<?php

namespace TestApp\Model\Entity;

use Cake\ORM\Entity;

/**
* Tests entity class used for asserting correct loading
*
*/
class User extends Entity {

}

0 comments on commit 0fbf8ba

Please sign in to comment.