Test Failed
Pull Request — develop (#6719)
by Marco
63:23
created

ReferenceProxyTest::testWakeupCalledOnProxy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional;
6
7
use Doctrine\Common\Util\ClassUtils;
8
use Doctrine\ORM\Configuration\ProxyConfiguration;
9
use Doctrine\ORM\Proxy\Factory\DefaultProxyResolver;
10
use Doctrine\ORM\Proxy\Factory\ProxyFactory;
11
use Doctrine\ORM\Proxy\Factory\ProxyResolver;
12
use Doctrine\ORM\Proxy\Factory\StaticProxyFactory;
13
use Doctrine\Tests\Models\Company\CompanyAuction;
14
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
15
use Doctrine\Tests\Models\ECommerce\ECommerceShipping;
16
use Doctrine\Tests\OrmFunctionalTestCase;
17
use ProxyManager\Proxy\GhostObjectInterface;
18
19
/**
20
 * Tests the generation of a proxy object for lazy loading.
21
 *
22
 * @author Giorgio Sironi <[email protected]>
23
 * @author Benjamin Eberlei <[email protected]>
24
 */
25
class ReferenceProxyTest extends OrmFunctionalTestCase
26
{
27
    /**
28
     * @var ProxyResolver
29
     */
30
    private $resolver;
31
32
    /**
33
     * @var ProxyFactory
34
     */
35
    private $factory;
36
37
    protected function setUp()
38
    {
39
        $this->useModelSet('ecommerce');
40
        $this->useModelSet('company');
41
42
        parent::setUp();
43
44
        $namespace = 'Doctrine\Tests\Proxies';
45
        $directory = __DIR__ . '/../../Proxies';
46
47
        $this->resolver = new DefaultProxyResolver($namespace, $directory);
48
49
        $proxyConfiguration = new ProxyConfiguration();
50
51
        $proxyConfiguration->setDirectory($directory);
52
        $proxyConfiguration->setNamespace($namespace);
53
        $proxyConfiguration->setAutoGenerate(ProxyFactory::AUTOGENERATE_ALWAYS);
54
        $proxyConfiguration->setResolver($this->resolver);
55
56
        $this->factory = new StaticProxyFactory($this->em, $proxyConfiguration);
57
    }
58
59
    public function createProduct()
60
    {
61
        $product = new ECommerceProduct();
62
        $product->setName('Doctrine Cookbook');
63
        $this->em->persist($product);
64
65
        $this->em->flush();
66
        $this->em->clear();
67
68
        return $product->getId();
69
    }
70
71
    public function createAuction()
72
    {
73
        $event = new CompanyAuction();
74
        $event->setData('Doctrine Cookbook');
75
        $this->em->persist($event);
76
77
        $this->em->flush();
78
        $this->em->clear();
79
80
        return $event->getId();
81
    }
82
83
    public function testLazyLoadsFieldValuesFromDatabase()
84
    {
85
        $id = $this->createProduct();
86
87
        $productProxy = $this->em->getReference(ECommerceProduct::class, ['id' => $id]);
88
89
        self::assertEquals('Doctrine Cookbook', $productProxy->getName());
90
    }
91
92
    /**
93
     * @group DDC-727
94
     */
95 View Code Duplication
    public function testAccessMetatadaForProxy()
96
    {
97
        $id = $this->createProduct();
98
99
        $entity = $this->em->getReference(ECommerceProduct::class , $id);
100
        $class = $this->em->getClassMetadata(get_class($entity));
101
102
        self::assertEquals(ECommerceProduct::class, $class->getClassName());
0 ignored issues
show
Bug introduced by
The method getClassName() does not seem to exist on object<Doctrine\Common\P...\Mapping\ClassMetadata>.

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...
103
    }
104
105
    /**
106
     * @group DDC-1033
107
     */
108 View Code Duplication
    public function testReferenceFind()
109
    {
110
        $id = $this->createProduct();
111
112
        $entity = $this->em->getReference(ECommerceProduct::class , $id);
113
        $entity2 = $this->em->find(ECommerceProduct::class , $id);
114
115
        self::assertSame($entity, $entity2);
116
        self::assertEquals('Doctrine Cookbook', $entity2->getName());
117
    }
118
119
    /**
120
     * @group DDC-1033
121
     */
122
    public function testCloneProxy()
123
    {
124
        $id = $this->createProduct();
125
126
        /* @var $entity ECommerceProduct */
127
        $entity = $this->em->getReference(ECommerceProduct::class , $id);
128
129
        /* @var $clone ECommerceProduct */
130
        $clone = clone $entity;
131
132
        self::assertEquals($id, $entity->getId());
133
        self::assertEquals('Doctrine Cookbook', $entity->getName());
134
135
        self::assertFalse($this->em->contains($clone), "Cloning a reference proxy should return an unmanaged/detached entity.");
136
        self::assertTrue($this->em->contains($entity), "Real instance should be managed");
137
        self::assertEquals($id, $clone->getId(), "Cloning a reference proxy should return same id.");
138
        self::assertEquals('Doctrine Cookbook', $clone->getName(), "Cloning a reference proxy should return same product name.");
139
        self::assertEquals('Doctrine Cookbook', $entity->getName(), "Real instance should contain the real data too");
140
141
        // domain logic, Product::__clone sets isCloned public property
142
        self::assertTrue($clone->isCloned);
143
        self::assertFalse($entity->isCloned);
144
    }
145
146
    /**
147
     * @group DDC-733
148
     */
149
    public function testInitializeProxy()
150
    {
151
        $id = $this->createProduct();
152
153
        /* @var $entity ECommerceProduct|GhostObjectInterface */
154
        $entity = $this->em->getReference(ECommerceProduct::class , $id);
155
156
        self::assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy.");
0 ignored issues
show
Bug introduced by
The method isProxyInitialized does only exist in ProxyManager\Proxy\GhostObjectInterface, but not in Doctrine\Tests\Models\ECommerce\ECommerceProduct.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
157
158
        $this->em->getUnitOfWork()->initializeObject($entity);
159
160
        self::assertTrue($entity->isProxyInitialized(), "Should be initialized after called UnitOfWork::initializeObject()");
161
    }
162
163
    /**
164
     * @group DDC-1163
165
     */
166 View Code Duplication
    public function testInitializeChangeAndFlushProxy()
167
    {
168
        $id = $this->createProduct();
169
170
        /* @var $entity ECommerceProduct|GhostObjectInterface */
171
        $entity = $this->em->getReference(ECommerceProduct::class , $id);
172
173
        $entity->setName('Doctrine 2 Cookbook');
0 ignored issues
show
Bug introduced by
The method setName does only exist in Doctrine\Tests\Models\ECommerce\ECommerceProduct, but not in ProxyManager\Proxy\GhostObjectInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
174
175
        $this->em->flush();
176
        $this->em->clear();
177
178
        /* @var $entity ECommerceProduct|GhostObjectInterface */
179
        $entity = $this->em->getReference(ECommerceProduct::class , $id);
180
181
        self::assertEquals('Doctrine 2 Cookbook', $entity->getName());
0 ignored issues
show
Bug introduced by
The method getName does only exist in Doctrine\Tests\Models\ECommerce\ECommerceProduct, but not in ProxyManager\Proxy\GhostObjectInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
182
    }
183
184 View Code Duplication
    public function testDoNotInitializeProxyOnGettingTheIdentifier()
185
    {
186
        $id = $this->createProduct();
187
188
        /* @var $entity ECommerceProduct|GhostObjectInterface */
189
        $entity = $this->em->getReference(ECommerceProduct::class , $id);
190
191
        self::assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy.");
0 ignored issues
show
Bug introduced by
The method isProxyInitialized does only exist in ProxyManager\Proxy\GhostObjectInterface, but not in Doctrine\Tests\Models\ECommerce\ECommerceProduct.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
192
        self::assertEquals($id, $entity->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Doctrine\Tests\Models\ECommerce\ECommerceProduct, but not in ProxyManager\Proxy\GhostObjectInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
193
        self::assertFalse($entity->isProxyInitialized(), "Getting the identifier doesn't initialize the proxy.");
194
    }
195
196
    /**
197
     * @group DDC-1625
198
     */
199 View Code Duplication
    public function testDoNotInitializeProxyOnGettingTheIdentifier_DDC_1625()
200
    {
201
        $id = $this->createAuction();
202
203
        /* @var $entity CompanyAuction|GhostObjectInterface */
204
        $entity = $this->em->getReference(CompanyAuction::class , $id);
205
206
        self::assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy.");
0 ignored issues
show
Bug introduced by
The method isProxyInitialized does only exist in ProxyManager\Proxy\GhostObjectInterface, but not in Doctrine\Tests\Models\Company\CompanyAuction.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
207
        self::assertEquals($id, $entity->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Doctrine\Tests\Models\Company\CompanyAuction, but not in ProxyManager\Proxy\GhostObjectInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
208
        self::assertFalse($entity->isProxyInitialized(), "Getting the identifier doesn't initialize the proxy when extending.");
209
    }
210
211
    public function testDoNotInitializeProxyOnGettingTheIdentifierAndReturnTheRightType()
212
    {
213
        $product = new ECommerceProduct();
214
        $product->setName('Doctrine Cookbook');
215
216
        $shipping = new ECommerceShipping();
217
        $shipping->setDays(1);
218
        $product->setShipping($shipping);
219
220
        $this->em->persist($product);
221
        $this->em->flush();
222
        $this->em->clear();
223
224
        $id = $shipping->getId();
225
226
        /* @var $entity ECommerceProduct|GhostObjectInterface */
227
        $product = $this->em->getRepository(ECommerceProduct::class)->find($product->getId());
228
229
        $entity = $product->getShipping();
230
231
        self::assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy.");
232
        self::assertEquals($id, $entity->getId());
233
        self::assertSame($id, $entity->getId(), "Check that the id's are the same value, and type.");
234
        self::assertFalse($entity->isProxyInitialized(), "Getting the identifier doesn't initialize the proxy.");
235
    }
236
237 View Code Duplication
    public function testInitializeProxyOnGettingSomethingOtherThanTheIdentifier()
238
    {
239
        $id = $this->createProduct();
240
241
        /* @var $entity ECommerceProduct|GhostObjectInterface */
242
        $entity = $this->em->getReference(ECommerceProduct::class , $id);
243
244
        self::assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy.");
0 ignored issues
show
Bug introduced by
The method isProxyInitialized does only exist in ProxyManager\Proxy\GhostObjectInterface, but not in Doctrine\Tests\Models\ECommerce\ECommerceProduct.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
245
        self::assertEquals('Doctrine Cookbook', $entity->getName());
0 ignored issues
show
Bug introduced by
The method getName does only exist in Doctrine\Tests\Models\ECommerce\ECommerceProduct, but not in ProxyManager\Proxy\GhostObjectInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
246
        self::assertTrue($entity->isProxyInitialized(), "Getting something other than the identifier initializes the proxy.");
247
    }
248
249
    /**
250
     * @group DDC-1604
251
     */
252
    public function testCommonPersistenceProxy()
253
    {
254
        $id = $this->createProduct();
255
256
        /* @var $entity ECommerceProduct|GhostObjectInterface */
257
        $entity = $this->em->getReference(ECommerceProduct::class , $id);
258
259
        $className = ClassUtils::getClass($entity);
260
261
        self::assertInstanceOf(GhostObjectInterface::class, $entity);
262
        self::assertFalse($entity->isProxyInitialized());
0 ignored issues
show
Bug introduced by
The method isProxyInitialized does only exist in ProxyManager\Proxy\GhostObjectInterface, but not in Doctrine\Tests\Models\ECommerce\ECommerceProduct.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
263
        self::assertEquals(ECommerceProduct::class, $className);
264
265
        $proxyFileName = $this->resolver->resolveProxyClassPath(ECommerceProduct::class );
266
267
        self::assertTrue(file_exists($proxyFileName), "Proxy file name cannot be found generically.");
268
269
        $entity->initializeProxy();
0 ignored issues
show
Bug introduced by
The method initializeProxy does only exist in ProxyManager\Proxy\GhostObjectInterface, but not in Doctrine\Tests\Models\ECommerce\ECommerceProduct.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
270
271
        self::assertTrue($entity->isProxyInitialized());
272
    }
273
}
274