forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogLoggerTest.php
executable file
·95 lines (76 loc) · 2.87 KB
/
LogLoggerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Illuminate\Tests\Log;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Events\Dispatcher;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Log\Logger;
use Mockery as m;
use Monolog\Logger as Monolog;
use PHPUnit\Framework\TestCase;
use RuntimeException;
class LogLoggerTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
public function testMethodsPassErrorAdditionsToMonolog()
{
$writer = new Logger($monolog = m::mock(Monolog::class));
$monolog->shouldReceive('error')->once()->with('foo', []);
$writer->error('foo');
}
public function testContextIsAddedToAllSubsequentLogs()
{
$writer = new Logger($monolog = m::mock(Monolog::class));
$writer->withContext(['bar' => 'baz']);
$monolog->shouldReceive('error')->once()->with('foo', ['bar' => 'baz']);
$writer->error('foo');
}
public function testContextIsFlushed()
{
$writer = new Logger($monolog = m::mock(Monolog::class));
$writer->withContext(['bar' => 'baz']);
$writer->withoutContext();
$monolog->expects('error')->with('foo', []);
$writer->error('foo');
}
public function testLoggerFiresEventsDispatcher()
{
$writer = new Logger($monolog = m::mock(Monolog::class), $events = new Dispatcher);
$monolog->shouldReceive('error')->once()->with('foo', []);
$events->listen(MessageLogged::class, function ($event) {
$_SERVER['__log.level'] = $event->level;
$_SERVER['__log.message'] = $event->message;
$_SERVER['__log.context'] = $event->context;
});
$writer->error('foo');
$this->assertTrue(isset($_SERVER['__log.level']));
$this->assertSame('error', $_SERVER['__log.level']);
unset($_SERVER['__log.level']);
$this->assertTrue(isset($_SERVER['__log.message']));
$this->assertSame('foo', $_SERVER['__log.message']);
unset($_SERVER['__log.message']);
$this->assertTrue(isset($_SERVER['__log.context']));
$this->assertEquals([], $_SERVER['__log.context']);
unset($_SERVER['__log.context']);
}
public function testListenShortcutFailsWithNoDispatcher()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Events dispatcher has not been set.');
$writer = new Logger(m::mock(Monolog::class));
$writer->listen(function () {
//
});
}
public function testListenShortcut()
{
$writer = new Logger(m::mock(Monolog::class), $events = m::mock(DispatcherContract::class));
$callback = function () {
return 'success';
};
$events->shouldReceive('listen')->with(MessageLogged::class, $callback)->once();
$writer->listen($callback);
}
}