|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace spec\Knp\Rad\DomainEvent\Dispatcher; |
|
4
|
|
|
|
|
5
|
|
|
use PhpSpec\ObjectBehavior; |
|
6
|
|
|
use Prophecy\Argument; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
8
|
|
|
use Knp\Rad\DomainEvent\Provider; |
|
9
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
|
10
|
|
|
use Doctrine\ORM\Event\PostFlushEventArgs; |
|
11
|
|
|
use Knp\Rad\DomainEvent\Event; |
|
12
|
|
|
|
|
13
|
|
|
class DoctrineSpec extends ObjectBehavior |
|
14
|
|
|
{ |
|
15
|
|
|
function let(EventDispatcherInterface $dispatcher) { |
|
16
|
|
|
$this->beConstructedWith($dispatcher); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
function it_is_initializable() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->shouldHaveType('Knp\Rad\DomainEvent\Dispatcher\Doctrine'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
View Code Duplication |
function it_pops_events_of_a_post_persisted_entity( |
|
|
|
|
|
|
25
|
|
|
LifecycleEventArgs $event, |
|
26
|
|
|
Provider $entity, |
|
27
|
|
|
PostFlushEventArgs $postFlushEvent, |
|
28
|
|
|
Event $raisedEvent, |
|
29
|
|
|
$dispatcher |
|
30
|
|
|
) { |
|
31
|
|
|
$event->getEntity()->willReturn($entity); |
|
32
|
|
|
$entity->popEvents()->willReturn([$raisedEvent]); |
|
33
|
|
|
|
|
34
|
|
|
$raisedEvent->setSubject($entity)->shouldBeCalled(); |
|
35
|
|
|
$raisedEvent->getName()->willReturn('FooEvent'); |
|
36
|
|
|
$dispatcher->dispatch('FooEvent', $raisedEvent)->shouldBeCalled(); |
|
37
|
|
|
|
|
38
|
|
|
$this->postPersist($event); |
|
39
|
|
|
$this->postFlush($postFlushEvent); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
View Code Duplication |
function it_pops_events_of_a_post_loaded_entity( |
|
|
|
|
|
|
43
|
|
|
LifecycleEventArgs $event, |
|
44
|
|
|
Provider $entity, |
|
45
|
|
|
PostFlushEventArgs $postFlushEvent, |
|
46
|
|
|
Event $raisedEvent, |
|
47
|
|
|
$dispatcher |
|
48
|
|
|
) { |
|
49
|
|
|
$event->getEntity()->willReturn($entity); |
|
50
|
|
|
$entity->popEvents()->willReturn([$raisedEvent]); |
|
51
|
|
|
|
|
52
|
|
|
$raisedEvent->setSubject($entity)->shouldBeCalled(); |
|
53
|
|
|
$raisedEvent->getName()->willReturn('FooEvent'); |
|
54
|
|
|
$dispatcher->dispatch('FooEvent', $raisedEvent)->shouldBeCalled(); |
|
55
|
|
|
|
|
56
|
|
|
$this->postLoad($event); |
|
57
|
|
|
$this->postFlush($postFlushEvent); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
function it_doesnt_dispatch_events_of_non_provider_entities( |
|
61
|
|
|
LifecycleEventArgs $event, |
|
62
|
|
|
\StdClass $entity, |
|
63
|
|
|
PostFlushEventArgs $postFlushEvent, |
|
64
|
|
|
Event $raisedEvent, |
|
65
|
|
|
$dispatcher |
|
66
|
|
|
) { |
|
67
|
|
|
$event->getEntity()->willReturn($entity); |
|
68
|
|
|
$dispatcher->dispatch(Argument::any(), $raisedEvent)->shouldNotBeCalled(); |
|
69
|
|
|
|
|
70
|
|
|
$this->postPersist($event); |
|
71
|
|
|
$this->postFlush($postFlushEvent); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
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.