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

ProjectorTests::testCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 9.4109
c 0
b 0
f 0
cc 1
eloc 45
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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