-
Notifications
You must be signed in to change notification settings - Fork 42
/
AggregateRepositoryFactoryTest.php
222 lines (192 loc) · 7.9 KB
/
AggregateRepositoryFactoryTest.php
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* This file is part of prooph/event-sourcing.
* (c) 2014-2021 Alexander Miertsch <kontakt@codeliner.ws>
* (c) 2015-2021 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ProophTest\EventSourcing\Container\Aggregate;
use InvalidArgumentException;
use Prooph\EventSourcing\Aggregate\AggregateTranslator;
use Prooph\EventSourcing\Container\Aggregate\AggregateRepositoryFactory;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Exception\ConfigurationException;
use ProophTest\EventSourcing\Mock\EventStoreMock;
use ProophTest\EventSourcing\Mock\RepositoryMock;
use ProophTest\EventStore\ActionEventEmitterEventStoreTestCase;
use ProophTest\EventStore\Mock\User;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Container\ContainerInterface;
class AggregateRepositoryFactoryTest extends ActionEventEmitterEventStoreTestCase
{
use ProphecyTrait;
/**
* @test
*/
public function it_creates_an_aggregate_from_static_call(): void
{
$container = $this->prophesize(ContainerInterface::class);
$container->has('config')->willReturn(true);
$container->get('config')->willReturn([
'prooph' => [
'event_sourcing' => [
'aggregate_repository' => [
'repository_mock' => [
'repository_class' => RepositoryMock::class,
'aggregate_type' => User::class,
'aggregate_translator' => 'user_translator',
],
],
],
],
]);
$container->get(EventStore::class)->willReturn($this->eventStore);
$userTranslator = $this->prophesize(AggregateTranslator::class);
$container->get('user_translator')->willReturn($userTranslator->reveal());
$factory = [AggregateRepositoryFactory::class, 'repository_mock'];
self::assertInstanceOf(RepositoryMock::class, $factory($container->reveal()));
}
/**
* @test
*/
public function it_creates_an_aggregate_from_static_call_with_custom_event_store(): void
{
$container = $this->prophesize(ContainerInterface::class);
$container->has('config')->willReturn(true);
$container->get('config')->willReturn([
'prooph' => [
'event_sourcing' => [
'aggregate_repository' => [
'repository_mock' => [
'repository_class' => RepositoryMock::class,
'event_store' => EventStoreMock::class,
'aggregate_type' => User::class,
'aggregate_translator' => 'user_translator',
],
],
],
],
]);
$container->get(EventStoreMock::class)->willReturn($this->eventStore);
$userTranslator = $this->prophesize(AggregateTranslator::class);
$container->get('user_translator')->willReturn($userTranslator->reveal());
$factory = [AggregateRepositoryFactory::class, 'repository_mock'];
self::assertInstanceOf(RepositoryMock::class, $factory($container->reveal()));
}
/**
* @test
*/
public function it_throws_invalid_argument_exception_without_container_on_static_call(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The first argument must be of type Psr\Container\ContainerInterface');
AggregateRepositoryFactory::other_config_id();
}
/**
* @test
*/
public function it_throws_exception_when_unknown_repository_class_given(): void
{
$this->expectException(ConfigurationException::class);
$container = $this->prophesize(ContainerInterface::class);
$container->has('config')->willReturn(true);
$container->get('config')->willReturn([
'prooph' => [
'event_sourcing' => [
'aggregate_repository' => [
'repository_mock' => [
'repository_class' => 'invalid',
'aggregate_type' => User::class,
'aggregate_translator' => 'user_translator',
],
],
],
],
]);
$factory = new AggregateRepositoryFactory('repository_mock');
$factory->__invoke($container->reveal());
}
/**
* @test
*/
public function it_throws_exception_when_invalid_repository_class_given(): void
{
$this->expectException(ConfigurationException::class);
$container = $this->prophesize(ContainerInterface::class);
$container->has('config')->willReturn(true);
$container->get('config')->willReturn([
'prooph' => [
'event_sourcing' => [
'aggregate_repository' => [
'repository_mock' => [
'repository_class' => 'stdClass',
'aggregate_type' => User::class,
'aggregate_translator' => 'user_translator',
],
],
],
],
]);
$factory = new AggregateRepositoryFactory('repository_mock');
$factory->__invoke($container->reveal());
}
/**
* @test
*/
public function it_throws_exception_when_invalid_event_store_class_given(): void
{
$this->expectException(\TypeError::class);
$container = $this->prophesize(ContainerInterface::class);
$container->has('config')->willReturn(true);
$container->get('config')->willReturn([
'prooph' => [
'event_sourcing' => [
'aggregate_repository' => [
'repository_mock' => [
'repository_class' => RepositoryMock::class,
'event_store' => 'stdClass',
'aggregate_type' => User::class,
'aggregate_translator' => 'user_translator',
],
],
],
],
]);
$container->get('stdClass')->willReturn('stdClass');
$userTranslator = $this->prophesize(AggregateTranslator::class);
$container->get('user_translator')->willReturn($userTranslator->reveal());
$factory = [AggregateRepositoryFactory::class, 'repository_mock'];
self::assertInstanceOf(RepositoryMock::class, $factory($container->reveal()));
}
/**
* @test
*/
public function it_uses_given_aggregate_type_mapping(): void
{
$container = $this->prophesize(ContainerInterface::class);
$container->has('config')->willReturn(true);
$container->get('config')->willReturn([
'prooph' => [
'event_sourcing' => [
'aggregate_repository' => [
'repository_mock' => [
'repository_class' => RepositoryMock::class,
'aggregate_type' => [
'user' => User::class,
],
'aggregate_translator' => 'user_translator',
],
],
],
],
]);
$container->get(EventStore::class)->willReturn($this->eventStore);
$userTranslator = $this->prophesize(AggregateTranslator::class);
$container->get('user_translator')->willReturn($userTranslator->reveal());
$factory = [AggregateRepositoryFactory::class, 'repository_mock'];
self::assertInstanceOf(RepositoryMock::class, $factory($container->reveal()));
}
}