Skip to content

Commit

Permalink
Test against EntityInterface instead of self
Browse files Browse the repository at this point in the history
Classes consuming `EntityTrait` do not necessarily have to
extend `Entity`, testing against `self` would fail in various
cases, like for example when one of the compared objects
extends `Entity` and the other just implements `EntityInterface`.
  • Loading branch information
ndm2 committed Dec 11, 2014
1 parent 19931dd commit 333ed72
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 32 deletions.
12 changes: 6 additions & 6 deletions src/Datasource/EntityTrait.php
Expand Up @@ -407,12 +407,12 @@ public function toArray() {
$result = [];
foreach ($this->visibleProperties() as $property) {
$value = $this->get($property);
if (is_array($value) && isset($value[0]) && $value[0] instanceof self) {
if (is_array($value) && isset($value[0]) && $value[0] instanceof EntityInterface) {
$result[$property] = [];
foreach ($value as $k => $entity) {
$result[$property][$k] = $entity->toArray();
}
} elseif ($value instanceof self) {
} elseif ($value instanceof EntityInterface) {
$result[$property] = $value->toArray();
} else {
$result[$property] = $value;
Expand Down Expand Up @@ -674,7 +674,7 @@ protected function _nestedErrors($field) {
while ($len) {
$part = array_shift($path);
$len = count($path);
if ($entity instanceof self) {
if ($entity instanceof EntityInterface) {
$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 self
$val instanceof EntityInterface
) {
$entity = $val;
} else {
Expand All @@ -705,12 +705,12 @@ protected function _nestedErrors($field) {
* @return array
*/
protected function _readError($object, $path = null) {
if ($object instanceof self) {
if ($object instanceof EntityInterface) {
return $object->errors($path);
}
if (is_array($object)) {
$array = array_map(function ($val) {
if ($val instanceof self) {
if ($val instanceof EntityInterface) {
return $val->errors();
}
}, $object);
Expand Down
25 changes: 13 additions & 12 deletions tests/TestCase/ORM/EntityTest.php
Expand Up @@ -17,9 +17,8 @@
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;
use TestApp\Model\Entity\Extending;
use TestApp\Model\Entity\NonExtending;

/**
* Entity test case.
Expand Down Expand Up @@ -659,10 +658,10 @@ public function testToArray() {
*/
public function testToArrayRecursive() {
$data = ['id' => 1, 'name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
$user = new Entity($data);
$user = new Extending($data);
$comments = [
new Entity(['user_id' => 1, 'body' => 'Comment 1']),
new Entity(['user_id' => 1, 'body' => 'Comment 2']),
new NonExtending(['user_id' => 1, 'body' => 'Comment 1']),
new NonExtending(['user_id' => 1, 'body' => 'Comment 2']),
];
$user->comments = $comments;
$user->profile = new Entity(['email' => 'mark@example.com']);
Expand Down Expand Up @@ -825,9 +824,9 @@ public function testErrors() {
* @return void
*/
public function testErrorsDeep() {
$user = new User();
$owner = new Owner();
$author = new Author([
$user = new Entity();
$owner = new NonExtending();
$author = new Extending([
'foo' => 'bar',
'thing' => 'baz',
'user' => $user,
Expand Down Expand Up @@ -857,14 +856,16 @@ public function testErrorsDeep() {
* @return void
*/
public function testErrorPathReading() {
$assoc = new User;
$entity = new Author([
$assoc = new Entity();
$assoc2 = new NonExtending();
$entity = new Extending([
'field' => 'value',
'one' => $assoc,
'many' => [$assoc]
'many' => [$assoc2]
]);
$entity->errors('wrong', 'Bad stuff');
$assoc->errors('nope', 'Terrible things');
$assoc2->errors('nope', 'Terrible things');

$this->assertEquals(['Bad stuff'], $entity->errors('wrong'));
$this->assertEquals(['Terrible things'], $entity->errors('many.0.nope'));
Expand Down
Expand Up @@ -8,6 +8,6 @@
* Tests entity class used for asserting correct loading
*
*/
class Owner extends Entity {
class Extending extends Entity {

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

namespace TestApp\Model\Entity;

use Cake\Datasource\EntityInterface;
use Cake\Datasource\EntityTrait;

/**
* Tests entity class used for asserting correct loading
*
*/
class NonExtending implements EntityInterface {

use EntityTrait;

public function __construct(array $properties = [], array $options = []) {
$options += [
'useSetters' => true,
'markClean' => false,
'markNew' => null,
'guard' => false,
'source' => null
];
$this->_className = get_class($this);

if (!empty($properties)) {
$this->set($properties, [
'setter' => $options['useSetters'],
'guard' => $options['guard']
]);
}

if ($options['markClean']) {
$this->clean();
}

if ($options['markNew'] !== null) {
$this->isNew($options['markNew']);
}

if (!empty($options['source'])) {
$this->source($options['source']);
}
}

}
13 changes: 0 additions & 13 deletions tests/test_app/TestApp/Model/Entity/User.php

This file was deleted.

0 comments on commit 333ed72

Please sign in to comment.