1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche/EventSourcing component. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Domain\EventSourcing\Tests\Units\Projector; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Specification\Criteria; |
15
|
|
|
use Cubiche\Domain\EventPublisher\DomainEventPublisher; |
16
|
|
|
use Cubiche\Domain\EventSourcing\AggregateRepository; |
17
|
|
|
use Cubiche\Domain\EventSourcing\EventStore\EventStoreInterface; |
18
|
|
|
use Cubiche\Domain\EventSourcing\EventStore\InMemoryEventStore; |
19
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\PostEventSourced; |
20
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Projector\PublishedPostProjector; |
21
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\ReadModel\PublishedPost; |
22
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Units\TestCase; |
23
|
|
|
use Cubiche\Domain\Model\Tests\Fixtures\PostId; |
24
|
|
|
use Cubiche\Domain\Repository\InMemory\InMemoryQueryRepository; |
25
|
|
|
use Cubiche\Domain\Repository\QueryRepositoryInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ProjectorTests class. |
29
|
|
|
* |
30
|
|
|
* Generated by TestGenerator on 2016-06-28 at 14:36:54. |
31
|
|
|
*/ |
32
|
|
|
class ProjectorTests extends TestCase |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
protected function createProjector(QueryRepositoryInterface $repository, EventStoreInterface $eventStore) |
38
|
|
|
{ |
39
|
|
|
return new PublishedPostProjector($repository, $eventStore); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Test create. |
44
|
|
|
*/ |
45
|
|
|
public function testCreate() |
46
|
|
|
{ |
47
|
|
|
$eventStore = new InMemoryEventStore(); |
48
|
|
|
$readModelRepository = new InMemoryQueryRepository(PublishedPost::class); |
49
|
|
|
$writeModelRepository = new AggregateRepository($eventStore, PostEventSourced::class); |
50
|
|
|
|
51
|
|
|
$this |
52
|
|
|
->given($projector = $this->createProjector($readModelRepository, $eventStore)) |
53
|
|
|
->and(DomainEventPublisher::subscribe($projector)) |
54
|
|
|
->and( |
55
|
|
|
$postId = PostId::fromNative(md5(rand())), |
56
|
|
|
$writeModel = new PostEventSourced( |
57
|
|
|
$postId, |
58
|
|
|
'The post title', |
59
|
|
|
'The post description' |
60
|
|
|
) |
61
|
|
|
) |
62
|
|
|
->then() |
63
|
|
|
->boolean($readModelRepository->isEmpty()) |
64
|
|
|
->isTrue() |
65
|
|
|
->and() |
66
|
|
|
->when($writeModelRepository->persist($writeModel)) |
67
|
|
|
->then() |
68
|
|
|
->boolean($readModelRepository->isEmpty()) |
69
|
|
|
->isTrue() |
70
|
|
|
->and() |
71
|
|
|
->when($writeModel->publish()) |
72
|
|
|
->and($writeModelRepository->persist($writeModel)) |
73
|
|
|
->then() |
74
|
|
|
->boolean($readModelRepository->isEmpty()) |
75
|
|
|
->isFalse() |
76
|
|
|
->object($readModel = $readModelRepository->findOne(Criteria::property('id')->eq($postId))) |
77
|
|
|
->isInstanceOf(PublishedPost::class) |
78
|
|
|
->string($readModel->title()) |
79
|
|
|
->isEqualTo('The post title') |
80
|
|
|
->and() |
81
|
|
|
->when($writeModel->changeTitle('foo')) |
82
|
|
|
->and($writeModelRepository->persist($writeModel)) |
83
|
|
|
->then() |
84
|
|
|
->object($readModel = $readModelRepository->findOne(Criteria::property('id')->eq($postId))) |
85
|
|
|
->isInstanceOf(PublishedPost::class) |
86
|
|
|
->string($readModel->title()) |
87
|
|
|
->isEqualTo('foo') |
88
|
|
|
->and() |
89
|
|
|
->when($writeModel->unpublish()) |
90
|
|
|
->and($writeModelRepository->persist($writeModel)) |
91
|
|
|
->then() |
92
|
|
|
->boolean($readModelRepository->isEmpty()) |
93
|
|
|
->isTrue() |
94
|
|
|
; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|