EventListenerTest::shouldHydrateEntityOnPostLoad()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 10
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\RDMBundle\Tests\Doctrine;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\RDMBundle\Doctrine\EventListener;
15
use Addiks\RDMBundle\Hydration\EntityHydratorInterface;
16
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
17
use Doctrine\ORM\Event\LifecycleEventArgs;
18
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface;
19
use Addiks\RDMBundle\DataLoader\DataLoaderInterface;
20
use Doctrine\ORM\EntityManagerInterface;
21
use Doctrine\ORM\Proxy\Proxy;
22
use Doctrine\ORM\Event\PostFlushEventArgs;
23
use Doctrine\ORM\UnitOfWork;
24
use Addiks\RDMBundle\Mapping\EntityMappingInterface;
25
26
final class EventListenerTest extends TestCase
27
{
28
29
    /**
30
     * @var EventListener
31
     */
32
    private $eventListener;
33
34
    /**
35
     * @var EntityHydratorInterface
36
     */
37
    private $entityServiceHydrator;
38
39
    /**
40
     * @var MappingDriverInterface
41
     */
42
    private $mappingDriver;
43
44
    /**
45
     * @var DataLoaderInterface
46
     */
47
    private $dbalDataLoader;
48
49
    public function setUp(): void
50
    {
51
        $this->entityServiceHydrator = $this->createMock(EntityHydratorInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Addiks...dratorInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Addiks\RDMBundle\Hydration\EntityHydratorInterface of property $entityServiceHydrator.

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...
52
        $this->mappingDriver = $this->createMock(MappingDriverInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Addiks...DriverInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Addiks\RDMBundle\Mapping...\MappingDriverInterface of property $mappingDriver.

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...
53
        $this->dbalDataLoader = $this->createMock(DataLoaderInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Addiks...LoaderInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Addiks\RDMBundle\DataLoader\DataLoaderInterface of property $dbalDataLoader.

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...
54
55
        $this->eventListener = new EventListener(
56
            $this->entityServiceHydrator,
57
            $this->mappingDriver,
58
            $this->dbalDataLoader
59
        );
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function shouldHydrateEntityOnPostLoad()
66
    {
67
        /** @var LifecycleEventArgs $arguments */
68
        $arguments = $this->createMock(LifecycleEventArgs::class);
69
70
        $entity = new EntityExample();
71
72
        $arguments->method('getEntity')->willReturn($entity);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\ORM\Event\LifecycleEventArgs. ( Ignorable by Annotation )

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

72
        $arguments->/** @scrutinizer ignore-call */ 
73
                    method('getEntity')->willReturn($entity);

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...
73
        $arguments->method('getEntityManager')->willReturn($this->createMock(EntityManagerInterface::class));
74
75
        $this->entityServiceHydrator->expects($this->once())->method('hydrateEntity')->with($entity);
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Addiks\RDMBundle\Hydration\EntityHydratorInterface. ( Ignorable by Annotation )

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

75
        $this->entityServiceHydrator->/** @scrutinizer ignore-call */ 
76
                                      expects($this->once())->method('hydrateEntity')->with($entity);

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
        $this->eventListener->postLoad($arguments);
78
    }
79
80
    /**
81
     * @test
82
     */
83
    public function shouldAssertHydrationOnPrePersist()
84
    {
85
        /** @var LifecycleEventArgs $arguments */
86
        $arguments = $this->createMock(LifecycleEventArgs::class);
87
88
        $entity = new EntityExample();
89
90
        $arguments->method('getEntity')->willReturn($entity);
91
        $arguments->method('getEntityManager')->willReturn($this->createMock(EntityManagerInterface::class));
92
93
        $this->entityServiceHydrator->expects($this->once())->method('assertHydrationOnEntity')->with($entity);
94
95
        $this->eventListener->prePersist($arguments);
96
    }
97
98
    /**
99
     * @test
100
     */
101
    public function shouldRemoveEntityProxiesOnlyWhenInitialized()
102
    {
103
        $this->mappingDriver->method('loadRDMMetadataForClass')->willReturn(
0 ignored issues
show
Bug introduced by
The method method() does not exist on Addiks\RDMBundle\Mapping...\MappingDriverInterface. ( Ignorable by Annotation )

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

103
        $this->mappingDriver->/** @scrutinizer ignore-call */ 
104
                              method('loadRDMMetadataForClass')->willReturn(

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...
104
            $this->createMock(EntityMappingInterface::class)
105
        );
106
107
        /** @var Proxy $proxiedEntity */
108
        $proxiedEntity = $this->createMock(Proxy::class);
109
110
        $proxiedEntity->method('__isInitialized')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\ORM\Proxy\Proxy. ( Ignorable by Annotation )

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

110
        $proxiedEntity->/** @scrutinizer ignore-call */ 
111
                        method('__isInitialized')->willReturn(true);

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...
111
112
        /** @var EntityManagerInterface $entityManager */
113
        $entityManager = $this->createMock(EntityManagerInterface::class);
114
115
        /** @var UnitOfWork $unitOfWork */
116
        $unitOfWork = $this->createMock(UnitOfWork::class);
117
118
        $entityManager->method('getUnitOfWork')->willReturn($unitOfWork);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\ORM\EntityManagerInterface. ( Ignorable by Annotation )

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

118
        $entityManager->/** @scrutinizer ignore-call */ 
119
                        method('getUnitOfWork')->willReturn($unitOfWork);

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...
119
120
        $unitOfWork->method('getIdentityMap')->willReturn([
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\ORM\UnitOfWork. ( Ignorable by Annotation )

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

120
        $unitOfWork->/** @scrutinizer ignore-call */ 
121
                     method('getIdentityMap')->willReturn([

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...
121
            EntityExample::class => [$proxiedEntity]
122
        ]);
123
124
        $unitOfWork->method('isScheduledForDelete')->willReturn(false);
125
126
        $this->dbalDataLoader->expects($this->once())->method('storeDBALDataForEntity')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Addiks\RDMBundle\DataLoader\DataLoaderInterface. ( Ignorable by Annotation )

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

126
        $this->dbalDataLoader->/** @scrutinizer ignore-call */ 
127
                               expects($this->once())->method('storeDBALDataForEntity')->with(

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...
127
            $this->equalTo($proxiedEntity),
128
            $this->equalTo($entityManager)
129
        );
130
131
        $this->eventListener->postFlush(new PostFlushEventArgs($entityManager));
132
    }
133
134
    /**
135
     * @test
136
     */
137
    public function shouldIgnoreUninitializedProxies()
138
    {
139
        /** @var Proxy $proxiedEntity */
140
        $proxiedEntity = $this->createMock(Proxy::class);
141
142
        $proxiedEntity->method('__isInitialized')->willReturn(false);
143
144
        /** @var EntityManagerInterface $entityManager */
145
        $entityManager = $this->createMock(EntityManagerInterface::class);
146
147
        /** @var UnitOfWork $unitOfWork */
148
        $unitOfWork = $this->createMock(UnitOfWork::class);
149
150
        $entityManager->method('getUnitOfWork')->willReturn($unitOfWork);
151
152
        $unitOfWork->method('getIdentityMap')->willReturn([
153
            EntityExample::class => [$proxiedEntity]
154
        ]);
155
156
        $unitOfWork->method('isScheduledForDelete')->willReturn(false);
157
158
        $this->dbalDataLoader->expects($this->never())->method('storeDBALDataForEntity');
159
160
        $this->eventListener->postFlush(new PostFlushEventArgs($entityManager));
161
    }
162
163
}
164