Failed Conditions
Push — 2.8.x ( 0ee171...60c486 )
by Benjamin
22:42 queued 16:29
created

testInvokeRemoveElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Cache\Persister\Collection;
4
5
use Doctrine\ORM\Cache\Persister\CachedPersister;
6
use Doctrine\ORM\Cache\Persister\Collection\CachedCollectionPersister;
7
use Doctrine\ORM\PersistentCollection;
8
use Doctrine\Tests\OrmTestCase;
9
10
use Doctrine\ORM\Cache\Region;
11
use Doctrine\ORM\EntityManager;
12
use Doctrine\ORM\Persisters\Collection\CollectionPersister;
13
14
use Doctrine\Tests\Models\Cache\State;
15
use Doctrine\Common\Collections\ArrayCollection;
16
17
/**
18
 * @group DDC-2183
19
 */
20
abstract class AbstractCollectionPersisterTest extends OrmTestCase
21
{
22
    /**
23
     * @var \Doctrine\ORM\Cache\Region
24
     */
25
    protected $region;
26
27
    /**
28
     * @var \Doctrine\ORM\Persisters\Collection\CollectionPersister
29
     */
30
    protected $collectionPersister;
31
32
    /**
33
     * @var \Doctrine\ORM\EntityManager
34
     */
35
    protected $em;
36
37
    /**
38
     * @var array
39
     */
40
    protected $regionMockMethods = [
41
        'getName',
42
        'contains',
43
        'get',
44
        'getMultiple',
45
        'put',
46
        'evict',
47
        'evictAll'
48
    ];
49
50
    /**
51
     * @var array
52
     */
53
    protected $collectionPersisterMockMethods = [
54
        'delete',
55
        'update',
56
        'count',
57
        'slice',
58
        'contains',
59
        'containsKey',
60
        'removeElement',
61
        'removeKey',
62
        'get',
63
        'getMultiple',
64
        'loadCriteria'
65
    ];
66
67
    /**
68
     * @param \Doctrine\ORM\EntityManager                             $em
69
     * @param \Doctrine\ORM\Persisters\Collection\CollectionPersister $persister
70
     * @param \Doctrine\ORM\Cache\Region                              $region
71
     * @param array                                                   $mapping
72
     *
73
     * @return \Doctrine\ORM\Cache\Persister\Collection\AbstractCollectionPersister
74
     */
75
    abstract protected function createPersister(EntityManager $em, CollectionPersister $persister, Region $region, array $mapping);
76
77
    protected function setUp()
78
    {
79
        $this->getSharedSecondLevelCacheDriverImpl()->flushAll();
80
        $this->enableSecondLevelCache();
81
        parent::setUp();
82
83
        $this->em                   = $this->_getTestEntityManager();
84
        $this->region               = $this->createRegion();
85
        $this->collectionPersister  = $this->getMockBuilder(CollectionPersister::class)
86
                                           ->setMethods($this->collectionPersisterMockMethods)
87
                                           ->getMock();
88
    }
89
90
    /**
91
     * @return \Doctrine\ORM\Cache\Region
92
     */
93
    protected function createRegion()
94
    {
95
        return $this->getMockBuilder(Region::class)
96
                    ->setMethods($this->regionMockMethods)
97
                    ->getMock();
98
    }
99
100
    /**
101
     * @return \Doctrine\ORM\PersistentCollection
102
     */
103
    protected function createCollection($owner, $assoc = null, $class = null, $elements = null)
104
    {
105
        $em    = $this->em;
106
        $class = $class ?: $this->em->getClassMetadata(State::class);
107
        $assoc = $assoc ?: $class->associationMappings['cities'];
108
        $coll  = new PersistentCollection($em, $class, $elements ?: new ArrayCollection);
109
110
        $coll->setOwner($owner, $assoc);
111
        $coll->setInitialized(true);
112
113
        return $coll;
114
    }
115
116
    protected function createPersisterDefault()
117
    {
118
        $assoc = $this->em->getClassMetadata(State::class)->associationMappings['cities'];
119
120
        return $this->createPersister($this->em, $this->collectionPersister, $this->region, $assoc);
121
    }
122
123
    public function testImplementsEntityPersister()
124
    {
125
        $persister = $this->createPersisterDefault();
126
127
        $this->assertInstanceOf(CollectionPersister::class, $persister);
128
        $this->assertInstanceOf(CachedPersister::class, $persister);
129
        $this->assertInstanceOf(CachedCollectionPersister::class, $persister);
130
    }
131
132
    public function testInvokeDelete()
133
    {
134
        $entity     = new State("Foo");
135
        $persister  = $this->createPersisterDefault();
136
        $collection = $this->createCollection($entity);
137
138
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
139
140
        $this->collectionPersister->expects($this->once())
141
            ->method('delete')
142
            ->with($this->equalTo($collection));
143
144
        $this->assertNull($persister->delete($collection));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $persister->delete($collection) targeting Doctrine\ORM\Persisters\...tionPersister::delete() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
145
    }
146
147
    public function testInvokeUpdate()
148
    {
149
        $entity     = new State("Foo");
150
        $persister  = $this->createPersisterDefault();
151
        $collection = $this->createCollection($entity);
152
153
        $collection->setDirty(true);
154
155
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
156
157
        $this->collectionPersister->expects($this->once())
158
            ->method('update')
159
            ->with($this->equalTo($collection));
160
161
        $this->assertNull($persister->update($collection));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $persister->update($collection) targeting Doctrine\ORM\Persisters\...tionPersister::update() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
162
    }
163
164
    public function testInvokeCount()
165
    {
166
        $entity     = new State("Foo");
167
        $persister  = $this->createPersisterDefault();
168
        $collection = $this->createCollection($entity);
169
170
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
171
172
        $this->collectionPersister->expects($this->once())
173
            ->method('count')
174
            ->with($this->equalTo($collection))
175
            ->will($this->returnValue(0));
176
177
        $this->assertEquals(0, $persister->count($collection));
178
    }
179
180
    public function testInvokeSlice()
181
    {
182
        $entity     = new State("Foo");
183
        $persister  = $this->createPersisterDefault();
184
        $collection = $this->createCollection($entity);
185
        $slice      = $this->createCollection($entity);
186
187
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
188
189
        $this->collectionPersister->expects($this->once())
190
            ->method('slice')
191
            ->with($this->equalTo($collection), $this->equalTo(1), $this->equalTo(2))
192
            ->will($this->returnValue($slice));
193
194
        $this->assertEquals($slice, $persister->slice($collection, 1 , 2));
195
    }
196
197
    public function testInvokeContains()
198
    {
199
        $entity     = new State("Foo");
200
        $element    = new State("Bar");
201
        $persister  = $this->createPersisterDefault();
202
        $collection = $this->createCollection($entity);
203
204
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
205
206
        $this->collectionPersister->expects($this->once())
207
            ->method('contains')
208
            ->with($this->equalTo($collection), $this->equalTo($element))
209
            ->will($this->returnValue(false));
210
211
        $this->assertFalse($persister->contains($collection,$element));
212
    }
213
214
    public function testInvokeContainsKey()
215
    {
216
        $entity     = new State("Foo");
217
        $persister  = $this->createPersisterDefault();
218
        $collection = $this->createCollection($entity);
219
220
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
221
222
        $this->collectionPersister->expects($this->once())
223
            ->method('containsKey')
224
            ->with($this->equalTo($collection), $this->equalTo(0))
225
            ->will($this->returnValue(false));
226
227
        $this->assertFalse($persister->containsKey($collection, 0));
228
    }
229
230
    public function testInvokeGet()
231
    {
232
        $entity     = new State("Foo");
233
        $element    = new State("Bar");
234
        $persister  = $this->createPersisterDefault();
235
        $collection = $this->createCollection($entity);
236
237
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
238
239
        $this->collectionPersister->expects($this->once())
240
            ->method('get')
241
            ->with($this->equalTo($collection), $this->equalTo(0))
242
            ->will($this->returnValue($element));
243
244
        $this->assertEquals($element, $persister->get($collection, 0));
245
    }
246
}
247