1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Log\Handler; |
6
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use Ecodev\Felix\Api\Exception; |
9
|
|
|
use Ecodev\Felix\Api\ExceptionWithoutMailLogging; |
10
|
|
|
use Ecodev\Felix\Log\Handler\MailerHandler; |
11
|
|
|
use Ecodev\Felix\Log\Handler\MailerHandlerFactory; |
12
|
|
|
use Ecodev\Felix\Log\RecordCompleter; |
13
|
|
|
use Ecodev\Felix\Log\RecordCompleterFactory; |
14
|
|
|
use EcodevTests\Felix\Log\InMemoryTransport; |
15
|
|
|
use Laminas\ServiceManager\ServiceManager; |
16
|
|
|
use Monolog\Handler\Handler; |
17
|
|
|
use Monolog\Level; |
18
|
|
|
use Monolog\LogRecord; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
use Symfony\Component\Mailer\Transport\TransportInterface; |
21
|
|
|
use Throwable; |
22
|
|
|
|
23
|
|
|
class MailerHandlerFactoryTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
public function testReturnNullIfNoEmailsConfiguredAtAll(): void |
26
|
|
|
{ |
27
|
|
|
$container = new ServiceManager([ |
28
|
|
|
'services' => [ |
29
|
|
|
'config' => [], |
30
|
|
|
], |
31
|
|
|
]); |
32
|
|
|
|
33
|
|
|
$factory = new MailerHandlerFactory(); |
34
|
|
|
$actual = $factory($container, ''); |
35
|
|
|
self::assertNull($actual); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testReturnNullIfEmptyEmails(): void |
39
|
|
|
{ |
40
|
|
|
$container = new ServiceManager([ |
41
|
|
|
'services' => [ |
42
|
|
|
'config' => [ |
43
|
|
|
'log' => [ |
44
|
|
|
'emails' => [], |
45
|
|
|
], |
46
|
|
|
], |
47
|
|
|
], |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
$factory = new MailerHandlerFactory(); |
51
|
|
|
$actual = $factory($container, ''); |
52
|
|
|
self::assertNull($actual); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return array{0: Handler, 1: InMemoryTransport} |
57
|
|
|
*/ |
58
|
|
|
private function createHandler(): array |
59
|
|
|
{ |
60
|
|
|
$transport = new InMemoryTransport(); |
61
|
|
|
|
62
|
|
|
$container = new ServiceManager([ |
63
|
|
|
'services' => [ |
64
|
|
|
'config' => [ |
65
|
|
|
'hostname' => 'example.com', |
66
|
|
|
'email' => [ |
67
|
|
|
'from' => '[email protected]', |
68
|
|
|
], |
69
|
|
|
'log' => [ |
70
|
|
|
'emails' => ['[email protected]'], |
71
|
|
|
], |
72
|
|
|
], |
73
|
|
|
], |
74
|
|
|
'factories' => [ |
75
|
|
|
RecordCompleter::class => RecordCompleterFactory::class, |
76
|
|
|
TransportInterface::class => fn () => $transport, |
77
|
|
|
], |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
$factory = new MailerHandlerFactory(); |
81
|
|
|
$handler = $factory($container, ''); |
82
|
|
|
|
83
|
|
|
self::assertInstanceOf(MailerHandler::class, $handler); |
84
|
|
|
self::assertFalse(isset($transport->lastMessage), 'no message sent yet'); |
85
|
|
|
|
86
|
|
|
return [$handler, $transport]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testLogError(): void |
90
|
|
|
{ |
91
|
|
|
[$handler, $transport] = $this->createHandler(); |
92
|
|
|
|
93
|
|
|
$this->handleOne($handler, null); |
94
|
|
|
$recipients = $transport->lastMessage->getTo(); |
95
|
|
|
self::assertCount(1, $recipients, 'must be sent to 1 recipient'); |
96
|
|
|
self::assertSame('[email protected]', $recipients[0]->getAddress(), 'must be sent to developers'); |
97
|
|
|
self::assertStringContainsString('referer', $transport->lastMessage->getHtmlBody(), 'must contains the extra field REFERER'); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testLogException(): void |
101
|
|
|
{ |
102
|
|
|
[$handler, $transport] = $this->createHandler(); |
103
|
|
|
$this->handleOne($handler, new Exception('some exception')); |
104
|
|
|
self::assertStringContainsString('referer', $transport->lastMessage->getHtmlBody(), 'must contains the extra field REFERER'); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testDoesNotLogIgnoredException(): void |
108
|
|
|
{ |
109
|
|
|
[$handler, $transport] = $this->createHandler(); |
110
|
|
|
|
111
|
|
|
$this->handleOne($handler, new ExceptionWithoutMailLogging('some non logged exception')); |
112
|
|
|
self::assertFalse(isset($transport->lastMessage), 'still no message because the exception was marked as ignored'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function handleOne(Handler $handler, ?Throwable $exception): void |
116
|
|
|
{ |
117
|
|
|
$handler->handle( |
118
|
|
|
new LogRecord( |
119
|
|
|
new DateTimeImmutable(), |
120
|
|
|
'', |
121
|
|
|
Level::Error, |
122
|
|
|
'some message', |
123
|
|
|
['exception' => $exception] |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$handler->close(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|