Skip to content

Commit 802a074

Browse files
committed
add some tests
1 parent ffcfbcf commit 802a074

File tree

3 files changed

+309
-0
lines changed

3 files changed

+309
-0
lines changed

tests/Unit/Doctrine/AbstractListenerTestCase.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ public function getId()
3535
}
3636
}
3737

38+
class ConditionalUpdateEntity extends Entity
39+
{
40+
private $shouldBeUpdated;
41+
42+
public function __construct($id, $shouldBeUpdated)
43+
{
44+
parent::__construct($id);
45+
$this->shouldBeUpdated = $shouldBeUpdated;
46+
}
47+
48+
public function shouldBeUpdated(): bool
49+
{
50+
return $this->shouldBeUpdated;
51+
}
52+
}
53+
3854
/**
3955
* See concrete MongoDB/ORM instances of this abstract test.
4056
*
@@ -254,6 +270,90 @@ public function testShouldPersistOnKernelTerminateIfDeferIsTrue()
254270
$listener->onTerminate();
255271
}
256272

273+
public function testConditionalUpdateObjectInsertedOnPersistWhenShouldBeUpdatedIsTrue()
274+
{
275+
$entity = new ConditionalUpdateEntity(1, true);
276+
$persister = $this->getMockPersister($entity, 'index');
277+
$eventArgs = $this->createLifecycleEventArgs($entity, $this->getMockObjectManager());
278+
$indexable = $this->getMockIndexable('index', $entity, true);
279+
280+
$listener = $this->createListener($persister, $indexable, ['indexName' => 'index']);
281+
$listener->postPersist($eventArgs);
282+
283+
$this->assertSame($entity, \current($listener->scheduledForInsertion));
284+
285+
$persister->expects($this->once())
286+
->method('insertMany')
287+
->with($listener->scheduledForInsertion)
288+
;
289+
290+
$listener->postFlush($eventArgs);
291+
}
292+
293+
public function testConditionalUpdateObjectNotInsertedOnPersistWhenShouldBeUpdatedIsFalse()
294+
{
295+
$entity = new ConditionalUpdateEntity(1, false);
296+
$persister = $this->getMockPersister($entity, 'index');
297+
$eventArgs = $this->createLifecycleEventArgs($entity, $this->getMockObjectManager());
298+
$indexable = $this->getMockIndexable('index', $entity, true);
299+
300+
$listener = $this->createListener($persister, $indexable, ['indexName' => 'index']);
301+
$listener->postPersist($eventArgs);
302+
303+
$this->assertEmpty($listener->scheduledForInsertion);
304+
305+
$persister->expects($this->never())
306+
->method('insertMany')
307+
;
308+
309+
$listener->postFlush($eventArgs);
310+
}
311+
312+
public function testConditionalUpdateObjectReplacedOnUpdateWhenShouldBeUpdatedIsTrue()
313+
{
314+
$entity = new ConditionalUpdateEntity(1, true);
315+
$persister = $this->getMockPersister($entity, 'index');
316+
$eventArgs = $this->createLifecycleEventArgs($entity, $this->getMockObjectManager());
317+
$indexable = $this->getMockIndexable('index', $entity, true);
318+
319+
$listener = $this->createListener($persister, $indexable, ['indexName' => 'index']);
320+
$listener->postUpdate($eventArgs);
321+
322+
$this->assertSame($entity, \current($listener->scheduledForUpdate));
323+
324+
$persister->expects($this->once())
325+
->method('replaceMany')
326+
->with([$entity])
327+
;
328+
$persister->expects($this->never())
329+
->method('deleteById')
330+
;
331+
332+
$listener->postFlush($eventArgs);
333+
}
334+
335+
public function testConditionalUpdateObjectNotReplacedOnUpdateWhenShouldBeUpdatedIsFalse()
336+
{
337+
$entity = new ConditionalUpdateEntity(1, false);
338+
$persister = $this->getMockPersister($entity, 'index');
339+
$eventArgs = $this->createLifecycleEventArgs($entity, $this->getMockObjectManager());
340+
$indexable = $this->getMockIndexable('index', $entity, true);
341+
342+
$listener = $this->createListener($persister, $indexable, ['indexName' => 'index']);
343+
$listener->postUpdate($eventArgs);
344+
345+
$this->assertEmpty($listener->scheduledForUpdate);
346+
347+
$persister->expects($this->never())
348+
->method('replaceMany')
349+
;
350+
$persister->expects($this->never())
351+
->method('deleteById')
352+
;
353+
354+
$listener->postFlush($eventArgs);
355+
}
356+
257357
abstract protected function getLifecycleEventArgsClass();
258358

259359
abstract protected function getListenerClass();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSElasticaBundle package.
5+
*
6+
* (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\ElasticaBundle\Tests\Unit\Doctrine;
13+
14+
use FOS\ElasticaBundle\Doctrine\ConditionalUpdate;
15+
16+
class ConditionalUpdateEntity implements ConditionalUpdate
17+
{
18+
public $identifier;
19+
private $id;
20+
private $shouldBeUpdated = true;
21+
22+
public function __construct($id, $shouldBeUpdated = true)
23+
{
24+
$this->id = $id;
25+
$this->shouldBeUpdated = $shouldBeUpdated;
26+
}
27+
28+
public function getId()
29+
{
30+
return $this->id;
31+
}
32+
33+
public function shouldBeUpdated(): bool
34+
{
35+
return $this->shouldBeUpdated;
36+
}
37+
38+
public function setShouldBeUpdated(bool $shouldBeUpdated): void
39+
{
40+
$this->shouldBeUpdated = $shouldBeUpdated;
41+
}
42+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSElasticaBundle package.
5+
*
6+
* (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\ElasticaBundle\Tests\Unit\Doctrine;
13+
14+
use Doctrine\Persistence\Event\LifecycleEventArgs;
15+
use FOS\ElasticaBundle\Doctrine\ConditionalUpdate;
16+
use FOS\ElasticaBundle\Doctrine\Listener;
17+
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
18+
use FOS\ElasticaBundle\Provider\IndexableInterface;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* @internal
23+
*/
24+
class ConditionalUpdateListenerTest extends TestCase
25+
{
26+
public function testEntityWithConditionalUpdateTrueIsIndexed()
27+
{
28+
$entity = $this->createMock(ConditionalUpdate::class);
29+
$entity->expects($this->once())
30+
->method('shouldBeUpdated')
31+
->willReturn(true);
32+
33+
$persister = $this->createMock(ObjectPersisterInterface::class);
34+
$persister->expects($this->once())
35+
->method('handlesObject')
36+
->with($entity)
37+
->willReturn(true);
38+
39+
$indexable = $this->createMock(IndexableInterface::class);
40+
$indexable->expects($this->once())
41+
->method('isObjectIndexable')
42+
->with('index_name', $entity)
43+
->willReturn(true);
44+
45+
$eventArgs = $this->createMock(LifecycleEventArgs::class);
46+
$eventArgs->expects($this->once())
47+
->method('getObject')
48+
->willReturn($entity);
49+
50+
$listener = new Listener($persister, $indexable, ['indexName' => 'index_name']);
51+
52+
$listener->postPersist($eventArgs);
53+
54+
$this->assertContains($entity, $listener->scheduledForInsertion);
55+
}
56+
57+
public function testEntityWithConditionalUpdateFalseIsNotIndexed()
58+
{
59+
// Create a mock entity implementing ConditionalUpdate that returns false
60+
$entity = $this->createMock(ConditionalUpdate::class);
61+
$entity->expects($this->once())
62+
->method('shouldBeUpdated')
63+
->willReturn(false);
64+
65+
// Mock dependencies
66+
$persister = $this->createMock(ObjectPersisterInterface::class);
67+
$persister->expects($this->once())
68+
->method('handlesObject')
69+
->with($entity)
70+
->willReturn(true);
71+
72+
$indexable = $this->createMock(IndexableInterface::class);
73+
$indexable->expects($this->once())
74+
->method('isObjectIndexable')
75+
->with('index_name', $entity)
76+
->willReturn(true);
77+
78+
// Create the event args
79+
$eventArgs = $this->createMock(LifecycleEventArgs::class);
80+
$eventArgs->expects($this->once())
81+
->method('getObject')
82+
->willReturn($entity);
83+
84+
// Create listener
85+
$listener = new Listener($persister, $indexable, ['indexName' => 'index_name']);
86+
87+
// Test postPersist
88+
$listener->postPersist($eventArgs);
89+
90+
// Check if entity is NOT in scheduledForInsertion
91+
$this->assertEmpty($listener->scheduledForInsertion);
92+
}
93+
94+
public function testEntityWithConditionalUpdateTrueIsUpdated()
95+
{
96+
// Create a mock entity implementing ConditionalUpdate that returns true
97+
$entity = $this->createMock(ConditionalUpdate::class);
98+
$entity->expects($this->once())
99+
->method('shouldBeUpdated')
100+
->willReturn(true);
101+
102+
// Mock dependencies
103+
$persister = $this->createMock(ObjectPersisterInterface::class);
104+
$persister->expects($this->once())
105+
->method('handlesObject')
106+
->with($entity)
107+
->willReturn(true);
108+
109+
$indexable = $this->createMock(IndexableInterface::class);
110+
$indexable->expects($this->once())
111+
->method('isObjectIndexable')
112+
->with('index_name', $entity)
113+
->willReturn(true);
114+
115+
// Create the event args
116+
$eventArgs = $this->createMock(LifecycleEventArgs::class);
117+
$eventArgs->expects($this->once())
118+
->method('getObject')
119+
->willReturn($entity);
120+
121+
// Create listener
122+
$listener = new Listener($persister, $indexable, ['indexName' => 'index_name']);
123+
124+
// Test postUpdate
125+
$listener->postUpdate($eventArgs);
126+
127+
// Check if entity is in scheduledForUpdate
128+
$this->assertContains($entity, $listener->scheduledForUpdate);
129+
}
130+
131+
public function testEntityWithConditionalUpdateFalseIsNotUpdated()
132+
{
133+
// Create a mock entity implementing ConditionalUpdate that returns false
134+
$entity = $this->createMock(ConditionalUpdate::class);
135+
$entity->expects($this->once())
136+
->method('shouldBeUpdated')
137+
->willReturn(false);
138+
139+
// Mock dependencies
140+
$persister = $this->createMock(ObjectPersisterInterface::class);
141+
$persister->expects($this->once())
142+
->method('handlesObject')
143+
->with($entity)
144+
->willReturn(true);
145+
146+
$indexable = $this->createMock(IndexableInterface::class);
147+
$indexable->expects($this->once())
148+
->method('isObjectIndexable')
149+
->with('index_name', $entity)
150+
->willReturn(true);
151+
152+
// Create the event args
153+
$eventArgs = $this->createMock(LifecycleEventArgs::class);
154+
$eventArgs->expects($this->once())
155+
->method('getObject')
156+
->willReturn($entity);
157+
158+
// Create listener
159+
$listener = new Listener($persister, $indexable, ['indexName' => 'index_name']);
160+
161+
// Test postUpdate
162+
$listener->postUpdate($eventArgs);
163+
164+
// Check if entity is NOT in scheduledForUpdate
165+
$this->assertEmpty($listener->scheduledForUpdate);
166+
}
167+
}

0 commit comments

Comments
 (0)