-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ProPheT777/feature-2
Add empty collection filter
- Loading branch information
Showing
4 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/DeepCopy/Filter/Doctrine/DoctrineEmptyCollectionFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace DeepCopy\Filter\Doctrine; | ||
|
||
use DeepCopy\Filter\Filter; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
|
||
class DoctrineEmptyCollectionFilter implements Filter | ||
{ | ||
/** | ||
* Apply the filter to the object. | ||
* | ||
* @param object $object | ||
* @param string $property | ||
* @param callable $objectCopier | ||
*/ | ||
public function apply($object, $property, $objectCopier) | ||
{ | ||
$reflectionProperty = new \ReflectionProperty($object, $property); | ||
$reflectionProperty->setAccessible(true); | ||
|
||
$reflectionProperty->setValue($object, new ArrayCollection()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/DeepCopyTest/Filter/Doctrine/DoctrineEmptyCollectionFilterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace DeepCopyTest\Filter\Doctrine; | ||
|
||
use DeepCopy\DeepCopy; | ||
use DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter; | ||
use DeepCopy\Matcher\PropertyMatcher; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
|
||
/** | ||
* Test Doctrine Collection filter | ||
*/ | ||
class DoctrineEmptyCollectionFilterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testApply() | ||
{ | ||
$object = new \StdClass(); | ||
|
||
$collection = new ArrayCollection(); | ||
$collection->add(new \StdClass()); | ||
|
||
$object->foo = $collection; | ||
|
||
$filter = new DoctrineEmptyCollectionFilter(); | ||
$filter->apply($object, 'foo', function($item){ return null; }); | ||
|
||
$this->assertTrue($object->foo instanceof Collection); | ||
$this->assertNotSame($collection, $object->foo); | ||
$this->assertTrue($object->foo->isEmpty()); | ||
} | ||
|
||
public function testIntegration() | ||
{ | ||
//Prepare object to copy | ||
$doctrineEmptyCollectionFixture = new \StdClass(); | ||
$originalCollection = new ArrayCollection(); | ||
$originalCollection->add(new \StdClass()); | ||
$doctrineEmptyCollectionFixture->foo = $originalCollection; | ||
|
||
//Copy | ||
$deepCopy = new DeepCopy(); | ||
$deepCopy->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher(get_class($doctrineEmptyCollectionFixture), 'foo')); | ||
$copied = $deepCopy->copy($doctrineEmptyCollectionFixture); | ||
|
||
//Check result | ||
$this->assertTrue($copied->foo instanceof Collection); | ||
$this->assertNotSame($originalCollection, $copied->foo); | ||
$this->assertTrue($copied->foo->isEmpty()); | ||
} | ||
} |