Failed Conditions
Pull Request — 2.6 (#7506)
by
unknown
09:52
created

testMergeWorksOnNonSerializedProxies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 20
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\ORM\Proxy\Proxy;
6
use Doctrine\Tests\Models\CMS\CmsGroup;
7
use Doctrine\Tests\VerifyDeprecations;
8
9
class DDC1734Test extends \Doctrine\Tests\OrmFunctionalTestCase
10
{
11
    use VerifyDeprecations;
12
13
    /**
14
     * {@inheritDoc}
15
     */
16
    protected function setUp()
17
    {
18
        $this->useModelSet('cms');
19
        parent::setUp();
20
    }
21
22
    /** @after */
23
    public function ensureTestGeneratedDeprecationMessages() : void
24
    {
25
        $this->assertHasDeprecationMessages();
26
    }
27
28
    /**
29
     * This test is DDC-1734 minus the serialization, i.e. it works
30
     *
31
     * @group DDC-1734
32
     */
33
    public function testMergeWorksOnNonSerializedProxies()
34
    {
35
        $group = new CmsGroup();
36
37
        $group->setName('Foo');
38
        $this->_em->persist($group);
39
        $this->_em->flush();
40
        $this->_em->clear();
41
42
        $proxy = $this->getProxy($group);
43
44
        $this->assertInstanceOf(Proxy::class, $proxy);
45
        $this->assertFalse($proxy->__isInitialized());
46
47
        $this->_em->detach($proxy);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::detach() has been deprecated: 2.7 This method is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

47
        /** @scrutinizer ignore-deprecated */ $this->_em->detach($proxy);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
48
        $this->_em->clear();
49
50
        $proxy = $this->_em->merge($proxy);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::merge() has been deprecated: 2.7 This method is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

50
        $proxy = /** @scrutinizer ignore-deprecated */ $this->_em->merge($proxy);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
51
52
        $this->assertEquals('Foo', $proxy->getName(), 'The entity is broken');
53
    }
54
55
    /**
56
     * This test reproduces DDC-1734 which is:
57
     * - A non-initialized proxy is detached and serialized (the identifier of the proxy is *not* serialized)
58
     * - the object is deserialized and merged (to turn into an entity)
59
     * - the entity is broken because it has no identifier and no field defined
60
     *
61
     * @group DDC-1734
62
     */
63
    public function testMergeWorksOnSerializedProxies()
64
    {
65
        $group = new CmsGroup();
66
67
        $group->setName('Foo');
68
        $this->_em->persist($group);
69
        $this->_em->flush();
70
        $this->_em->clear();
71
72
        $proxy = $this->getProxy($group);
73
74
        $this->assertInstanceOf(Proxy::class, $proxy);
75
        $this->assertFalse($proxy->__isInitialized());
76
77
        $this->_em->detach($proxy);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::detach() has been deprecated: 2.7 This method is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
        /** @scrutinizer ignore-deprecated */ $this->_em->detach($proxy);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
78
        $serializedProxy = serialize($proxy);
79
        $this->_em->clear();
80
81
        $unserializedProxy = $this->_em->merge(unserialize($serializedProxy));
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::merge() has been deprecated: 2.7 This method is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

81
        $unserializedProxy = /** @scrutinizer ignore-deprecated */ $this->_em->merge(unserialize($serializedProxy));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
82
        $this->assertEquals('Foo', $unserializedProxy->getName(), 'The entity is broken');
83
    }
84
85
    /**
86
     * @param object $object
87
     *
88
     * @return \Doctrine\Common\Proxy\Proxy
89
     */
90
    private function getProxy($object)
91
    {
92
        $metadataFactory = $this->_em->getMetadataFactory();
93
        $className       = get_class($object);
94
        $identifier      = $metadataFactory->getMetadataFor($className)->getIdentifierValues($object);
95
96
        return $this->_em->getProxyFactory()->getProxy($className, $identifier);
97
    }
98
99
}
100