Failed Conditions
Pull Request — master (#6626)
by Issei
19:24
created

ProxyFactoryTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 188
Duplicated Lines 34.04 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 11
dl 64
loc 188
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A testReferenceProxyDelegatesLoadingToThePersister() 0 18 1
A testSkipMappedSuperClassesOnGeneration() 9 9 1
A testSkipEmbeddableClassesOnGeneration() 9 9 1
A testSkipAbstractClassesOnGeneration() 0 10 1
A testFailedProxyLoadingDoesNotMarkTheProxyAsInitialized() 23 23 2
A testFailedProxyCloningDoesNotMarkTheProxyAsInitialized() 23 23 2
B testProxyClonesParentFields() 0 41 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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