Passed
Push — master ( 18c542...46352d )
by Kevin
02:34
created

anonymous//tests/Functional/EmailTest.php$2   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
c 0
b 0
f 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Tests\Functional;
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\RawMessage;
9
use Zenstruck\ScheduleBundle\Schedule;
10
use Zenstruck\ScheduleBundle\Schedule\Extension\Handler\EmailHandler;
11
use Zenstruck\ScheduleBundle\Schedule\ScheduleBuilder;
12
use Zenstruck\ScheduleBundle\Tests\Fixture\MockScheduleBuilder;
13
use Zenstruck\ScheduleBundle\Tests\Fixture\MockTask;
14
15
/**
16
 * @author Kevin Bond <[email protected]>
17
 */
18
final class EmailTest extends TestCase
19
{
20
    /**
21
     * @test
22
     */
23
    public function sends_schedule_failure_email()
24
    {
25
        $mailer = $this->createMailer();
26
27
        (new MockScheduleBuilder())
28
            ->addHandler(new EmailHandler($mailer, '[email protected]', '[email protected]'))
29
            ->addBuilder(new class() implements ScheduleBuilder {
30
                public function buildSchedule(Schedule $schedule): void
31
                {
32
                    $schedule->emailOnFailure();
33
                }
34
            })
35
            ->addTask(MockTask::exception(new \Exception('task 1 exception message'), 'my task 1'))
36
            ->addTask(MockTask::success('my task 2'))
37
            ->addTask(MockTask::exception(new \Exception('task 3 exception message'), 'my task 3'))
38
            ->run()
39
        ;
40
41
        $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

41
        $this->assertSame('[email protected]', $mailer->lastMessage->/** @scrutinizer ignore-call */ getFrom()[0]->getAddress());
Loading history...
42
        $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

42
        $this->assertSame('[email protected]', $mailer->lastMessage->/** @scrutinizer ignore-call */ getTo()[0]->getAddress());
Loading history...
43
        $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

43
        $this->assertSame('[Scheduled Failed] 2 tasks failed', $mailer->lastMessage->/** @scrutinizer ignore-call */ getSubject());
Loading history...
44
        $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

44
        $this->assertStringContainsString('2 tasks failed', $mailer->lastMessage->/** @scrutinizer ignore-call */ getTextBody());
Loading history...
45
        $this->assertStringContainsString('# (Failure 1/2) MockTask: my task 1', $mailer->lastMessage->getTextBody());
46
        $this->assertStringContainsString('## Exception', $mailer->lastMessage->getTextBody());
47
        $this->assertStringContainsString('Exception: task 1 exception message', $mailer->lastMessage->getTextBody());
48
        $this->assertStringContainsString('# (Failure 2/2) MockTask: my task 3', $mailer->lastMessage->getTextBody());
49
        $this->assertStringContainsString('## Exception', $mailer->lastMessage->getTextBody());
50
        $this->assertStringContainsString('Exception: task 3 exception message', $mailer->lastMessage->getTextBody());
51
    }
52
53
    /**
54
     * @test
55
     */
56
    public function sends_task_failure_email()
57
    {
58
        $mailer = $this->createMailer();
59
60
        (new MockScheduleBuilder())
61
            ->addHandler(new EmailHandler($mailer, '[email protected]', '[email protected]'))
62
            ->addTask(MockTask::failure('Exit 127: Command not found', 'my task', 'sh: 1: sdsdsd: not found')
63
                ->emailOnFailure()
64
            )
65
            ->run()
66
        ;
67
68
        $this->assertSame('[email protected]', $mailer->lastMessage->getFrom()[0]->getAddress());
69
        $this->assertSame('[email protected]', $mailer->lastMessage->getTo()[0]->getAddress());
70
        $this->assertSame('[Scheduled Task Failed] MockTask: my task', $mailer->lastMessage->getSubject());
71
        $this->assertStringContainsString('Exit 127: Command not found', $mailer->lastMessage->getTextBody());
72
        $this->assertStringContainsString('## Task Output:', $mailer->lastMessage->getTextBody());
73
        $this->assertStringContainsString('sh: 1: sdsdsd: not found', $mailer->lastMessage->getTextBody());
74
    }
75
76
    /**
77
     * @test
78
     */
79
    public function sends_after_task_email()
80
    {
81
        $mailer = $this->createMailer();
82
83
        (new MockScheduleBuilder())
84
            ->addHandler(new EmailHandler($mailer, '[email protected]', '[email protected]'))
85
            ->addTask(MockTask::success('my task', 'my task output')->emailAfter())
86
            ->run()
87
        ;
88
89
        $this->assertSame('[email protected]', $mailer->lastMessage->getFrom()[0]->getAddress());
90
        $this->assertSame('[email protected]', $mailer->lastMessage->getTo()[0]->getAddress());
91
        $this->assertSame('[Scheduled Task Succeeded] MockTask: my task', $mailer->lastMessage->getSubject());
92
        $this->assertStringContainsString('Successful', $mailer->lastMessage->getTextBody());
93
        $this->assertStringContainsString('## Task Output:', $mailer->lastMessage->getTextBody());
94
        $this->assertStringContainsString('my task output', $mailer->lastMessage->getTextBody());
95
    }
96
97
    /**
98
     * @test
99
     */
100
    public function provides_helpful_message_if_handler_not_configured()
101
    {
102
        $this->expectException(\LogicException::class);
103
        $this->expectExceptionMessage('To use the email extension you must configure a mailer (config path: "zenstruck_schedule.email_handler").');
104
105
        (new MockScheduleBuilder())
106
            ->addBuilder(new class() implements ScheduleBuilder {
107
                public function buildSchedule(Schedule $schedule): void
108
                {
109
                    $schedule->emailOnFailure();
110
                }
111
            })
112
            ->run()
113
        ;
114
    }
115
116
    /**
117
     * @test
118
     */
119
    public function to_address_must_be_configured()
120
    {
121
        $event = (new MockScheduleBuilder())
122
            ->addHandler(new EmailHandler($this->createMailer()))
123
            ->addTask(MockTask::failure()->emailOnFailure())
124
            ->run()
125
        ;
126
127
        $this->assertInstanceOf(\LogicException::class, $event->getResults()[0]->getException());
128
        $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());
129
    }
130
131
    private function createMailer(): MailerInterface
132
    {
133
        return new class() implements MailerInterface {
134
            /** @var RawMessage */
135
            public $lastMessage;
136
137
            public function send(RawMessage $message, Envelope $envelope = null): void
138
            {
139
                $this->lastMessage = $message;
140
            }
141
        };
142
    }
143
}
144