Completed
Push — master ( 67ee93...84505b )
by Ivannis Suárez
07:55
created

AggregateRepositoryTests::testPersistAll()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
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;
13
14
use Cubiche\Domain\EventPublisher\DomainEventPublisher;
15
use Cubiche\Domain\EventSourcing\AggregateRepository;
16
use Cubiche\Domain\EventSourcing\EventStore\InMemoryEventStore;
17
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Listener\PostPersistSubscriber;
18
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Listener\PostRemoveSubscriber;
19
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Listener\PrePersistSubscriber;
20
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Listener\PreRemoveSubscriber;
21
use Cubiche\Domain\EventSourcing\Tests\Fixtures\PostEventSourced;
22
use Cubiche\Domain\EventSourcing\Tests\Fixtures\PostEventSourcedFactory;
23
use Cubiche\Domain\Model\Tests\Fixtures\Post;
24
use Cubiche\Domain\Model\Tests\Fixtures\PostId;
25
26
/**
27
 * AggregateRepositoryTests class.
28
 *
29
 * Generated by TestGenerator on 2016-06-28 at 14:36:54.
30
 */
31
class AggregateRepositoryTests extends TestCase
32
{
33
    /**
34
     * @return AggregateRepository
35
     */
36
    protected function createRepository()
37
    {
38
        return new AggregateRepository(new InMemoryEventStore(), PostEventSourced::class);
39
    }
40
41
    /**
42
     * Test Persist method.
43
     */
44
    public function testPersist()
45
    {
46
        $this
47
            ->given($repository = $this->createRepository())
48
            ->and(
49
                $post = PostEventSourcedFactory::create(
50
                    $this->faker->sentence,
51
                    $this->faker->paragraph
52
                )
53
            )
54
            ->and($post->changeTitle($this->faker->sentence))
55
            ->when($repository->persist($post))
56
            ->then()
57
                ->object($repository->get($post->id()))
58
                    ->isEqualTo($post)
59
        ;
60
61
        $this
62
            ->given($repository = $this->createRepository())
63
            ->and(
64
                $post = PostEventSourcedFactory::create(
65
                    $this->faker->sentence,
66
                    $this->faker->paragraph
67
                )
68
            )
69
            ->and($post->clearEvents())
70
            ->when($repository->persist($post))
71
            ->then()
72
                ->exception(function () use ($repository, $post) {
73
                    $repository->get($post->id());
74
                })
75
                ->isInstanceOf(\RuntimeException::class)
76
        ;
77
78
        $this
79
            ->given($repository = $this->createRepository())
80
            ->and(
81
                $post = new Post(
82
                    PostId::fromNative(md5(rand())),
83
                    $this->faker->sentence,
84
                    $this->faker->paragraph
85
                )
86
            )
87
            ->then()
88
                ->exception(function () use ($repository, $post) {
89
                    $repository->persist($post);
90
                })
91
                ->isInstanceOf(\InvalidArgumentException::class)
92
        ;
93
94
        $this
95
            ->given($repository = $this->createRepository())
96
            ->and(
97
                $post = PostEventSourcedFactory::create(
98
                    $this->faker->sentence,
99
                    $this->faker->paragraph
100
                )
101
            )
102
            ->and($post->changeTitle($this->faker->sentence))
103
            ->and($prePersistSubscriber = new PrePersistSubscriber(42))
104
            ->and($postPersistSubscriber = new PostPersistSubscriber())
105
            ->and(DomainEventPublisher::subscribe($prePersistSubscriber))
106
            ->and(DomainEventPublisher::subscribe($postPersistSubscriber))
107
            ->when($repository->persist($post))
108
            ->then()
109
                ->integer($post->version()->patch())
110
                    ->isEqualTo(84)
111
        ;
112
    }
113
114
    /**
115
     * Test PersistAll method.
116
     */
117
    public function testPersistAll()
118
    {
119
        $this
120
            ->given($repository = $this->createRepository())
121
            ->and(
122
                $post1 = PostEventSourcedFactory::create(
123
                    $this->faker->sentence,
124
                    $this->faker->paragraph
125
                )
126
            )
127
            ->and(
128
                $post2 = PostEventSourcedFactory::create(
129
                    $this->faker->sentence,
130
                    $this->faker->paragraph
131
                )
132
            )
133
            ->and($post1->changeTitle($this->faker->sentence))
134
            ->and($post2->changeTitle($this->faker->sentence))
135
            ->when($repository->persistAll(array($post1, $post2)))
136
            ->then()
137
                ->object($repository->get($post1->id()))
138
                    ->isEqualTo($post1)
139
                ->and()
140
                ->object($repository->get($post2->id()))
141
                    ->isEqualTo($post2)
142
        ;
143
    }
144
145
    /**
146
     * Test Remove method.
147
     */
148
    public function testRemove()
149
    {
150
        $this
151
            ->given($repository = $this->createRepository())
152
            ->and(
153
                $post = PostEventSourcedFactory::create(
154
                    $this->faker->sentence,
155
                    $this->faker->paragraph
156
                )
157
            )
158
            ->and($post->changeTitle($this->faker->sentence))
159
            ->when($repository->persist($post))
160
            ->then()
161
                ->object($repository->get($post->id()))
162
                    ->isEqualTo($post)
163
                ->and()
164
                ->when($repository->remove($post))
165
                ->then()
166
                    ->exception(function () use ($repository, $post) {
167
                        $repository->get($post->id());
168
                    })->isInstanceOf(\RuntimeException::class)
169
        ;
170
171
        $this
172
            ->given($repository = $this->createRepository())
173
            ->and(
174
                $post = new Post(
175
                    PostId::fromNative(md5(rand())),
176
                    $this->faker->sentence,
177
                    $this->faker->paragraph
178
                )
179
            )
180
            ->then()
181
                ->exception(function () use ($repository, $post) {
182
                    $repository->remove($post);
183
                })
184
                ->isInstanceOf(\InvalidArgumentException::class)
185
        ;
186
187
        $this
188
            ->given($repository = $this->createRepository())
189
            ->and(
190
                $post = PostEventSourcedFactory::create(
191
                    $this->faker->sentence,
192
                    $this->faker->paragraph
193
                )
194
            )
195
            ->and($repository->persist($post))
196
            ->and($preRemoveSubscriber = new PreRemoveSubscriber(42))
197
            ->and($postRemoveSubscriber = new PostRemoveSubscriber())
198
            ->and(DomainEventPublisher::subscribe($preRemoveSubscriber))
199
            ->and(DomainEventPublisher::subscribe($postRemoveSubscriber))
200
            ->when($repository->remove($post))
201
            ->then()
202
                ->integer($post->version()->patch())
203
                    ->isEqualTo(21)
204
        ;
205
    }
206
}
207