Completed
Pull Request — master (#129)
by Gorka
02:26
created

DomainPublishedMatcher::supports()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Kreta\SharedKernel\Tests\Matchers;
14
15
use Kreta\SharedKernel\Domain\Model\AggregateRoot;
16
use PhpSpec\Exception\Example\FailureException;
17
use PhpSpec\Matcher\BasicMatcher;
18
19
/**
20
 * Class DomainPublishedMatcher
21
 *
22
 * Usage:
23
 *    $this->shouldHavePublished(EventPublished::class);
24
 * 
25
 * @package Kreta\SharedKernel\Tests\Matchers
26
 */
27
class DomainPublishedMatcher extends BasicMatcher
28
{
29
    protected function matches($subject, array $arguments)
30
    {
31
        /** @var AggregateRoot $subject */
32
        foreach ($subject->recordedEvents() as $event) {
33
            if ($event instanceof $arguments[0]) {
34
                return true;
35
            }
36
        }
37
38
        return false;
39
    }
40
41
    protected function getFailureException($name, $subject, array $arguments)
42
    {
43
        /* @var AggregateRoot $subject */
44
        return new FailureException(
45
            sprintf(
46
                'Expected an event of type %s to be published. Found other %d event(s)',
47
                $arguments[0],
48
                count($subject->recordedEvents())
49
            )
50
        );
51
    }
52
53
    protected function getNegativeFailureException($name, $subject, array $arguments)
54
    {
55
        return new FailureException(
56
            sprintf(
57
                'Expected an event of type %s not to be published but it was.',
58
                $arguments[0]
59
            )
60
        );
61
    }
62
63
    public function supports($name, $subject, array $arguments)
64
    {
65
        return $name === 'havePublished' && $subject instanceof AggregateRoot;
66
    }
67
}
68