Completed
Push — master ( d68d48...ce9b81 )
by Beñat
8s
created

DomainPublishedMatcher::matches()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
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
 * Usage:
21
 *    $this->shouldHavePublished(EventPublished::class);.
22
 */
23
class DomainPublishedMatcher extends BasicMatcher
24
{
25
    protected function matches($subject, array $arguments)
26
    {
27
        foreach ($subject->recordedEvents() as $event) {
28
            if ($event instanceof $arguments[0]) {
29
                return true;
30
            }
31
        }
32
33
        return false;
34
    }
35
36
    protected function getFailureException($name, $subject, array $arguments)
37
    {
38
        return new FailureException(
39
            sprintf(
40
                'Expected an event of type %s to be published. Found other %d event(s)',
41
                $arguments[0],
42
                count($subject->recordedEvents())
43
            )
44
        );
45
    }
46
47
    protected function getNegativeFailureException($name, $subject, array $arguments)
48
    {
49
        return new FailureException(
50
            sprintf(
51
                'Expected an event of type %s not to be published but it was.',
52
                $arguments[0]
53
            )
54
        );
55
    }
56
57
    public function supports($name, $subject, array $arguments)
58
    {
59
        return $name === 'havePublished' && $subject instanceof AggregateRoot;
60
    }
61
}
62