|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Log\Writer; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
|
8
|
|
|
use Ecodev\Felix\Api\Exception; |
|
9
|
|
|
use Ecodev\Felix\Api\ExceptionWithoutMailLogging; |
|
10
|
|
|
use Ecodev\Felix\Log\EventCompleter; |
|
11
|
|
|
use Ecodev\Felix\Log\EventCompleterFactory; |
|
12
|
|
|
use Ecodev\Felix\Log\Formatter\Extras; |
|
13
|
|
|
use Ecodev\Felix\Log\Writer\MailFactory; |
|
14
|
|
|
use Laminas\Log\Logger; |
|
15
|
|
|
use Laminas\Log\Writer\Mail; |
|
16
|
|
|
use Laminas\Log\Writer\WriterInterface; |
|
17
|
|
|
use Laminas\Mail\Transport\InMemory; |
|
18
|
|
|
use Laminas\Mail\Transport\TransportInterface; |
|
19
|
|
|
use Laminas\ServiceManager\ServiceManager; |
|
20
|
|
|
use PHPUnit\Framework\TestCase; |
|
21
|
|
|
use Throwable; |
|
22
|
|
|
|
|
23
|
|
|
class MailFactoryTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
public function testReturnNullIfNoEmailsConfiguredAtAll(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$container = new ServiceManager([ |
|
28
|
|
|
'services' => [ |
|
29
|
|
|
'config' => [], |
|
30
|
|
|
], |
|
31
|
|
|
]); |
|
32
|
|
|
|
|
33
|
|
|
$factory = new MailFactory(); |
|
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 MailFactory(); |
|
51
|
|
|
$actual = $factory($container, ''); |
|
52
|
|
|
self::assertNull($actual); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function createWriter(): array |
|
56
|
|
|
{ |
|
57
|
|
|
$transport = new InMemory(); |
|
58
|
|
|
$container = new ServiceManager([ |
|
59
|
|
|
'services' => [ |
|
60
|
|
|
'config' => [ |
|
61
|
|
|
'hostname' => 'example.com', |
|
62
|
|
|
'email' => [ |
|
63
|
|
|
'from' => ['[email protected]'], |
|
64
|
|
|
], |
|
65
|
|
|
'log' => [ |
|
66
|
|
|
'emails' => ['[email protected]'], |
|
67
|
|
|
], |
|
68
|
|
|
], |
|
69
|
|
|
], |
|
70
|
|
|
'invokables' => [ |
|
71
|
|
|
Extras::class, |
|
72
|
|
|
], |
|
73
|
|
|
'factories' => [ |
|
74
|
|
|
EventCompleter::class => EventCompleterFactory::class, |
|
75
|
|
|
TransportInterface::class => function () use ($transport) { |
|
76
|
|
|
return $transport; |
|
77
|
|
|
}, |
|
78
|
|
|
], |
|
79
|
|
|
]); |
|
80
|
|
|
|
|
81
|
|
|
$factory = new MailFactory(); |
|
82
|
|
|
$writer = $factory($container, ''); |
|
83
|
|
|
|
|
84
|
|
|
self::assertInstanceOf(Mail::class, $writer); |
|
85
|
|
|
self::assertNull($transport->getLastMessage(), 'no message sent yet'); |
|
86
|
|
|
|
|
87
|
|
|
return [$writer, $transport]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testLogError(): void |
|
91
|
|
|
{ |
|
92
|
|
|
[$writer, $transport] = $this->createWriter(); |
|
93
|
|
|
|
|
94
|
|
|
$this->writeOne($writer, null); |
|
95
|
|
|
self::assertTrue($transport->getLastMessage()->getTo()->has('[email protected]'), 'must be sent to developers'); |
|
96
|
|
|
self::assertStringContainsString('REFERER', $transport->getLastMessage()->getBody(), 'must contains the extra field REFERER'); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function testLogException(): void |
|
100
|
|
|
{ |
|
101
|
|
|
[$writer, $transport] = $this->createWriter(); |
|
102
|
|
|
$this->writeOne($writer, new Exception('some exception')); |
|
103
|
|
|
self::assertStringContainsString('REFERER', $transport->getLastMessage()->getBody(), 'must contains the extra field REFERER'); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testDoesNotLogIgnoredException(): void |
|
107
|
|
|
{ |
|
108
|
|
|
[$writer, $transport] = $this->createWriter(); |
|
109
|
|
|
|
|
110
|
|
|
$this->writeOne($writer, new ExceptionWithoutMailLogging('some non logged exception')); |
|
111
|
|
|
self::assertNull($transport->getLastMessage(), 'still no message because the exception was marked as ignored'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function writeOne(WriterInterface $writer, ?Throwable $exception): void |
|
115
|
|
|
{ |
|
116
|
|
|
$writer->write([ |
|
117
|
|
|
'timestamp' => new DateTimeImmutable(), |
|
118
|
|
|
'priority' => Logger::ERR, |
|
119
|
|
|
'priorityName' => 'some priority name', |
|
120
|
|
|
'message' => 'some message', |
|
121
|
|
|
'extra' => ['exception' => $exception], |
|
122
|
|
|
]); |
|
123
|
|
|
|
|
124
|
|
|
$writer->shutdown(); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|