Passed
Push — master ( 0e1e27...d5cf0a )
by Valentin
05:11
created

JobTest::expectRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\SendIt;
13
14
use Mockery as m;
15
use PHPUnit\Framework\TestCase;
16
use Psr\Log\LoggerInterface;
17
use Spiral\Mailer\Message;
18
use Spiral\SendIt\Config\MailerConfig;
19
use Spiral\SendIt\MailJob;
20
use Spiral\SendIt\MailQueue;
21
use Spiral\SendIt\MessageSerializer;
22
use Spiral\SendIt\RendererInterface;
23
use Symfony\Component\Mailer\Exception\TransportException;
24
use Symfony\Component\Mailer\MailerInterface;
25
use Symfony\Component\Mime\Email;
26
27
class JobTest extends TestCase
28
{
29
    /** @var MailerInterface */
30
    protected $mailer;
31
    /** @var RendererInterface */
32
    protected $renderer;
33
    /** @var LoggerInterface */
34
    protected $logger;
35
36
    public function setUp(): void
37
    {
38
        parent::setUp();
39
40
        $this->mailer = m::mock(MailerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like Mockery::mock(Symfony\Co...MailerInterface::class) of type Mockery\LegacyMockInterface or Mockery\MockInterface is incompatible with the declared type Symfony\Component\Mailer\MailerInterface of property $mailer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
        $this->renderer = m::mock(RendererInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like Mockery::mock(Spiral\Sen...ndererInterface::class) of type Mockery\LegacyMockInterface or Mockery\MockInterface is incompatible with the declared type Spiral\SendIt\RendererInterface of property $renderer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $this->logger = m::mock(LoggerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like Mockery::mock(Psr\Log\LoggerInterface::class) of type Mockery\LegacyMockInterface or Mockery\MockInterface is incompatible with the declared type Psr\Log\LoggerInterface of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
    }
44
45
    public function testHandler(): void
46
    {
47
        $email = $this->getEmail();
48
49
        $this->expectRenderer($email);
50
51
        $this->mailer->expects('send')->with($email);
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Symfony\Component\Mailer\MailerInterface. ( Ignorable by Annotation )

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

51
        $this->mailer->/** @scrutinizer ignore-call */ 
52
                       expects('send')->with($email);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
53
        $this->logger->expects('debug')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Log\LoggerInterface. It seems like you code against a sub-type of Psr\Log\LoggerInterface such as Psr\Log\Test\TestLogger. ( Ignorable by Annotation )

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

53
        $this->logger->/** @scrutinizer ignore-call */ 
54
                       expects('debug')->with(
Loading history...
54
            'Sent `test` to "[email protected]"',
55
            ['emails' => ['[email protected]']]
56
        );
57
58
        $this->getHandler()->handle(
59
            MailQueue::JOB_NAME,
60
            'id',
61
            json_encode(MessageSerializer::pack($this->getMail()))
62
        );
63
    }
64
65
    public function testHandlerError(): void
66
    {
67
        $email = $this->getEmail();
68
69
        $this->expectRenderer($email);
70
71
        $this->mailer->expects('send')->with($email)->andThrow(new TransportException('failed'));
72
73
        $this->logger->expects('error')->with(
74
            'Failed to send `test` to "[email protected]": failed',
75
            ['emails' => ['[email protected]']]
76
        );
77
78
        try {
79
            $this->getHandler()->handle(
80
                MailQueue::JOB_NAME,
81
                'id',
82
                json_encode(MessageSerializer::pack($this->getMail()))
83
            );
84
        } catch (TransportException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
85
        }
86
87
        $this->logger->mockery_verify();
0 ignored issues
show
Bug introduced by
The method mockery_verify() does not exist on Psr\Log\LoggerInterface. It seems like you code against a sub-type of Psr\Log\LoggerInterface such as Psr\Log\Test\TestLogger. ( Ignorable by Annotation )

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

87
        $this->logger->/** @scrutinizer ignore-call */ 
88
                       mockery_verify();
Loading history...
88
    }
89
90
    private function getEmail(): Email
91
    {
92
        $email = new Email();
93
        $email->to('[email protected]');
94
        $email->html('message body');
95
        return $email;
96
    }
97
98
    private function expectRenderer(Email $email): void
99
    {
100
        $this->renderer->expects('render')->withArgs(
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Spiral\SendIt\RendererInterface. ( Ignorable by Annotation )

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

100
        $this->renderer->/** @scrutinizer ignore-call */ 
101
                         expects('render')->withArgs(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
            function (Message $message) {
102
                $this->assertSame($message->getSubject(), 'test');
103
                return true;
104
            }
105
        )->andReturn($email);
106
    }
107
108
    private function getHandler(): MailJob
109
    {
110
        $handler = new MailJob(
111
            new MailerConfig(['from' => '[email protected]']),
112
            $this->mailer,
113
            $this->renderer
114
        );
115
        $handler->setLogger($this->logger);
116
        return $handler;
117
    }
118
119
    private function getMail(): Message
120
    {
121
        $mail = new Message('test', ['[email protected]'], ['key' => 'value']);
122
        $mail->setFrom('[email protected]');
123
        $mail->setReplyTo('[email protected]');
124
        $mail->setCC('[email protected]');
125
        $mail->setBCC('[email protected]');
126
        return $mail;
127
    }
128
}
129