Browse Source

Adding __debugInfo to EntityTrait

Jose Lorenzo Rodriguez 12 years ago
parent
commit
a081d33f32
2 changed files with 38 additions and 0 deletions
  1. 17 0
      src/Datasource/EntityTrait.php
  2. 21 0
      tests/TestCase/ORM/EntityTest.php

+ 17 - 0
src/Datasource/EntityTrait.php

@@ -671,4 +671,21 @@ trait EntityTrait {
 		return json_encode($this, JSON_PRETTY_PRINT);
 	}
 
+/**
+ * Returns an array that can be used to describe the internal estate of this
+ * object.
+ *
+ * @return array
+ */
+	public function __debugInfo() {
+		return [
+			'new' => $this->isNew(),
+			'accessible' => array_filter($this->_accessible),
+			'properties' => $this->_properties,
+			'dirty' => $this->_dirty,
+			'virtual' => $this->_virtual,
+			'errors' => $this->_errors
+		];
+	}
+
 }

+ 21 - 0
tests/TestCase/ORM/EntityTest.php

@@ -996,4 +996,25 @@ class EntityTest extends TestCase {
 		$this->assertEquals(json_encode($entity, JSON_PRETTY_PRINT), (string)$entity);
 	}
 
+/**
+ * Tests __debugInfo
+ *
+ * @return void
+ */
+	public function testDebugInfo() {
+		$entity = new Entity(['foo' => 'bar'], ['markClean' => true]);
+		$entity->accessible('name', true);
+		$entity->virtualProperties(['baz']);
+		$entity->errors('foo', ['An error']);
+		$result = $entity->__debugInfo();
+		$expected = [
+			'new' => true,
+			'accessible' => ['name'],
+			'properties' => ['foo' => 'bar'],
+			'dirty' => ['foo' => true],
+			'virtual' => ['baz'],
+			'errors' => ['foo' => ['An error']]
+		];
+	}
+
 }