Passed
Push — master ( 751812...ad3fb0 )
by Kevin
02:24
created

EmailExtensionTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 252
rs 10
c 0
b 0
f 0
eloc 114

29 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$3 ➔ buildSchedule() 0 3 1
A hp$4 ➔ createMailer() 0 9 1
createMailer() 0 9 ?
A hp$0 ➔ buildSchedule() 0 3 1
sends_schedule_failure_email_with_configured_subject_prefix() 0 19 ?
A hp$2 ➔ sends_task_failure_email() 0 18 1
A hp$1 ➔ sends_schedule_failure_email_with_overrides() 0 23 1
A hp$2 ➔ sends_task_failure_email_with_configured_subject_prefix() 0 13 1
A hp$2 ➔ sends_after_task_email_with_overrides() 0 17 1
A hp$2 ➔ sends_schedule_failure_email_with_configured_subject_prefix() 0 19 1
to_address_must_be_configured_or_passed_to_extension() 0 10 ?
A hp$4 ➔ send() 0 3 1
A hp$1 ➔ buildSchedule() 0 4 1
A hp$3 ➔ to_address_must_be_configured_or_passed_to_extension() 0 10 1
sends_task_failure_email_with_configured_subject_prefix() 0 13 ?
sends_schedule_failure_email() 0 28 ?
A hp$2 ➔ sends_after_task_email() 0 16 1
sends_after_task_email_with_configured_subject_prefix() 0 11 ?
sends_task_failure_email_with_overrides() 0 17 ?
A hp$2 ➔ buildSchedule() 0 3 1
sends_after_task_email_with_overrides() 0 17 ?
A hp$2 ➔ sends_task_failure_email_with_overrides() 0 17 1
sends_task_failure_email() 0 18 ?
A hp$2 ➔ sends_after_task_email_with_configured_subject_prefix() 0 11 1
A hp$3 ➔ provides_helpful_message_if_handler_not_configured() 0 13 1
A hp$0 ➔ sends_schedule_failure_email() 0 28 1
sends_schedule_failure_email_with_overrides() 0 23 ?
provides_helpful_message_if_handler_not_configured() 0 13 ?
sends_after_task_email() 0 16 ?
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Tests\Schedule\Extension;
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\Mailer\Envelope;
7
use Symfony\Component\Mailer\MailerInterface;
8
use Symfony\Component\Mime\Email;
9
use Symfony\Component\Mime\RawMessage;
10
use Zenstruck\ScheduleBundle\Schedule;
11
use Zenstruck\ScheduleBundle\Schedule\Extension\Handler\EmailHandler;
12
use Zenstruck\ScheduleBundle\Schedule\ScheduleBuilder;
13
use Zenstruck\ScheduleBundle\Tests\Fixture\MockScheduleBuilder;
14
use Zenstruck\ScheduleBundle\Tests\Fixture\MockTask;
15
16
/**
17
 * @author Kevin Bond <[email protected]>
18
 */
19
final class EmailExtensionTest extends TestCase
20
{
21
    /**
22
     * @test
23
     */
24
    public function sends_schedule_failure_email()
25
    {
26
        $mailer = $this->createMailer();
27
28
        (new MockScheduleBuilder())
29
            ->addHandler(new EmailHandler($mailer, '[email protected]', '[email protected]'))
30
            ->addBuilder(new class() implements ScheduleBuilder {
31
                public function buildSchedule(Schedule $schedule): void
32
                {
33
                    $schedule->emailOnFailure();
34
                }
35
            })
36
            ->addTask(MockTask::exception(new \Exception('task 1 exception message'), 'my task 1'))
37
            ->addTask(MockTask::success('my task 2'))
38
            ->addTask(MockTask::exception(new \Exception('task 3 exception message'), 'my task 3'))
39
            ->run()
40
        ;
41
42
        $this->assertSame('[email protected]', $mailer->lastMessage->getFrom()[0]->getAddress());
0 ignored issues
show
Bug introduced by
The method getFrom() does not exist on Symfony\Component\Mime\RawMessage. It seems like you code against a sub-type of Symfony\Component\Mime\RawMessage such as Symfony\Component\Mime\Email. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $this->assertSame('[email protected]', $mailer->lastMessage->/** @scrutinizer ignore-call */ getFrom()[0]->getAddress());
Loading history...
43
        $this->assertSame('[email protected]', $mailer->lastMessage->getTo()[0]->getAddress());
0 ignored issues
show
Bug introduced by
The method getTo() does not exist on Symfony\Component\Mime\RawMessage. It seems like you code against a sub-type of Symfony\Component\Mime\RawMessage such as Symfony\Component\Mime\Email. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $this->assertSame('[email protected]', $mailer->lastMessage->/** @scrutinizer ignore-call */ getTo()[0]->getAddress());
Loading history...
44
        $this->assertSame('[Scheduled Failed] 2 tasks failed', $mailer->lastMessage->getSubject());
0 ignored issues
show
Bug introduced by
The method getSubject() does not exist on Symfony\Component\Mime\RawMessage. It seems like you code against a sub-type of Symfony\Component\Mime\RawMessage such as Symfony\Component\Mime\Email. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        $this->assertSame('[Scheduled Failed] 2 tasks failed', $mailer->lastMessage->/** @scrutinizer ignore-call */ getSubject());
Loading history...
45
        $this->assertStringContainsString('2 tasks failed', $mailer->lastMessage->getTextBody());
0 ignored issues
show
Bug introduced by
The method getTextBody() does not exist on Symfony\Component\Mime\RawMessage. It seems like you code against a sub-type of Symfony\Component\Mime\RawMessage such as Symfony\Component\Mime\Email. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        $this->assertStringContainsString('2 tasks failed', $mailer->lastMessage->/** @scrutinizer ignore-call */ getTextBody());
Loading history...
46
        $this->assertStringContainsString('# (Failure 1/2) MockTask: my task 1', $mailer->lastMessage->getTextBody());
47
        $this->assertStringContainsString('## Exception', $mailer->lastMessage->getTextBody());
48
        $this->assertStringContainsString('Exception: task 1 exception message', $mailer->lastMessage->getTextBody());
49
        $this->assertStringContainsString('# (Failure 2/2) MockTask: my task 3', $mailer->lastMessage->getTextBody());
50
        $this->assertStringContainsString('## Exception', $mailer->lastMessage->getTextBody());
51
        $this->assertStringContainsString('Exception: task 3 exception message', $mailer->lastMessage->getTextBody());
52
    }
53
54
    /**
55
     * @test
56
     */
57
    public function sends_schedule_failure_email_with_overrides()
58
    {
59
        $mailer = $this->createMailer();
60
61
        (new MockScheduleBuilder())
62
            ->addHandler(new EmailHandler($mailer, '[email protected]', '[email protected]'))
63
            ->addBuilder(new class() implements ScheduleBuilder {
64
                public function buildSchedule(Schedule $schedule): void
65
                {
66
                    $schedule->emailOnFailure('[email protected]', 'my subject', function (Email $email) {
67
                        $email->cc('[email protected]');
68
                    });
69
                }
70
            })
71
            ->addTask(MockTask::exception(new \Exception('task 1 exception message'), 'my task 1'))
72
            ->addTask(MockTask::success('my task 2'))
73
            ->addTask(MockTask::exception(new \Exception('task 3 exception message'), 'my task 3'))
74
            ->run()
75
        ;
76
77
        $this->assertSame('[email protected]', $mailer->lastMessage->getTo()[0]->getAddress());
78
        $this->assertSame('my subject', $mailer->lastMessage->getSubject());
79
        $this->assertSame('[email protected]', $mailer->lastMessage->getCc()[0]->getAddress());
0 ignored issues
show
Bug introduced by
The method getCc() does not exist on Symfony\Component\Mime\RawMessage. It seems like you code against a sub-type of Symfony\Component\Mime\RawMessage such as Symfony\Component\Mime\Email. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        $this->assertSame('[email protected]', $mailer->lastMessage->/** @scrutinizer ignore-call */ getCc()[0]->getAddress());
Loading history...
80
    }
81
82
    /**
83
     * @test
84
     */
85
    public function sends_schedule_failure_email_with_configured_subject_prefix()
86
    {
87
        $mailer = $this->createMailer();
88
89
        (new MockScheduleBuilder())
90
            ->addHandler(new EmailHandler($mailer, '[email protected]', '[email protected]', '[Acme Inc]'))
91
            ->addBuilder(new class() implements ScheduleBuilder {
92
                public function buildSchedule(Schedule $schedule): void
93
                {
94
                    $schedule->emailOnFailure();
95
                }
96
            })
97
            ->addTask(MockTask::exception(new \Exception('task 1 exception message'), 'my task 1'))
98
            ->addTask(MockTask::success('my task 2'))
99
            ->addTask(MockTask::exception(new \Exception('task 3 exception message'), 'my task 3'))
100
            ->run()
101
        ;
102
103
        $this->assertSame('[Acme Inc][Scheduled Failed] 2 tasks failed', $mailer->lastMessage->getSubject());
104
    }
105
106
    /**
107
     * @test
108
     */
109
    public function sends_task_failure_email()
110
    {
111
        $mailer = $this->createMailer();
112
113
        (new MockScheduleBuilder())
114
            ->addHandler(new EmailHandler($mailer, '[email protected]', '[email protected]'))
115
            ->addTask(MockTask::failure('Exit 127: Command not found', 'my task', 'sh: 1: sdsdsd: not found')
116
                ->emailOnFailure()
117
            )
118
            ->run()
119
        ;
120
121
        $this->assertSame('[email protected]', $mailer->lastMessage->getFrom()[0]->getAddress());
122
        $this->assertSame('[email protected]', $mailer->lastMessage->getTo()[0]->getAddress());
123
        $this->assertSame('[Scheduled Task Failed] MockTask: my task', $mailer->lastMessage->getSubject());
124
        $this->assertStringContainsString('Exit 127: Command not found', $mailer->lastMessage->getTextBody());
125
        $this->assertStringContainsString('## Task Output:', $mailer->lastMessage->getTextBody());
126
        $this->assertStringContainsString('sh: 1: sdsdsd: not found', $mailer->lastMessage->getTextBody());
127
    }
128
129
    /**
130
     * @test
131
     */
132
    public function sends_task_failure_email_with_overrides()
133
    {
134
        $mailer = $this->createMailer();
135
136
        (new MockScheduleBuilder())
137
            ->addHandler(new EmailHandler($mailer, '[email protected]', '[email protected]'))
138
            ->addTask(MockTask::failure('Exit 127: Command not found', 'my task', 'sh: 1: sdsdsd: not found')
139
                ->emailOnFailure('[email protected]', 'my subject', function (Email $email) {
140
                    $email->cc('[email protected]');
141
                })
142
            )
143
            ->run()
144
        ;
145
146
        $this->assertSame('[email protected]', $mailer->lastMessage->getTo()[0]->getAddress());
147
        $this->assertSame('my subject', $mailer->lastMessage->getSubject());
148
        $this->assertSame('[email protected]', $mailer->lastMessage->getCc()[0]->getAddress());
149
    }
150
151
    /**
152
     * @test
153
     */
154
    public function sends_task_failure_email_with_configured_subject_prefix()
155
    {
156
        $mailer = $this->createMailer();
157
158
        (new MockScheduleBuilder())
159
            ->addHandler(new EmailHandler($mailer, '[email protected]', '[email protected]', '[Acme Inc]'))
160
            ->addTask(MockTask::failure('Exit 127: Command not found', 'my task', 'sh: 1: sdsdsd: not found')
161
                ->emailOnFailure()
162
            )
163
            ->run()
164
        ;
165
166
        $this->assertSame('[Acme Inc][Scheduled Task Failed] MockTask: my task', $mailer->lastMessage->getSubject());
167
    }
168
169
    /**
170
     * @test
171
     */
172
    public function sends_after_task_email()
173
    {
174
        $mailer = $this->createMailer();
175
176
        (new MockScheduleBuilder())
177
            ->addHandler(new EmailHandler($mailer, '[email protected]', '[email protected]'))
178
            ->addTask(MockTask::success('my task', 'my task output')->emailAfter())
179
            ->run()
180
        ;
181
182
        $this->assertSame('[email protected]', $mailer->lastMessage->getFrom()[0]->getAddress());
183
        $this->assertSame('[email protected]', $mailer->lastMessage->getTo()[0]->getAddress());
184
        $this->assertSame('[Scheduled Task Succeeded] MockTask: my task', $mailer->lastMessage->getSubject());
185
        $this->assertStringContainsString('Successful', $mailer->lastMessage->getTextBody());
186
        $this->assertStringContainsString('## Task Output:', $mailer->lastMessage->getTextBody());
187
        $this->assertStringContainsString('my task output', $mailer->lastMessage->getTextBody());
188
    }
189
190
    /**
191
     * @test
192
     */
193
    public function sends_after_task_email_with_overrides()
194
    {
195
        $mailer = $this->createMailer();
196
197
        (new MockScheduleBuilder())
198
            ->addHandler(new EmailHandler($mailer, '[email protected]', '[email protected]'))
199
            ->addTask(MockTask::success('my task', 'my task output')
200
                ->emailAfter('[email protected]', 'my subject', function (Email $email) {
201
                    $email->cc('[email protected]');
202
                })
203
            )
204
            ->run()
205
        ;
206
207
        $this->assertSame('[email protected]', $mailer->lastMessage->getTo()[0]->getAddress());
208
        $this->assertSame('my subject', $mailer->lastMessage->getSubject());
209
        $this->assertSame('[email protected]', $mailer->lastMessage->getCc()[0]->getAddress());
210
    }
211
212
    /**
213
     * @test
214
     */
215
    public function sends_after_task_email_with_configured_subject_prefix()
216
    {
217
        $mailer = $this->createMailer();
218
219
        (new MockScheduleBuilder())
220
            ->addHandler(new EmailHandler($mailer, '[email protected]', '[email protected]', '[Acme Inc]'))
221
            ->addTask(MockTask::success('my task', 'my task output')->emailAfter())
222
            ->run()
223
        ;
224
225
        $this->assertSame('[Acme Inc][Scheduled Task Succeeded] MockTask: my task', $mailer->lastMessage->getSubject());
226
    }
227
228
    /**
229
     * @test
230
     */
231
    public function provides_helpful_message_if_handler_not_configured()
232
    {
233
        $this->expectException(\LogicException::class);
234
        $this->expectExceptionMessage('To use the email extension you must configure a mailer (config path: "zenstruck_schedule.email_handler").');
235
236
        (new MockScheduleBuilder())
237
            ->addBuilder(new class() implements ScheduleBuilder {
238
                public function buildSchedule(Schedule $schedule): void
239
                {
240
                    $schedule->emailOnFailure();
241
                }
242
            })
243
            ->run()
244
        ;
245
    }
246
247
    /**
248
     * @test
249
     */
250
    public function to_address_must_be_configured_or_passed_to_extension()
251
    {
252
        $event = (new MockScheduleBuilder())
253
            ->addHandler(new EmailHandler($this->createMailer()))
254
            ->addTask(MockTask::failure()->emailOnFailure())
255
            ->run()
256
        ;
257
258
        $this->assertInstanceOf(\LogicException::class, $event->getResults()[0]->getException());
259
        $this->assertSame('There is no "To" configured for the email. Either set it when adding the extension or in your configuration (config path: "zenstruck_schedule.email_handler.default_to").', $event->getResults()[0]->getException()->getMessage());
260
    }
261
262
    private function createMailer(): MailerInterface
263
    {
264
        return new class() implements MailerInterface {
265
            /** @var RawMessage */
266
            public $lastMessage;
267
268
            public function send(RawMessage $message, Envelope $envelope = null): void
269
            {
270
                $this->lastMessage = $message;
271
            }
272
        };
273
    }
274
}
275