Skip to content

Commit

Permalink
Added hasId()
Browse files Browse the repository at this point in the history
  • Loading branch information
leogr committed Jul 31, 2015
1 parent 49279be commit 2ff080c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 11 additions & 1 deletion library/Criteria/ActiveRecord/AbstractCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,21 @@ public function setId($id)
*/
public function getId()
{
if (!$this->id) {
if (null === $this->id) {
throw new Exception\RuntimeException(
'getId(), apply() and applyDelete() require that an id must be present using a prior call to setId()'
);
}
return $this->id;
}

/**
* Check if Id has been set
*
* @return boolean
*/
public function hasId()
{
return $this->id !== null;
}
}
9 changes: 8 additions & 1 deletion tests/Criteria/ActiveRecord/AbstractCriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ public function testShouldThrowExceptionWhenNoId()
/**
* @depends testShouldThrowExceptionWhenNoId
*/
public function testGetSetId()
public function testGetSetHasId()
{
$this->assertFalse($this->criteria->hasId());

$id = 'foo';
$this->assertInstanceOf(
'\MatryoshkaTest\Model\Criteria\ActiveRecord\TestAsset\ConcreteCriteria',
$this->criteria->setId($id)
);
$this->assertSame($id, $this->criteria->getId());

$this->assertTrue($this->criteria->hasId());
$this->criteria->setId(null);
$this->assertFalse($this->criteria->hasId());
}

}

0 comments on commit 2ff080c

Please sign in to comment.