Skip to content

Commit

Permalink
Merge pull request #4 from eliseacornejo/EmptyEntities
Browse files Browse the repository at this point in the history
Checking entity is not empty
  • Loading branch information
OscarMerino committed Mar 4, 2016
2 parents d14c8dc + 52c216e commit 0339bfa
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ private function remove()

$this->persister->beginTransaction();
foreach ($this as $entity) {
$this->persister->remove($entity);
if (!empty($entity)) {
$this->persister->remove($entity);
}
}

$this->persister->commitTransaction();
Expand All @@ -147,7 +149,9 @@ public function persist()

$this->persister->beginTransaction();
foreach ($this as $entity) {
$this->persister->persist($entity);
if (!empty($entity)){
$this->persister->persist($entity);
}
}

$this->persister->commitTransaction();
Expand All @@ -165,7 +169,9 @@ public function reload()
$this->checkPersister();

foreach ($this as $key => $entity) {
$this[$key] = $this->persister->reload($entity);
if (!empty($entity)) {
$this[$key] = $this->persister->reload($entity);
}
}

return $this;
Expand Down
52 changes: 52 additions & 0 deletions tests/RegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ public function testPersist()
$registry->persist();
}


public function testPersistNull()
{
$firstEntity = null;

$persister = $this->getMock('\eBayEnterprise\Behat\RegistryExtension\Persister');
$persister->expects($this->at(0))
->method('beginTransaction');

$persister->expects($this->never())
->method('persist');

$persister->expects($this->at(1))
->method('commitTransaction');

$registry = new Registry($persister);
$registry->append($firstEntity);
$registry->persist();
}

public function testPersistWithoutPersister()
{
$this->setExpectedException('InvalidArgumentException');
Expand Down Expand Up @@ -78,6 +98,25 @@ public function testCleanWithoutPersister()
$this->assertEquals(array(), $registry->getArrayCopy());
}

public function testCleanNull()
{
$firstEntity = null;

$persister = $this->getMock('\eBayEnterprise\Behat\RegistryExtension\Persister');
$persister->expects($this->at(0))
->method('beginTransaction');

$persister->expects($this->never())
->method('remove');

$persister->expects($this->at(1))
->method('commitTransaction');

$registry = new Registry($persister);
$registry->append($firstEntity);
$registry->reset();
}

public function testReload()
{
$oldEntity = new \stdClass();
Expand All @@ -96,6 +135,19 @@ public function testReload()
$this->assertSame($newEntity, $registry->findOne(get_class($oldEntity)));
}

public function testReloadNull()
{
$oldEntity = null;

$persister = $this->getMock('\eBayEnterprise\Behat\RegistryExtension\Persister');
$persister->expects($this->never())
->method('reload');

$registry = new Registry($persister);
$registry->append($oldEntity);
$registry->reload();
}

public function testMergeWithArray()
{
$firstEntity = new \stdClass();
Expand Down

0 comments on commit 0339bfa

Please sign in to comment.