DomainEventCollectionTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 78
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetDomainEventsOfType_WhenNoEvents_ShouldReturnEmptyArray() 0 8 1
A testGetDomainEventsOfType_WhenTwoEventsPerEachType_ShouldReturnTwoEvents() 0 14 1
A testGetDomainEventOfType_WhenNoEvents_ShouldReturnNull() 0 8 1
A testGetDomainEventOfType_WhenTwoEvents_ShouldThrowMoreThanOneEventOfRequestedTypeExists() 0 12 1
A testGetDomainEventOfType_WhenOneEvent_ShouldReturnEvent() 0 11 1
A givenCollectionWithEvents() 0 4 1
A whenGetEventsOfType() 0 4 1
A whenGetEventOfType() 0 4 1
1
<?php declare(strict_types = 1);
2
3
namespace TomCizek\ResponseRecorder\Tests\Application\ResponseRecorder\MutationResponse;
4
5
use PHPUnit\Framework\TestCase;
6
use Prooph\Common\Messaging\DomainEvent;
7
use TomCizek\ResponseRecorder\Application\ResponseRecorder\MutationResponse\DomainEventCollection;
8
use TomCizek\ResponseRecorder\Application\ResponseRecorder\MutationResponse\DomainEventCollection\MoreThanOneEventOfRequestedTypeExists;
9
use TomCizek\ResponseRecorder\Tests\FakeImplementations\TestDomainEvent;
10
use TomCizek\ResponseRecorder\Tests\FakeImplementations\TestTranslatableDomainErrorDomainEvent;
11
12
class DomainEventCollectionTest extends TestCase
13
{
14
	/** @var DomainEventCollection */
15
	private $domainEventCollection;
16
17
	public function testGetDomainEventsOfType_WhenNoEvents_ShouldReturnEmptyArray()
18
	{
19
		$this->givenCollectionWithEvents([]);
20
21
		$result = $this->whenGetEventsOfType(TestDomainEvent::class);
22
23
		self::assertSame([], $result);
24
	}
25
26
	public function testGetDomainEventsOfType_WhenTwoEventsPerEachType_ShouldReturnTwoEvents()
27
	{
28
		$this->givenCollectionWithEvents(
29
			[
30
				new TestDomainEvent([]),
31
				new TestDomainEvent([]),
32
				new TestTranslatableDomainErrorDomainEvent([]),
33
				new TestTranslatableDomainErrorDomainEvent([]),
34
			]
35
		);
36
		$result = $this->whenGetEventsOfType(TestTranslatableDomainErrorDomainEvent::class);
37
38
		self::assertCount(2, $result);
39
	}
40
41
	public function testGetDomainEventOfType_WhenNoEvents_ShouldReturnNull()
42
	{
43
		$this->givenCollectionWithEvents([]);
44
45
		$result = $this->whenGetEventOfType(TestDomainEvent::class);
46
47
		self::assertNull($result);
48
	}
49
50
	public function testGetDomainEventOfType_WhenTwoEvents_ShouldThrowMoreThanOneEventOfRequestedTypeExists()
51
	{
52
		$this->givenCollectionWithEvents(
53
			[
54
				new TestDomainEvent([]),
55
				new TestDomainEvent([]),
56
			]
57
		);
58
		$this->expectException(MoreThanOneEventOfRequestedTypeExists::class);
59
60
		$this->whenGetEventOfType(TestDomainEvent::class);
61
	}
62
63
	public function testGetDomainEventOfType_WhenOneEvent_ShouldReturnEvent()
64
	{
65
		$this->givenCollectionWithEvents(
66
			[
67
				new TestDomainEvent([]),
68
			]
69
		);
70
		$result = $this->whenGetEventOfType(TestDomainEvent::class);
71
72
		self::assertInstanceOf(TestDomainEvent::class, $result);
73
	}
74
75
	private function givenCollectionWithEvents(array $events): void
76
	{
77
		$this->domainEventCollection = new DomainEventCollection($events);
78
	}
79
80
	private function whenGetEventsOfType(string $type): array
81
	{
82
		return $this->domainEventCollection->getDomainEventsOfType($type);
83
	}
84
85
	private function whenGetEventOfType(string $type): ?DomainEvent
86
	{
87
		return $this->domainEventCollection->getDomainEventOfType($type);
88
	}
89
}
90