1
|
|
|
<?php |
2
|
|
|
namespace FOS\ElasticaBundle\Tests\Persister\Event; |
3
|
|
|
|
4
|
|
|
use FOS\ElasticaBundle\Persister\Event\PersistEvent; |
5
|
|
|
use FOS\ElasticaBundle\Persister\Event\PrePersistEvent; |
6
|
|
|
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface; |
7
|
|
|
use FOS\ElasticaBundle\Provider\PagerInterface; |
8
|
|
|
use Symfony\Component\EventDispatcher\Event; |
9
|
|
|
|
10
|
|
View Code Duplication |
final class PrePersistEventTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
public function testShouldBeSubClassOfEventClass() |
13
|
|
|
{ |
14
|
|
|
$rc = new \ReflectionClass(PrePersistEvent::class); |
15
|
|
|
|
16
|
|
|
$this->assertTrue($rc->isSubclassOf(Event::class)); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testShouldImplementPersistEventInterface() |
20
|
|
|
{ |
21
|
|
|
$rc = new \ReflectionClass(PrePersistEvent::class); |
22
|
|
|
|
23
|
|
|
$this->assertTrue($rc->implementsInterface(PersistEvent::class)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testShouldFinal() |
27
|
|
|
{ |
28
|
|
|
$rc = new \ReflectionClass(PrePersistEvent::class); |
29
|
|
|
|
30
|
|
|
$this->assertTrue($rc->isFinal()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testCouldBeConstructedWithPagerAndObjectPersisterAndOptions() |
34
|
|
|
{ |
35
|
|
|
new PrePersistEvent($this->createPagerMock(), $this->createObjectPersisterMock(), []); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testShouldAllowGetPagerSetInConstructor() |
39
|
|
|
{ |
40
|
|
|
$expectedPager = $this->createPagerMock(); |
41
|
|
|
|
42
|
|
|
$event = new PrePersistEvent($expectedPager, $this->createObjectPersisterMock(), []); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$this->assertSame($expectedPager, $event->getPager()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testShouldAllowGetPreviouslySetPager() |
48
|
|
|
{ |
49
|
|
|
$event = new PrePersistEvent($this->createPagerMock(), $this->createObjectPersisterMock(), []); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$expectedPager = $this->createPagerMock(); |
52
|
|
|
$event->setPager($expectedPager); |
53
|
|
|
|
54
|
|
|
$this->assertSame($expectedPager, $event->getPager()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testShouldAllowGetObjectPersisterSetInConstructor() |
58
|
|
|
{ |
59
|
|
|
$expectedPersister = $this->createObjectPersisterMock(); |
60
|
|
|
|
61
|
|
|
$event = new PrePersistEvent($this->createPagerMock(), $expectedPersister, []); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$this->assertSame($expectedPersister, $event->getObjectPersister()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testShouldAllowGetPreviouslySetObjectsPersister() |
67
|
|
|
{ |
68
|
|
|
$event = new PrePersistEvent($this->createPagerMock(), $this->createObjectPersisterMock(), []); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$expectedPersister = $this->createObjectPersisterMock(); |
71
|
|
|
$event->setObjectPersister($expectedPersister); |
72
|
|
|
|
73
|
|
|
$this->assertSame($expectedPersister, $event->getObjectPersister()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testShouldAllowGetOptionsSetInConstructor() |
77
|
|
|
{ |
78
|
|
|
$expectedOptions = ['foo' => 'fooVal', 'bar' => 'barVal']; |
79
|
|
|
|
80
|
|
|
$event = new PrePersistEvent($this->createPagerMock(), $this->createObjectPersisterMock(), $expectedOptions); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$this->assertSame($expectedOptions, $event->getOptions()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testShouldAllowGetPreviouslySetOptions() |
86
|
|
|
{ |
87
|
|
|
$event = new PrePersistEvent($this->createPagerMock(), $this->createObjectPersisterMock(), ['foo' => 'fooVal', 'bar' => 'barVal']); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$expectedOptions = ['foo' => 'fooNewVal', 'bar' => 'barnewVal']; |
90
|
|
|
$event->setOptions($expectedOptions); |
91
|
|
|
|
92
|
|
|
$this->assertSame($expectedOptions, $event->getOptions()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return ObjectPersisterInterface|\PHPUnit_Framework_MockObject_MockObject |
97
|
|
|
*/ |
98
|
|
|
private function createObjectPersisterMock() |
99
|
|
|
{ |
100
|
|
|
return $this->getMock(ObjectPersisterInterface::class, [], [], '', false); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return PagerInterface|\PHPUnit_Framework_MockObject_MockObject |
105
|
|
|
*/ |
106
|
|
|
private function createPagerMock() |
107
|
|
|
{ |
108
|
|
|
return $this->getMock(PagerInterface::class, [], [], '', false); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.