-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathDomainEventPublisherTest.php
More file actions
67 lines (55 loc) · 1.73 KB
/
DomainEventPublisherTest.php
File metadata and controls
67 lines (55 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Ddd\Domain;
class DomainEventPublisherTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function itShouldNotifySubscriber()
{
$this->subscribe($subscriber = new SpySubscriber('test-event'));
$this->publish($domainEvent = new FakeDomainEvent('test-event'));
$this->assertEventHandled($subscriber, $domainEvent);
}
private function subscribe($subscriber)
{
return DomainEventPublisher::instance()->subscribe($subscriber);
}
private function publish($domainEvent)
{
DomainEventPublisher::instance()->publish($domainEvent);
}
private function assertEventHandled($subscriber, $domainEvent)
{
$this->assertTrue($subscriber->isHandled);
$this->assertEquals($domainEvent, $subscriber->domainEvent);
}
/**
* @test
*/
public function notSubscribedSubscribersShouldNotBeNotified()
{
$this->subscribe($subscriber = new SpySubscriber('test-event'));
$this->publish(new FakeDomainEvent('other-test-event'));
$this->assertEventNotHandled($subscriber);
}
private function assertEventNotHandled($subscriber)
{
$this->assertFalse($subscriber->isHandled);
$this->assertNull($subscriber->domainEvent);
}
/**
* @test
*/
public function itShouldUnsubscribeSubscriber()
{
$subscriberId = $this->subscribe($subscriber = new SpySubscriber('test-event'));
$this->unsubscribe($subscriberId);
$this->publish(new FakeDomainEvent('test-event'));
$this->assertEventNotHandled($subscriber);
}
private function unsubscribe($id)
{
DomainEventPublisher::instance()->unsubscribe($id);
}
}