Passed
Push — master ( 0675d7...59fbae )
by Kevin
02:33
created

MessageTaskRunnerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 77
c 1
b 0
f 1
dl 0
loc 163
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A is_sent_to_multiple_transports() 0 22 1
A is_handled_by_multiple_handlers() 0 26 1
A createBuilder() 0 4 1
A not_configured() 0 7 1
A is_sent_to_transport() 0 14 1
A fails_if_not_handled_or_sent() 0 14 1
A is_handled_and_sent_to_transport() 0 22 1
A is_handled() 0 14 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Tests\Schedule\Task\Runner;
4
5
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Symfony\Component\Messenger\Envelope;
7
use Symfony\Component\Messenger\MessageBusInterface;
8
use Symfony\Component\Messenger\Stamp\HandledStamp;
9
use Symfony\Component\Messenger\Stamp\SentStamp;
10
use Zenstruck\ScheduleBundle\Schedule\Exception\MissingDependency;
11
use Zenstruck\ScheduleBundle\Schedule\Task\MessageTask;
12
use Zenstruck\ScheduleBundle\Schedule\Task\Runner\MessageTaskRunner;
13
use Zenstruck\ScheduleBundle\Tests\Fixture\MockScheduleBuilder;
14
15
/**
16
 * @author Kevin Bond <[email protected]>
17
 */
18
final class MessageTaskRunnerTest extends TestCase
19
{
20
    /**
21
     * @test
22
     */
23
    public function fails_if_not_handled_or_sent(): void
24
    {
25
        $bus = new MockMessageBus(new Envelope($this));
26
27
        $this->assertNull($bus->message);
28
29
        $context = self::createBuilder($bus)
30
            ->addTask(new MessageTask($this))
31
            ->run()
32
        ;
33
34
        $this->assertInstanceOf(self::class, $bus->message);
35
        $this->assertFalse($context->isSuccessful());
36
        $this->assertSame('Message not handled or sent to transport.', $context->getFailures()[0]->getDescription());
37
    }
38
39
    /**
40
     * @test
41
     */
42
    public function is_handled(): void
43
    {
44
        $bus = new MockMessageBus(new Envelope($this, [new HandledStamp(null, 'my handler')]));
45
46
        $this->assertNull($bus->message);
47
48
        $context = self::createBuilder($bus)
49
            ->addTask(new MessageTask($this))
50
            ->run()
51
        ;
52
53
        $this->assertInstanceOf(self::class, $bus->message);
54
        $this->assertTrue($context->isSuccessful());
55
        $this->assertSame('Handled by: "my handler", return: (none)', $context->getSuccessful()[0]->getOutput());
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function is_handled_by_multiple_handlers(): void
62
    {
63
        $bus = new MockMessageBus(new Envelope($this, [
64
            new HandledStamp(null, 'handler 1'),
65
            new HandledStamp(['foo'], 'handler 2'),
66
            new HandledStamp('bar', 'handler 3'),
67
            new HandledStamp(17, 'handler 3'),
68
        ]));
69
70
        $this->assertNull($bus->message);
71
72
        $context = self::createBuilder($bus)
73
            ->addTask(new MessageTask($this))
74
            ->run()
75
        ;
76
77
        $this->assertInstanceOf(self::class, $bus->message);
78
        $this->assertTrue($context->isSuccessful());
79
        $this->assertSame(
80
            \implode("\n", [
81
                'Handled by: "handler 1", return: (none)',
82
                'Handled by: "handler 2", return: (array)',
83
                'Handled by: "handler 3", return: (string) "bar"',
84
                'Handled by: "handler 3", return: (int) "17"',
85
            ]),
86
            $context->getSuccessful()[0]->getOutput()
87
        );
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function is_sent_to_transport(): void
94
    {
95
        $bus = new MockMessageBus(new Envelope($this, [new SentStamp('my transport')]));
96
97
        $this->assertNull($bus->message);
98
99
        $context = self::createBuilder($bus)
100
            ->addTask(new MessageTask($this))
101
            ->run()
102
        ;
103
104
        $this->assertInstanceOf(self::class, $bus->message);
105
        $this->assertTrue($context->isSuccessful());
106
        $this->assertSame('Sent to: "my transport"', $context->getSuccessful()[0]->getOutput());
107
    }
108
109
    /**
110
     * @test
111
     */
112
    public function is_sent_to_multiple_transports(): void
113
    {
114
        $bus = new MockMessageBus(new Envelope($this, [
115
            new SentStamp('transport 1'),
116
            new SentStamp('transport 2'),
117
        ]));
118
119
        $this->assertNull($bus->message);
120
121
        $context = self::createBuilder($bus)
122
            ->addTask(new MessageTask($this))
123
            ->run()
124
        ;
125
126
        $this->assertInstanceOf(self::class, $bus->message);
127
        $this->assertTrue($context->isSuccessful());
128
        $this->assertSame(
129
            \implode("\n", [
130
                'Sent to: "transport 1"',
131
                'Sent to: "transport 2"',
132
            ]),
133
            $context->getSuccessful()[0]->getOutput()
134
        );
135
    }
136
137
    /**
138
     * @test
139
     */
140
    public function is_handled_and_sent_to_transport(): void
141
    {
142
        $bus = new MockMessageBus(new Envelope($this, [
143
            new HandledStamp(null, 'handler'),
144
            new SentStamp('transport'),
145
        ]));
146
147
        $this->assertNull($bus->message);
148
149
        $context = self::createBuilder($bus)
150
            ->addTask(new MessageTask($this))
151
            ->run()
152
        ;
153
154
        $this->assertInstanceOf(self::class, $bus->message);
155
        $this->assertTrue($context->isSuccessful());
156
        $this->assertSame(
157
            \implode("\n", [
158
                'Handled by: "handler", return: (none)',
159
                'Sent to: "transport"',
160
            ]),
161
            $context->getSuccessful()[0]->getOutput()
162
        );
163
    }
164
165
    /**
166
     * @test
167
     */
168
    public function not_configured(): void
169
    {
170
        $context = (new MockScheduleBuilder())->addTask(new MessageTask($this))->run();
171
172
        $this->assertFalse($context->isSuccessful());
173
        $this->assertInstanceOf(MissingDependency::class, $context->getFailures()[0]->getException());
174
        $this->assertStringContainsString('you must install symfony/messenger', $context->getFailures()[0]->getDescription());
175
    }
176
177
    private static function createBuilder(MessageBusInterface $bus): MockScheduleBuilder
178
    {
179
        return (new MockScheduleBuilder())
180
            ->addRunner(new MessageTaskRunner($bus))
181
        ;
182
    }
183
}
184
185
final class MockMessageBus implements MessageBusInterface
186
{
187
    public $message;
188
    private $return;
189
190
    public function __construct(Envelope $return)
191
    {
192
        $this->return = $return;
193
    }
194
195
    public function dispatch($message, array $stamps = []): Envelope
196
    {
197
        $this->message = $message;
198
199
        return $this->return;
200
    }
201
}
202