Failed Conditions
Pull Request — master (#6626)
by Issei
18:33
created

ProxyFactoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Proxy;
4
5
use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService;
6
use Doctrine\Common\Proxy\AbstractProxyFactory;
7
use Doctrine\ORM\EntityNotFoundException;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister;
10
use Doctrine\ORM\Proxy\ProxyFactory;
11
use Doctrine\Tests\Mocks\ConnectionMock;
12
use Doctrine\Tests\Mocks\DriverMock;
13
use Doctrine\Tests\Mocks\EntityManagerMock;
14
use Doctrine\Tests\Mocks\UnitOfWorkMock;
15
use Doctrine\Tests\Models\Company\CompanyEmployee;
16
use Doctrine\Tests\Models\Company\CompanyPerson;
17
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
18
use Doctrine\Tests\OrmTestCase;
19
20
/**
21
 * Test the proxy generator. Its work is generating on-the-fly subclasses of a given model, which implement the Proxy pattern.
22
 * @author Giorgio Sironi <[email protected]>
23
 */
24
class ProxyFactoryTest extends OrmTestCase
25
{
26
    /**
27
     * @var ConnectionMock
28
     */
29
    private $connectionMock;
30
31
    /**
32
     * @var UnitOfWorkMock
33
     */
34
    private $uowMock;
35
36
    /**
37
     * @var EntityManagerMock
38
     */
39
    private $emMock;
40
41
    /**
42
     * @var \Doctrine\ORM\Proxy\ProxyFactory
43
     */
44
    private $proxyFactory;
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    protected function setUp()
50
    {
51
        parent::setUp();
52
        $this->connectionMock = new ConnectionMock([], new DriverMock());
53
        $this->emMock = EntityManagerMock::create($this->connectionMock);
54
        $this->uowMock = new UnitOfWorkMock($this->emMock);
55
        $this->emMock->setUnitOfWork($this->uowMock);
56
        $this->proxyFactory = new ProxyFactory($this->emMock, sys_get_temp_dir(), 'Proxies', AbstractProxyFactory::AUTOGENERATE_ALWAYS);
57
    }
58
59
    public function testReferenceProxyDelegatesLoadingToThePersister()
60
    {
61
        $identifier = ['id' => 42];
62
        $proxyClass = 'Proxies\__CG__\Doctrine\Tests\Models\ECommerce\ECommerceFeature';
63
        $persister  = $this->getMockBuilder(BasicEntityPersister::class)->setMethods(['load'])->disableOriginalConstructor()->getMock();
64
65
        $this->uowMock->setEntityPersister(ECommerceFeature::class, $persister);
66
67
        $proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, $identifier);
68
69
        $persister
70
            ->expects($this->atLeastOnce())
71
            ->method('load')
72
            ->with($this->equalTo($identifier), $this->isInstanceOf($proxyClass))
73
            ->will($this->returnValue(new \stdClass()));
74
75
        $proxy->getDescription();
0 ignored issues
show
Bug introduced by
The method getDescription() does not seem to exist on object<Doctrine\Common\Proxy\Proxy>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
    }
77
78 View Code Duplication
    public function testSkipMappedSuperClassesOnGeneration()
79
    {
80
        $cm = new ClassMetadata(\stdClass::class);
81
        $cm->isMappedSuperclass = true;
82
83
        $num = $this->proxyFactory->generateProxyClasses([$cm]);
84
85
        $this->assertEquals(0, $num, "No proxies generated.");
86
    }
87
88 View Code Duplication
    public function testSkipEmbeddableClassesOnGeneration()
89
    {
90
        $cm = new ClassMetadata(\stdClass::class);
91
        $cm->isEmbeddedClass = true;
92
93
        $num = $this->proxyFactory->generateProxyClasses([$cm]);
94
95
        $this->assertEquals(0, $num, "No proxies generated.");
96
    }
97
98
    /**
99
     * @group DDC-1771
100
     */
101
    public function testSkipAbstractClassesOnGeneration()
102
    {
103
        $cm = new ClassMetadata(AbstractClass::class);
104
        $cm->initializeReflection(new RuntimeReflectionService());
105
        $this->assertNotNull($cm->reflClass);
106
107
        $num = $this->proxyFactory->generateProxyClasses([$cm]);
108
109
        $this->assertEquals(0, $num, "No proxies generated.");
110
    }
111
112
    /**
113
     * @group DDC-2432
114
     */
115 View Code Duplication
    public function testFailedProxyLoadingDoesNotMarkTheProxyAsInitialized()
116
    {
117
        $persister = $this->getMockBuilder(BasicEntityPersister::class)->setMethods(['load'])->disableOriginalConstructor()->getMock();
118
        $this->uowMock->setEntityPersister(ECommerceFeature::class, $persister);
119
120
        /* @var $proxy \Doctrine\Common\Proxy\Proxy */
121
        $proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, ['id' => 42]);
122
123
        $persister
124
            ->expects($this->atLeastOnce())
125
            ->method('load')
126
            ->will($this->returnValue(null));
127
128
        try {
129
            $proxy->getDescription();
0 ignored issues
show
Bug introduced by
The method getDescription() does not seem to exist on object<Doctrine\Common\Proxy\Proxy>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
            $this->fail('An exception was expected to be raised');
131
        } catch (EntityNotFoundException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
132
        }
133
134
        $this->assertFalse($proxy->__isInitialized());
135
        $this->assertInstanceOf('Closure', $proxy->__getInitializer(), 'The initializer wasn\'t removed');
136
        $this->assertInstanceOf('Closure', $proxy->__getCloner(), 'The cloner wasn\'t removed');
137
    }
138
139
    /**
140
     * @group DDC-2432
141
     */
142 View Code Duplication
    public function testFailedProxyCloningDoesNotMarkTheProxyAsInitialized()
143
    {
144
        $persister = $this->getMockBuilder(BasicEntityPersister::class)->setMethods(['load'])->disableOriginalConstructor()->getMock();
145
        $this->uowMock->setEntityPersister(ECommerceFeature::class, $persister);
146
147
        /* @var $proxy \Doctrine\Common\Proxy\Proxy */
148
        $proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, ['id' => 42]);
149
150
        $persister
151
            ->expects($this->atLeastOnce())
152
            ->method('load')
153
            ->will($this->returnValue(null));
154
155
        try {
156
            $cloned = clone $proxy;
0 ignored issues
show
Unused Code introduced by
$cloned is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
157
            $this->fail('An exception was expected to be raised');
158
        } catch (EntityNotFoundException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
159
        }
160
161
        $this->assertFalse($proxy->__isInitialized());
162
        $this->assertInstanceOf('Closure', $proxy->__getInitializer(), 'The initializer wasn\'t removed');
163
        $this->assertInstanceOf('Closure', $proxy->__getCloner(), 'The cloner wasn\'t removed');
164
    }
165
166
    public function testProxyClonesParentFields()
167
    {
168
        $companyEmployee = new CompanyEmployee();
169
        $companyEmployee->setSalary(1000); // A property on the CompanyEmployee
170
        $companyEmployee->setName('Bob'); // A property on the parent class, CompanyPerson
171
172
        // Set the id of the CompanyEmployee (which is in the parent CompanyPerson)
173
        $property = new \ReflectionProperty(CompanyPerson::class, 'id');
174
175
        $property->setAccessible(true);
176
        $property->setValue($companyEmployee, 42);
177
178
        $classMetaData = $this->emMock->getClassMetadata(CompanyEmployee::class);
179
180
        $persister = $this
181
            ->getMockBuilder(BasicEntityPersister::class)
182
            ->setMethods(['load', 'getClassMetadata'])
183
            ->disableOriginalConstructor()
184
            ->getMock();
185
        $this->uowMock->setEntityPersister(CompanyEmployee::class, $persister);
186
187
        /* @var $proxy \Doctrine\Common\Proxy\Proxy */
188
        $proxy = $this->proxyFactory->getProxy(CompanyEmployee::class, ['id' => 42]);
189
190
        $persister
191
            ->expects(self::atLeastOnce())
192
            ->method('load')
193
            ->willReturn($companyEmployee);
194
195
        $persister
196
            ->expects(self::atLeastOnce())
197
            ->method('getClassMetadata')
198
            ->willReturn($classMetaData);
199
200
        /* @var $cloned CompanyEmployee */
201
        $cloned = clone $proxy;
202
203
        self::assertSame(42, $cloned->getId(), 'Expected the Id to be cloned');
204
        self::assertSame(1000, $cloned->getSalary(), 'Expect properties on the CompanyEmployee class to be cloned');
205
        self::assertSame('Bob', $cloned->getName(), 'Expect properties on the CompanyPerson class to be cloned');
206
    }
207
}
208
209
abstract class AbstractClass
210
{
211
212
}
213