1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NotificationBundle\Tests\Consumer; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Sonata\NotificationBundle\Consumer\ConsumerEvent; |
19
|
|
|
use Sonata\NotificationBundle\Consumer\LoggerConsumer; |
20
|
|
|
use Sonata\NotificationBundle\Exception\InvalidParameterException; |
21
|
|
|
use Sonata\NotificationBundle\Tests\Entity\Message; |
22
|
|
|
|
23
|
|
|
class LoggerConsumerTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @dataProvider calledTypeProvider |
27
|
|
|
* |
28
|
|
|
* @param $type |
29
|
|
|
* @param $calledType |
30
|
|
|
*/ |
31
|
|
|
public function testProcess($type, $calledType): void |
32
|
|
|
{ |
33
|
|
|
$logger = $this->createMock(LoggerInterface::class); |
34
|
|
|
$logger->expects($this->once())->method($calledType); |
35
|
|
|
|
36
|
|
|
$message = new Message(); |
37
|
|
|
$message->setBody([ |
38
|
|
|
'level' => $type, |
39
|
|
|
'message' => 'Alert - Area 52 get compromised!!', |
40
|
|
|
]); |
41
|
|
|
|
42
|
|
|
$event = new ConsumerEvent($message); |
43
|
|
|
|
44
|
|
|
$consumer = new LoggerConsumer($logger); |
45
|
|
|
$consumer->process($event); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array[] |
50
|
|
|
*/ |
51
|
|
|
public function calledTypeProvider(): array |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
['emerg', 'emergency'], |
55
|
|
|
['alert', 'alert'], |
56
|
|
|
['crit', 'critical'], |
57
|
|
|
['err', 'error'], |
58
|
|
|
['warn', 'warning'], |
59
|
|
|
['notice', 'notice'], |
60
|
|
|
['info', 'info'], |
61
|
|
|
['debug', 'debug'], |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testInvalidType(): void |
66
|
|
|
{ |
67
|
|
|
$this->expectException(InvalidParameterException::class); |
68
|
|
|
|
69
|
|
|
$logger = $this->createMock(LoggerInterface::class); |
70
|
|
|
|
71
|
|
|
$message = new Message(); |
72
|
|
|
$message->setBody([ |
73
|
|
|
'level' => 'ERROR', |
74
|
|
|
'message' => 'Alert - Area 52 get compromised!!', |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
$event = new ConsumerEvent($message); |
78
|
|
|
|
79
|
|
|
$consumer = new LoggerConsumer($logger); |
80
|
|
|
$consumer->process($event); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|