|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Tests\Domain\Author; |
|
6
|
|
|
|
|
7
|
|
|
use App\Domain\Author\Author; |
|
8
|
|
|
use App\Domain\Author\Events\AuthorNameWasChanged; |
|
9
|
|
|
use App\Domain\Author\Events\AuthorWasCreated; |
|
10
|
|
|
use App\Domain\Author\Events\AuthorWasDeleted; |
|
11
|
|
|
use App\Domain\Category\Exception\SameNameException; |
|
12
|
|
|
use App\Domain\Common\ValueObject\AggregateRootId; |
|
13
|
|
|
use App\Domain\Common\ValueObject\Name; |
|
14
|
|
|
use Tests\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
class AuthorTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function test_it_create() |
|
19
|
|
|
{ |
|
20
|
|
|
$author = Author::create(AggregateRootId::generate(), Name::fromString('test')); |
|
21
|
|
|
$this->assertInstanceOf(Author::class, $author); |
|
22
|
|
|
$events = $this->popRecordedEvent($author); |
|
23
|
|
|
$this->assertEquals(1, \count($events)); |
|
24
|
|
|
$this->assertInstanceOf(AuthorWasCreated::class, $events[0]); |
|
25
|
|
|
$expectedPayload = [ |
|
26
|
|
|
'name' => 'test', |
|
27
|
|
|
]; |
|
28
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function test_it_change_name() |
|
32
|
|
|
{ |
|
33
|
|
|
$author = Author::create(AggregateRootId::generate(), Name::fromString('test')); |
|
34
|
|
|
$this->assertInstanceOf(Author::class, $author); |
|
35
|
|
|
$events = $this->popRecordedEvent($author); |
|
36
|
|
|
$this->assertEquals(1, \count($events)); |
|
37
|
|
|
$this->assertInstanceOf(AuthorWasCreated::class, $events[0]); |
|
38
|
|
|
$expectedPayload = [ |
|
39
|
|
|
'name' => 'test', |
|
40
|
|
|
]; |
|
41
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
42
|
|
|
$author->changeName('test2'); |
|
43
|
|
|
$events = $this->popRecordedEvent($author); |
|
44
|
|
|
$this->assertEquals(1, \count($events)); |
|
45
|
|
|
$this->assertInstanceOf(AuthorNameWasChanged::class, $events[0]); |
|
46
|
|
|
$expectedPayload = [ |
|
47
|
|
|
'name' => 'test2', |
|
48
|
|
|
]; |
|
49
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function test_it_change_same_name() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->expectException(SameNameException::class); |
|
55
|
|
|
$author = Author::create(AggregateRootId::generate(), Name::fromString('test')); |
|
56
|
|
|
$this->assertInstanceOf(Author::class, $author); |
|
57
|
|
|
$events = $this->popRecordedEvent($author); |
|
58
|
|
|
$this->assertEquals(1, \count($events)); |
|
59
|
|
|
$this->assertInstanceOf(AuthorWasCreated::class, $events[0]); |
|
60
|
|
|
$expectedPayload = [ |
|
61
|
|
|
'name' => 'test', |
|
62
|
|
|
]; |
|
63
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
64
|
|
|
$author->changeName('test'); |
|
65
|
|
|
$events = $this->popRecordedEvent($author); |
|
66
|
|
|
$this->assertEquals(1, \count($events)); |
|
67
|
|
|
$this->assertInstanceOf(AuthorNameWasChanged::class, $events[0]); |
|
68
|
|
|
$expectedPayload = [ |
|
69
|
|
|
'name' => 'test2', |
|
70
|
|
|
]; |
|
71
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function test_it_delete_author() |
|
75
|
|
|
{ |
|
76
|
|
|
$author = Author::create(AggregateRootId::generate(), Name::fromString('test')); |
|
77
|
|
|
$this->assertInstanceOf(Author::class, $author); |
|
78
|
|
|
$events = $this->popRecordedEvent($author); |
|
79
|
|
|
$this->assertEquals(1, \count($events)); |
|
80
|
|
|
$this->assertInstanceOf(AuthorWasCreated::class, $events[0]); |
|
81
|
|
|
$expectedPayload = [ |
|
82
|
|
|
'name' => 'test', |
|
83
|
|
|
]; |
|
84
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
85
|
|
|
$author->delete(); |
|
86
|
|
|
$events = $this->popRecordedEvent($author); |
|
87
|
|
|
$this->assertEquals(1, \count($events)); |
|
88
|
|
|
$this->assertInstanceOf(AuthorWasDeleted::class, $events[0]); |
|
89
|
|
|
$expectedPayload = []; |
|
90
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|