Failed Conditions
Push — master ( 398dce...01f535 )
by Adrien
02:21
created

MailerTest.php$2 ➔ createMockMessage()   A

Complexity

Conditions 1

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 53
rs 9.0254

10 Methods

Rating   Name   Duplication   Size   Complexity  
A MailerTest.php$2 ➔ getEmail() 0 3 1
A MailerTest.php$2 ➔ getId() 0 3 1
A MailerTest.php$2 ➔ setDateSent() 0 3 1
A MailerTest.php$2 ➔ getSubject() 0 3 1
A MailerTest.php$2 ➔ getDateSent() 0 3 1
A MailerTest.php$2 ➔ setSubject() 0 2 1
A MailerTest.php$2 ➔ getRecipient() 0 3 1
A MailerTest.php$2 ➔ setBody() 0 2 1
A MailerTest.php$2 ➔ setEmail() 0 2 1
A MailerTest.php$2 ➔ getBody() 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Service;
6
7
use Cake\Chronos\Chronos;
8
use Doctrine\ORM\EntityManager;
9
use Ecodev\Felix\Model\User;
10
use Ecodev\Felix\Repository\MessageRepository;
11
use Ecodev\Felix\Service\Mailer;
12
use Laminas\Mail;
13
use Laminas\Mail\Address;
14
use Laminas\Mail\Transport\TransportInterface;
15
16
class MailerTest extends \PHPUnit\Framework\TestCase
17
{
18
    private function createMockMailer(): Mailer
19
    {
20
        /** @var EntityManager $entityManager */
21
        $entityManager = $this->createMock(EntityManager::class);
22
        $transport = $this->createMockTransport();
23
24
        $messageRepository = new class() implements MessageRepository {
25
            public function getAllMessageToSend(): array
26
            {
27
                return [];
28
            }
29
        };
30
31
        $mailer = new Mailer(
32
            $entityManager,
33
            $messageRepository,
34
            $transport,
35
            null,
36
            '[email protected]',
37
            '/user/bin/php',
38
            'Epicerio'
39
        );
40
41
        return $mailer;
42
    }
43
44
    private function createMockTransport(): TransportInterface
45
    {
46
        return new class() implements TransportInterface {
47
            public function send(Mail\Message $message): void
48
            {
49
                // Purposefully place current cursor at the end of list
50
                foreach ($message->getFrom() as $a) {
51
                    $a->getEmail();
52
                }
53
54
                foreach ($message->getTo() as $a) {
55
                    $a->getEmail();
56
                }
57
            }
58
        };
59
    }
60
61
    public function testMockTransportHasCursorAtEndOfList(): void
62
    {
63
        $message = new Mail\Message();
64
        $message->setFrom('[email protected]');
65
        $message->setTo('[email protected]');
66
67
        // New message has current cursor on first element
68
        self::assertInstanceOf(Address::class, $message->getFrom()->current());
69
        self::assertInstanceOf(Address::class, $message->getTo()->current());
70
71
        $transport = $this->createMockTransport();
72
        $transport->send($message);
73
74
        // After transport, message has current cursor on end of list
75
        self::assertFalse($message->getFrom()->current());
76
        self::assertFalse($message->getTo()->current());
77
    }
78
79
    public function testSendMessage(): void
80
    {
81
        $mailer = $this->createMockMailer();
82
        $message = $this->createMockMessage();
83
84
        $this->expectOutputRegex('~email from noreply@example\.com sent to: john\.doe@example\.com~');
85
        $mailer->sendMessage($message);
86
        self::assertNotNull($message->getDateSent());
87
    }
88
89
    private function createMockMessage(): \Ecodev\Felix\Model\Message
90
    {
91
        return new class() implements \Ecodev\Felix\Model\Message {
92
            /**
93
             * @var null|Chronos
94
             */
95
            private $dateSent;
96
97
            public function getSubject(): string
98
            {
99
                return 'my subject';
100
            }
101
102
            public function getBody(): string
103
            {
104
                return 'my body';
105
            }
106
107
            public function setDateSent(?Chronos $dateSent): void
108
            {
109
                $this->dateSent = $dateSent;
110
            }
111
112
            public function getEmail(): string
113
            {
114
                return '[email protected]';
115
            }
116
117
            public function getRecipient(): ?User
118
            {
119
                return null;
120
            }
121
122
            public function getId(): ?int
123
            {
124
                return null;
125
            }
126
127
            public function setSubject(string $subject): void
128
            {
129
            }
130
131
            public function setBody(string $body): void
132
            {
133
            }
134
135
            public function getDateSent(): ?Chronos
136
            {
137
                return $this->dateSent;
138
            }
139
140
            public function setEmail(string $email): void
141
            {
142
                // TODO: Implement setEmail() method.
143
            }
144
        };
145
    }
146
}
147