Skip to content
Permalink
Browse files

Making Entity::has() return false on null values

  • Loading branch information...
lorenzo committed Oct 6, 2013
1 parent 9f0160d commit 90dc98622a351292d3abf50b6e3a840c76c130df
Showing with 10 additions and 6 deletions.
  1. +2 −3 Cake/ORM/Entity.php
  2. +8 −3 Cake/Test/TestCase/ORM/EntityTest.php
@@ -158,16 +158,15 @@ public function &get($property) {
* {{{
* $entity = new Entity(['id' => 1, 'name' => null]);
* $entity->has('id'); // true
* $entity->has('name'); // true
* $entity->has('name'); // false
* $entity->has('last_name'); // false
* }}}
*
* @param string $property
* @return boolean
*/
public function has($property) {
$set = array_key_exists($property, $this->_properties);
return $set || method_exists($this, 'get' . ucFirst($property));
return $this->get($property) !== null;
}
/**
@@ -251,8 +251,13 @@ public function testHas() {
$entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
$this->assertTrue($entity->has('id'));
$this->assertTrue($entity->has('name'));
$this->assertTrue($entity->has('foo'));
$this->assertFalse($entity->has('foo'));
$this->assertFalse($entity->has('last_name'));
$entity = $this->getMock('\Cake\ORM\Entity', ['getThings']);
$entity->expects($this->once())->method('getThings')
->will($this->returnValue(0));
$this->assertTrue($entity->has('things'));
}
/**
@@ -291,7 +296,7 @@ public function testMagicIsset() {
$entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
$this->assertTrue(isset($entity->id));
$this->assertTrue(isset($entity->name));
$this->assertTrue(isset($entity->foo));
$this->assertFalse(isset($entity->foo));
$this->assertFalse(isset($entity->thing));
}
@@ -317,7 +322,7 @@ public function testIssetArrayAccess() {
$entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
$this->assertTrue(isset($entity['id']));
$this->assertTrue(isset($entity['name']));
$this->assertTrue(isset($entity['foo']));
$this->assertFalse(isset($entity['foo']));
$this->assertFalse(isset($entity['thing']));
}

0 comments on commit 90dc986

Please sign in to comment.
You can’t perform that action at this time.