Passed
Pull Request — 1.x (#6)
by Kevin
06:51
created

EnvelopeCollection::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Zenstruck\Messenger\Test;
4
5
use PHPUnit\Framework\Assert as PHPUnit;
6
use Symfony\Component\Messenger\Envelope;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
final class EnvelopeCollection implements \IteratorAggregate, \Countable
12
{
13
    private array $envelopes;
14
15
    /**
16
     * @internal
17
     */
18
    public function __construct(Envelope ...$envelopes)
19
    {
20
        $this->envelopes = $envelopes;
21
    }
22
23
    public function assertEmpty(): self
24
    {
25
        return $this->assertCount(0);
26
    }
27
28
    public function assertNotEmpty(): self
29
    {
30
        PHPUnit::assertNotEmpty($this, 'Expected some messages but found none.');
31
32
        return $this;
33
    }
34
35
    public function assertCount(int $count): self
36
    {
37
        PHPUnit::assertCount($count, $this->envelopes, \sprintf('Expected %d messages, but %d messages found.', $count, \count($this->envelopes)));
38
39
        return $this;
40
    }
41
42
    public function assertContains(string $messageClass, ?int $times = null): self
43
    {
44
        $actual = $this->messages($messageClass);
45
46
        PHPUnit::assertNotEmpty($actual, "Message \"{$messageClass}\" not found.");
47
48
        if (null !== $times) {
49
            PHPUnit::assertCount($times, $actual, \sprintf('Expected to find message "%s" %d times but found %d times.', $messageClass, $times, \count($actual)));
50
        }
51
52
        return $this;
53
    }
54
55
    public function assertNotContains(string $messageClass): self
56
    {
57
        $actual = $this->messages($messageClass);
58
59
        PHPUnit::assertEmpty($actual, "Found message \"{$messageClass}\" but should not.");
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param string|callable|null $filter
66
     */
67
    public function first($filter = null): TestEnvelope
68
    {
69
        if (null === $filter) {
70
            // just the first envelope
71
            return $this->first(fn() => true);
72
        }
73
74
        if (!\is_callable($filter)) {
75
            // first envelope for message class
76
            return $this->first(fn(Envelope $e) => $filter === \get_class($e->getMessage()));
77
        }
78
79
        foreach ($this->envelopes as $envelope) {
80
            if ($filter($envelope)) {
81
                return new TestEnvelope($envelope);
82
            }
83
        }
84
85
        throw new \RuntimeException('No envelopes found.');
86
    }
87
88
    /**
89
     * The messages extracted from envelopes.
90
     *
91
     * @param string|null $class Only messages of this class
92
     *
93
     * @return object[]
94
     */
95
    public function messages(?string $class = null): array
96
    {
97
        $messages = \array_map(static fn(Envelope $envelope) => $envelope->getMessage(), $this->envelopes);
98
99
        if (!$class) {
100
            return $messages;
101
        }
102
103
        return \array_values(\array_filter($messages, static fn(object $message) => $class === \get_class($message)));
104
    }
105
106
    /**
107
     * @return TestEnvelope[]
108
     */
109
    public function all(): array
110
    {
111
        return \iterator_to_array($this);
112
    }
113
114
    public function getIterator(): \Iterator
115
    {
116
        foreach ($this->envelopes as $envelope) {
117
            yield new TestEnvelope($envelope);
118
        }
119
    }
120
121
    public function count(): int
122
    {
123
        return \count($this->envelopes);
124
    }
125
}
126