Test Setup Failed
Push — develop ( 082d66...6f26e1 )
by Guilherme
63:04
created

ProxyFactoryTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 25
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Proxy;
6
7
use Doctrine\ORM\Mapping\ClassMetadataBuildingContext;
8
use Doctrine\ORM\Mapping\ClassMetadataFactory;
9
use Doctrine\ORM\Proxy\Factory\DefaultProxyResolver;
10
use Doctrine\ORM\Proxy\Factory\ProxyFactory;
11
use Doctrine\ORM\Configuration\ProxyConfiguration;
12
use Doctrine\ORM\EntityNotFoundException;
13
use Doctrine\ORM\Mapping\ClassMetadata;
14
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister;
15
use Doctrine\ORM\Proxy\Factory\StaticProxyFactory;
16
use Doctrine\ORM\Reflection\RuntimeReflectionService;
17
use Doctrine\Tests\Mocks\ConnectionMock;
18
use Doctrine\Tests\Mocks\DriverMock;
19
use Doctrine\Tests\Mocks\EntityManagerMock;
20
use Doctrine\Tests\Mocks\UnitOfWorkMock;
21
use Doctrine\Tests\Models\Company\CompanyEmployee;
22
use Doctrine\Tests\Models\Company\CompanyPerson;
23
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
24
use Doctrine\Tests\OrmTestCase;
25
26
/**
27
 * Test the proxy generator. Its work is generating on-the-fly subclasses of a given model, which implement the Proxy pattern.
28
 * @author Giorgio Sironi <[email protected]>
29
 */
30
class ProxyFactoryTest extends OrmTestCase
31
{
32
    /**
33
     * @var ConnectionMock
34
     */
35
    private $connectionMock;
36
37
    /**
38
     * @var UnitOfWorkMock
39
     */
40
    private $uowMock;
41
42
    /**
43
     * @var EntityManagerMock
44
     */
45
    private $emMock;
46
47
    /**
48
     * @var \Doctrine\ORM\Proxy\ProxyFactory
49
     */
50
    private $proxyFactory;
51
52
    /**
53
     * @var ClassMetadataBuildingContext
54
     */
55
    private $metadataBuildingContext;
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    protected function setUp()
61
    {
62
        parent::setUp();
63
64
        $this->metadataBuildingContext = new ClassMetadataBuildingContext(
65
            $this->createMock(ClassMetadataFactory::class)
66
        );
67
        $this->connectionMock          = new ConnectionMock([], new DriverMock());
68
        $this->emMock                  = EntityManagerMock::create($this->connectionMock);
69
        $this->uowMock                 = new UnitOfWorkMock($this->emMock);
70
71
        $this->emMock->setUnitOfWork($this->uowMock);
72
73
        $proxyConfiguration = new ProxyConfiguration();
74
75
        $proxyConfiguration->setDirectory(sys_get_temp_dir());
76
        $proxyConfiguration->setNamespace('Proxies');
77
        $proxyConfiguration->setAutoGenerate(ProxyFactory::AUTOGENERATE_ALWAYS);
78
        $proxyConfiguration->setResolver(new DefaultProxyResolver(
79
            $proxyConfiguration->getNamespace(),
80
            $proxyConfiguration->getDirectory()
81
        ));
82
83
        $this->proxyFactory = new StaticProxyFactory($this->emMock, $proxyConfiguration);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\ORM\Proxy\...k, $proxyConfiguration) of type object<Doctrine\ORM\Prox...ory\StaticProxyFactory> is incompatible with the declared type object<Doctrine\ORM\Proxy\ProxyFactory> of property $proxyFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
84
    }
85
86
    public function testReferenceProxyDelegatesLoadingToThePersister()
87
    {
88
        $identifier = ['id' => 42];
89
        $proxyClass = 'Proxies\__CG__\Doctrine\Tests\Models\ECommerce\ECommerceFeature';
90
91
        $classMetaData = $this->emMock->getClassMetadata(ECommerceFeature::class);
92
93
        $persister = $this
94
            ->getMockBuilder(BasicEntityPersister::class)
95
            ->setConstructorArgs([$this->emMock, $classMetaData])
96
            ->setMethods(['load'])
97
            ->getMock();
98
99
        $persister
100
            ->expects($this->atLeastOnce())
101
            ->method('load')
102
            ->with($this->equalTo($identifier), $this->isInstanceOf($proxyClass))
103
            ->will($this->returnValue(new \stdClass()));
104
105
        $this->uowMock->setEntityPersister(ECommerceFeature::class, $persister);
106
107
        $proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, $identifier);
108
109
        $proxy->getDescription();
110
    }
111
112
    /**
113
     * @group DDC-1771
114
     */
115
    public function testSkipAbstractClassesOnGeneration()
116
    {
117
        $cm = new ClassMetadata(AbstractClass::class, $this->metadataBuildingContext);
118
        $cm->initializeReflection(new RuntimeReflectionService());
119
120
        self::assertNotNull($cm->getReflectionClass());
121
122
        $num = $this->proxyFactory->generateProxyClasses([$cm]);
123
124
        self::assertEquals(0, $num, "No proxies generated.");
125
    }
126
127
    /**
128
     * @group DDC-2432
129
     */
130 View Code Duplication
    public function testFailedProxyLoadingDoesNotMarkTheProxyAsInitialized()
131
    {
132
        $classMetaData = $this->emMock->getClassMetadata(ECommerceFeature::class);
133
134
        $persister = $this
135
            ->getMockBuilder(BasicEntityPersister::class)
136
            ->setConstructorArgs([$this->emMock, $classMetaData])
137
            ->setMethods(['load'])
138
            ->getMock();
139
140
        $persister
141
            ->expects($this->atLeastOnce())
142
            ->method('load')
143
            ->will($this->returnValue(null));
144
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
        try {
151
            $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...
152
            $this->fail('An exception was expected to be raised');
153
        } catch (EntityNotFoundException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
154
        }
155
156
        self::assertFalse($proxy->__isInitialized());
157
    }
158
159
    /**
160
     * @group DDC-2432
161
     */
162 View Code Duplication
    public function testFailedProxyCloningDoesNotMarkTheProxyAsInitialized()
163
    {
164
        $classMetaData = $this->emMock->getClassMetadata(ECommerceFeature::class);
165
166
        $persister = $this
167
            ->getMockBuilder(BasicEntityPersister::class)
168
            ->setConstructorArgs([$this->emMock, $classMetaData])
169
            ->setMethods(['load'])
170
            ->getMock();
171
172
        $persister
173
            ->expects($this->atLeastOnce())
174
            ->method('load')
175
            ->will($this->returnValue(null));
176
177
        $this->uowMock->setEntityPersister(ECommerceFeature::class, $persister);
178
179
        /* @var $proxy \Doctrine\Common\Proxy\Proxy */
180
        $proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, ['id' => 42]);
181
182
        try {
183
            $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...
184
            $this->fail('An exception was expected to be raised');
185
        } catch (EntityNotFoundException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
186
        }
187
188
        self::assertFalse($proxy->__isInitialized());
189
    }
190
191
    public function testProxyClonesParentFields()
192
    {
193
        $companyEmployee = new CompanyEmployee();
194
        $companyEmployee->setSalary(1000); // A property on the CompanyEmployee
195
        $companyEmployee->setName('Bob'); // A property on the parent class, CompanyPerson
196
197
        // Set the id of the CompanyEmployee (which is in the parent CompanyPerson)
198
        $property = new \ReflectionProperty(CompanyPerson::class, 'id');
199
200
        $property->setAccessible(true);
201
        $property->setValue($companyEmployee, 42);
202
203
        $classMetaData = $this->emMock->getClassMetadata(CompanyEmployee::class);
204
205
        $persister = $this
206
            ->getMockBuilder(BasicEntityPersister::class)
207
            ->setConstructorArgs([$this->emMock, $classMetaData])
208
            ->setMethods(['load'])
209
            ->getMock();
210
211
        $persister
212
            ->expects(self::atLeastOnce())
213
            ->method('load')
214
            ->willReturn($companyEmployee);
215
216
        $this->uowMock->setEntityPersister(CompanyEmployee::class, $persister);
217
218
        /* @var $proxy \Doctrine\Common\Proxy\Proxy */
219
        $proxy = $this->proxyFactory->getProxy(CompanyEmployee::class, ['id' => 42]);
220
221
        /* @var $cloned CompanyEmployee */
222
        $cloned = clone $proxy;
223
224
        self::assertSame(42, $cloned->getId(), 'Expected the Id to be cloned');
225
        self::assertSame(1000, $cloned->getSalary(), 'Expect properties on the CompanyEmployee class to be cloned');
226
        self::assertSame('Bob', $cloned->getName(), 'Expect properties on the CompanyPerson class to be cloned');
227
    }
228
}
229
230
abstract class AbstractClass
231
{
232
233
}
234