Completed
Push — master ( cd2604...399c1f )
by Matthieu
03:36
created

Filter/Doctrine/DoctrineCollectionFilterTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace DeepCopyTest\Filter\Doctrine;
4
5
use DeepCopy\DeepCopy;
6
use DeepCopy\Filter\Doctrine\DoctrineCollectionFilter;
7
use DeepCopy\Matcher\PropertyMatcher;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\Common\Collections\Collection;
10
11
/**
12
 * Test Doctrine Collection filter
13
 */
14
class DoctrineCollectionFilterTest extends \PHPUnit_Framework_TestCase
15
{
16
    public function testApply()
17
    {
18
        $object = new \stdClass();
19
        $oldCollection = new ArrayCollection();
20
        $oldCollection->add(new \stdClass());
21
        $object->foo = $oldCollection;
22
23
        $filter = new DoctrineCollectionFilter();
24
        $filter->apply($object, 'foo', function($item) {
0 ignored issues
show
The parameter $item is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
                return null;
26
            });
27
28
        $this->assertTrue($object->foo instanceof Collection);
29
        $this->assertNotSame($oldCollection, $object->foo);
30
        $this->assertCount(1, $object->foo);
31
    }
32
33
    public function testIntegration()
34
    {
35
        $o = new DoctrineCollectionFilterTestFixture();
36
        $oldCollection = new ArrayCollection();
37
        $oldCollectionItem = new \stdClass();
38
        $oldCollection->add($oldCollectionItem);
39
        $o->property1 = $oldCollection;
40
41
        $deepCopy = new DeepCopy();
42
        $deepCopy->addFilter(new DoctrineCollectionFilter(), new PropertyMatcher(get_class($o), 'property1'));
43
        /** @var DoctrineCollectionFilterTestFixture $new */
44
        $new = $deepCopy->copy($o);
45
46
        $this->assertTrue($new->property1 instanceof Collection);
47
        $this->assertNotSame($oldCollection, $new->property1);
48
        $this->assertCount(1, $new->property1);
49
        $newItem = $new->property1[0];
50
        $this->assertNotSame($oldCollectionItem, $newItem);
51
    }
52
}
53
54
class DoctrineCollectionFilterTestFixture
55
{
56
    public $property1;
57
}
58