Failed Conditions
Pull Request — develop (#6873)
by
unknown
112:44 queued 47:41
created

testUpdateLockFailureShouldIgnoreQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 23
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Cache\Persister\Collection;
6
7
use Doctrine\ORM\Cache\ConcurrentRegion;
8
use Doctrine\ORM\Cache\Lock;
9
use Doctrine\ORM\Cache\Region;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Doctrine\ORM\Mapping\AssociationMetadata;
12
use Doctrine\Tests\Models\Cache\State;
13
use Doctrine\ORM\Cache\CollectionCacheKey;
14
use Doctrine\ORM\Persisters\Collection\CollectionPersister;
15
use Doctrine\ORM\Cache\Persister\Collection\ReadWriteCachedCollectionPersister;
16
17
/**
18
 * @group DDC-2183
19
 */
20
class ReadWriteCachedCollectionPersisterTest extends AbstractCollectionPersisterTest
21
{
22
    protected $regionMockMethods = [
23
        'getName',
24
        'contains',
25
        'get',
26
        'getMultiple',
27
        'put',
28
        'evict',
29
        'evictAll',
30
        'lock',
31
        'unlock',
32
    ];
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function createPersister(
38
        EntityManagerInterface $em,
39
        CollectionPersister $persister,
40
        Region $region,
41
        AssociationMetadata $association
42
    )
43
    {
44
        return new ReadWriteCachedCollectionPersister($persister, $region, $em, $association);
45
    }
46
47
    /**
48
     * @return \Doctrine\ORM\Cache\Region
49
     */
50
    protected function createRegion()
51
    {
52
        return $this->getMockBuilder(ConcurrentRegion::class)
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getMockBui...MockMethods)->getMock() returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the documented return type Doctrine\ORM\Cache\Region.
Loading history...
53
                    ->setMethods($this->regionMockMethods)
54
                    ->getMock();
55
    }
56
57 View Code Duplication
    public function testDeleteShouldLockItem()
58
    {
59
        $entity     = new State("Foo");
60
        $lock       = Lock::createLockRead();
61
        $persister  = $this->createPersisterDefault();
62
        $collection = $this->createCollection($entity);
63
        $key        = new CollectionCacheKey(State::class, 'cities', ['id'=>1]);
64
65
        $this->region->expects($this->once())
1 ignored issue
show
Bug introduced by
The method expects() does not exist on Doctrine\ORM\Cache\Region. ( Ignorable by Annotation )

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

65
        $this->region->/** @scrutinizer ignore-call */ 
66
                       expects($this->once())

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...
66
            ->method('lock')
67
            ->with($this->equalTo($key))
68
            ->will($this->returnValue($lock));
69
70
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
71
72
        $persister->delete($collection);
73
    }
74
75 View Code Duplication
    public function testUpdateShouldLockItem()
76
    {
77
        $entity     = new State("Foo");
78
        $lock       = Lock::createLockRead();
79
        $persister  = $this->createPersisterDefault();
80
        $collection = $this->createCollection($entity);
81
        $key        = new CollectionCacheKey(State::class, 'cities', ['id'=>1]);
82
83
        $this->region->expects($this->once())
84
            ->method('lock')
85
            ->with($this->equalTo($key))
86
            ->will($this->returnValue($lock));
87
88
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
89
90
        $persister->update($collection);
91
    }
92
93 View Code Duplication
    public function testUpdateTransactionRollBackShouldEvictItem()
94
    {
95
        $entity     = new State("Foo");
96
        $lock       = Lock::createLockRead();
97
        $persister  = $this->createPersisterDefault();
98
        $collection = $this->createCollection($entity);
99
        $key        = new CollectionCacheKey(State::class, 'cities', ['id'=>1]);
100
101
        $this->region->expects($this->once())
102
            ->method('lock')
103
            ->with($this->equalTo($key))
104
            ->will($this->returnValue($lock));
105
106
        $this->region->expects($this->once())
107
            ->method('evict')
108
            ->with($this->equalTo($key))
109
            ->will($this->returnValue($lock));
110
111
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
112
113
        $persister->update($collection);
114
        $persister->afterTransactionRolledBack();
115
    }
116
117 View Code Duplication
    public function testDeleteTransactionRollBackShouldEvictItem()
118
    {
119
        $entity     = new State("Foo");
120
        $lock       = Lock::createLockRead();
121
        $persister  = $this->createPersisterDefault();
122
        $collection = $this->createCollection($entity);
123
        $key        = new CollectionCacheKey(State::class, 'cities', ['id'=>1]);
124
125
        $this->region->expects($this->once())
126
            ->method('lock')
127
            ->with($this->equalTo($key))
128
            ->will($this->returnValue($lock));
129
130
        $this->region->expects($this->once())
131
            ->method('evict')
132
            ->with($this->equalTo($key));
133
134
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
135
136
        $persister->delete($collection);
137
        $persister->afterTransactionRolledBack();
138
    }
139
140 View Code Duplication
    public function testTransactionRollBackDeleteShouldClearQueue()
141
    {
142
        $entity     = new State("Foo");
143
        $lock       = Lock::createLockRead();
144
        $persister  = $this->createPersisterDefault();
145
        $collection = $this->createCollection($entity);
146
        $key        = new CollectionCacheKey(State::class, 'cities', ['id'=>1]);
147
        $property   = new \ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');
148
149
        $property->setAccessible(true);
150
151
        $this->region->expects($this->once())
152
            ->method('lock')
153
            ->with($this->equalTo($key))
154
            ->will($this->returnValue($lock));
155
156
        $this->region->expects($this->once())
157
            ->method('evict')
158
            ->with($this->equalTo($key));
159
160
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
161
162
        $persister->delete($collection);
163
164
        self::assertCount(1, $property->getValue($persister));
165
166
        $persister->afterTransactionRolledBack();
167
168
        self::assertCount(0, $property->getValue($persister));
169
    }
170
171 View Code Duplication
    public function testTransactionRollBackUpdateShouldClearQueue()
172
    {
173
        $entity     = new State("Foo");
174
        $lock       = Lock::createLockRead();
175
        $persister  = $this->createPersisterDefault();
176
        $collection = $this->createCollection($entity);
177
        $key        = new CollectionCacheKey(State::class, 'cities', ['id'=>1]);
178
        $property   = new \ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');
179
180
        $property->setAccessible(true);
181
182
        $this->region->expects($this->once())
183
            ->method('lock')
184
            ->with($this->equalTo($key))
185
            ->will($this->returnValue($lock));
186
187
        $this->region->expects($this->once())
188
            ->method('evict')
189
            ->with($this->equalTo($key));
190
191
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
192
193
        $persister->update($collection);
194
195
        self::assertCount(1, $property->getValue($persister));
196
197
        $persister->afterTransactionRolledBack();
198
199
        self::assertCount(0, $property->getValue($persister));
200
    }
201
202 View Code Duplication
    public function testTransactionRollCommitDeleteShouldClearQueue()
203
    {
204
        $entity     = new State("Foo");
205
        $lock       = Lock::createLockRead();
206
        $persister  = $this->createPersisterDefault();
207
        $collection = $this->createCollection($entity);
208
        $key        = new CollectionCacheKey(State::class, 'cities', ['id'=>1]);
209
        $property   = new \ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');
210
211
        $property->setAccessible(true);
212
213
        $this->region->expects($this->once())
214
            ->method('lock')
215
            ->with($this->equalTo($key))
216
            ->will($this->returnValue($lock));
217
218
        $this->region->expects($this->once())
219
            ->method('evict')
220
            ->with($this->equalTo($key));
221
222
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
223
224
        $persister->delete($collection);
225
226
        self::assertCount(1, $property->getValue($persister));
227
228
        $persister->afterTransactionComplete();
229
230
        self::assertCount(0, $property->getValue($persister));
231
    }
232
233 View Code Duplication
    public function testTransactionRollCommitUpdateShouldClearQueue()
234
    {
235
        $entity     = new State("Foo");
236
        $lock       = Lock::createLockRead();
237
        $persister  = $this->createPersisterDefault();
238
        $collection = $this->createCollection($entity);
239
        $key        = new CollectionCacheKey(State::class, 'cities', ['id'=>1]);
240
        $property   = new \ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');
241
242
        $property->setAccessible(true);
243
244
        $this->region->expects($this->once())
245
            ->method('lock')
246
            ->with($this->equalTo($key))
247
            ->will($this->returnValue($lock));
248
249
        $this->region->expects($this->once())
250
            ->method('evict')
251
            ->with($this->equalTo($key));
252
253
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
254
255
        $persister->update($collection);
256
257
        self::assertCount(1, $property->getValue($persister));
258
259
        $persister->afterTransactionComplete();
260
261
        self::assertCount(0, $property->getValue($persister));
262
    }
263
264 View Code Duplication
    public function testDeleteLockFailureShouldIgnoreQueue()
265
    {
266
        $entity     = new State("Foo");
267
        $persister  = $this->createPersisterDefault();
268
        $collection = $this->createCollection($entity);
269
        $key        = new CollectionCacheKey(State::class, 'cities', ['id'=>1]);
270
        $property   = new \ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');
271
272
        $property->setAccessible(true);
273
274
        $this->region->expects($this->once())
275
            ->method('lock')
276
            ->with($this->equalTo($key))
277
            ->will($this->returnValue(null));
278
279
        $this->collectionPersister->expects($this->once())
1 ignored issue
show
Bug introduced by
The method expects() does not exist on Doctrine\ORM\Persisters\...ion\CollectionPersister. ( Ignorable by Annotation )

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

279
        $this->collectionPersister->/** @scrutinizer ignore-call */ 
280
                                    expects($this->once())

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...
280
            ->method('delete')
281
            ->with($this->equalTo($collection));
282
283
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
284
285
        $persister->delete($collection);
286
        self::assertCount(0, $property->getValue($persister));
287
    }
288
289 View Code Duplication
    public function testUpdateLockFailureShouldIgnoreQueue()
290
    {
291
        $entity     = new State("Foo");
292
        $persister  = $this->createPersisterDefault();
293
        $collection = $this->createCollection($entity);
294
        $key        = new CollectionCacheKey(State::class, 'cities', ['id'=>1]);
295
        $property   = new \ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');
296
297
        $property->setAccessible(true);
298
299
        $this->region->expects($this->once())
300
            ->method('lock')
301
            ->with($this->equalTo($key))
302
            ->will($this->returnValue(null));
303
304
        $this->collectionPersister->expects($this->once())
305
            ->method('update')
306
            ->with($this->equalTo($collection));
307
308
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
309
310
        $persister->update($collection);
311
        self::assertCount(0, $property->getValue($persister));
312
    }
313
}
314