Permalink
Browse files

Merge pull request #6 from jjbohn/feature/silent-remove-restore

No-op on delete or restore on previously scheduled documents.
  • Loading branch information...
2 parents f43f8de + d80621b commit 31dfa56a8f40a10696f10510ecaee73333cc18bd @jmikola jmikola committed Mar 20, 2012
@@ -27,7 +27,7 @@
/**
* The SoftDeleteManager class is responsible for managing the deleted state of a SoftDeleteable instance.
- *
+ *
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 1.0
@@ -220,13 +220,12 @@ public function restoreBy($className, array $criteria, array $flags = array())
* Schedules a SoftDeleteable document instance for deletion on next flush.
*
* @param SoftDeleteable $document
- * @throws InvalidArgumentException
*/
public function delete(SoftDeleteable $document)
{
$oid = spl_object_hash($document);
if (isset($this->documentDeletes[$oid])) {
- throw new \InvalidArgumentException('Document is already scheduled for delete.');
+ return;
}
// If scheduled for restore then remove it
@@ -243,13 +242,12 @@ public function delete(SoftDeleteable $document)
* Schedules a SoftDeleteable document instance for restoration on next flush.
*
* @param SoftDeleteable $document
- * @throws InvalidArgumentException
*/
public function restore(SoftDeleteable $document)
{
$oid = spl_object_hash($document);
if (isset($this->documentRestores[$oid])) {
- throw new \InvalidArgumentException('Document is already scheduled for restore.');
+ return;
}
// If scheduled for delete then remove it
@@ -10,47 +10,51 @@
class SoftDeleteManagerTest extends PHPUnit_Framework_TestCase
{
+ private $dm;
+ private $configuration;
+ private $eventManager;
+ private $softDeleteable;
+ private $sdm;
+
+ protected function setUp()
+ {
+ $this->dm = $this->getMockDocumentManager();
+ $this->configuration = $this->getMockConfiguration();
+ $this->eventManager = $this->getMockEventManager();
+ $this->softDeleteable = $this->getMockSoftDeletable();
+ $this->sdm = new SoftDeleteManager($this->dm, $this->configuration, $this->eventManager);
+ }
+
public function testConstructor()
{
- $dm = $this->getMockDocumentManager();
- $configuration = $this->getMockConfiguration();
- $eventManager = $this->getMockEventManager();
- $uow = $this->getTestSoftDeleteManager($dm, $configuration, $eventManager);
- $this->assertSame($dm, $uow->getDocumentManager());
+ $this->assertSame($this->dm, $this->sdm->getDocumentManager());
+ $this->assertSame($this->configuration, $this->sdm->getConfiguration());
+ $this->assertSame($this->eventManager, $this->sdm->getEventManager());
}
public function testDelete()
{
- $mockSoftDeleteable = $this->getMockSoftDeletable();
- $dm = $this->getMockDocumentManager();
- $configuration = $this->getMockConfiguration();
- $eventManager = $this->getMockEventManager();
- $uow = $this->getTestSoftDeleteManager($dm, $configuration, $eventManager);
- $uow->delete($mockSoftDeleteable);
- $this->assertTrue($uow->isScheduledForDelete($mockSoftDeleteable));
+ $this->sdm->delete($this->softDeleteable);
+ $this->assertTrue($this->sdm->isScheduledForDelete($this->softDeleteable));
+
+ $deletes = $this->sdm->getDocumentDeletes();
+ $this->sdm->delete($this->softDeleteable);
+ $this->assertEquals($deletes, $this->sdm->getDocumentDeletes(), 'Delete of already scheduled document does nothing');
}
public function testRestore()
{
- $mockSoftDeleteable = $this->getMockSoftDeletable();
- $dm = $this->getMockDocumentManager();
- $configuration = $this->getMockConfiguration();
- $eventManager = $this->getMockEventManager();
- $uow = $this->getTestSoftDeleteManager($dm, $configuration, $eventManager);
- $uow->restore($mockSoftDeleteable);
- $this->assertTrue($uow->isScheduledForRestore($mockSoftDeleteable));
- }
+ $this->sdm->restore($this->softDeleteable);
+ $this->assertTrue($this->sdm->isScheduledForRestore($this->softDeleteable));
- private function getTestSoftDeleteManager(DocumentManager $dm, Configuration $configuration, EventManager $eventManager)
- {
- return new SoftDeleteManager($dm, $configuration, $eventManager);
+ $restores = $this->sdm->getDocumentRestores();
+ $this->sdm->restore($this->softDeleteable);
+ $this->assertEquals($restores, $this->sdm->getDocumentRestores(), 'Restore of already scheduled document does nothing');
}
private function getMockConfiguration()
{
- return $this->getMockBuilder('Doctrine\ODM\MongoDB\SoftDelete\Configuration')
- ->disableOriginalConstructor()
- ->getMock();
+ return $this->getMock('Doctrine\ODM\MongoDB\SoftDelete\Configuration');
}
private function getMockDocumentManager()
@@ -62,9 +66,7 @@ private function getMockDocumentManager()
private function getMockSoftDeletable()
{
- return $this->getMockBuilder('Doctrine\ODM\MongoDB\SoftDelete\SoftDeleteable')
- ->disableOriginalConstructor()
- ->getMock();
+ return $this->getMock('Doctrine\ODM\MongoDB\SoftDelete\SoftDeleteable');
}
private function getMockEventManager()

0 comments on commit 31dfa56

Please sign in to comment.