ソースを参照

Test against `EntityInterface` instead of `self`

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`.
ndm2 11 年 前
コミット
333ed728e4

+ 6 - 6
src/Datasource/EntityTrait.php

@@ -407,12 +407,12 @@ trait EntityTrait {
 		$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;
@@ -674,7 +674,7 @@ trait EntityTrait {
 		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;
@@ -683,7 +683,7 @@ trait EntityTrait {
 			if (
 				is_array($val) ||
 				$val instanceof Traversable ||
-				$val instanceof self
+				$val instanceof EntityInterface
 			) {
 				$entity = $val;
 			} else {
@@ -705,12 +705,12 @@ trait EntityTrait {
  * @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);

+ 13 - 12
tests/TestCase/ORM/EntityTest.php

@@ -17,9 +17,8 @@ namespace Cake\Test\TestCase\ORM;
 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.
@@ -659,10 +658,10 @@ class EntityTest extends TestCase {
  */
 	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']);
@@ -825,9 +824,9 @@ class EntityTest extends TestCase {
  * @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,
@@ -857,14 +856,16 @@ class EntityTest extends TestCase {
  * @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'));

+ 1 - 1
tests/test_app/TestApp/Model/Entity/Owner.php

@@ -8,6 +8,6 @@ use Cake\ORM\Entity;
  * Tests entity class used for asserting correct loading
  *
  */
-class Owner extends Entity {
+class Extending extends Entity {
 
 }

+ 46 - 0
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']);
+		}
+	}
+
+}

+ 0 - 13
tests/test_app/TestApp/Model/Entity/User.php

@@ -1,13 +0,0 @@
-<?php
-
-namespace TestApp\Model\Entity;
-
-use Cake\ORM\Entity;
-
-/**
- * Tests entity class used for asserting correct loading
- *
- */
-class User extends Entity {
-
-}