LogLoggerTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Illuminate\Tests\Log;
  3. use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
  4. use Illuminate\Events\Dispatcher;
  5. use Illuminate\Log\Events\MessageLogged;
  6. use Illuminate\Log\Logger;
  7. use Mockery as m;
  8. use Monolog\Logger as Monolog;
  9. use PHPUnit\Framework\TestCase;
  10. use RuntimeException;
  11. class LogLoggerTest extends TestCase
  12. {
  13. protected function tearDown(): void
  14. {
  15. m::close();
  16. }
  17. public function testMethodsPassErrorAdditionsToMonolog()
  18. {
  19. $writer = new Logger($monolog = m::mock(Monolog::class));
  20. $monolog->shouldReceive('error')->once()->with('foo', []);
  21. $writer->error('foo');
  22. }
  23. public function testContextIsAddedToAllSubsequentLogs()
  24. {
  25. $writer = new Logger($monolog = m::mock(Monolog::class));
  26. $writer->withContext(['bar' => 'baz']);
  27. $monolog->shouldReceive('error')->once()->with('foo', ['bar' => 'baz']);
  28. $writer->error('foo');
  29. }
  30. public function testContextIsFlushed()
  31. {
  32. $writer = new Logger($monolog = m::mock(Monolog::class));
  33. $writer->withContext(['bar' => 'baz']);
  34. $writer->withoutContext();
  35. $monolog->expects('error')->with('foo', []);
  36. $writer->error('foo');
  37. }
  38. public function testLoggerFiresEventsDispatcher()
  39. {
  40. $writer = new Logger($monolog = m::mock(Monolog::class), $events = new Dispatcher);
  41. $monolog->shouldReceive('error')->once()->with('foo', []);
  42. $events->listen(MessageLogged::class, function ($event) {
  43. $_SERVER['__log.level'] = $event->level;
  44. $_SERVER['__log.message'] = $event->message;
  45. $_SERVER['__log.context'] = $event->context;
  46. });
  47. $writer->error('foo');
  48. $this->assertTrue(isset($_SERVER['__log.level']));
  49. $this->assertSame('error', $_SERVER['__log.level']);
  50. unset($_SERVER['__log.level']);
  51. $this->assertTrue(isset($_SERVER['__log.message']));
  52. $this->assertSame('foo', $_SERVER['__log.message']);
  53. unset($_SERVER['__log.message']);
  54. $this->assertTrue(isset($_SERVER['__log.context']));
  55. $this->assertEquals([], $_SERVER['__log.context']);
  56. unset($_SERVER['__log.context']);
  57. }
  58. public function testListenShortcutFailsWithNoDispatcher()
  59. {
  60. $this->expectException(RuntimeException::class);
  61. $this->expectExceptionMessage('Events dispatcher has not been set.');
  62. $writer = new Logger(m::mock(Monolog::class));
  63. $writer->listen(function () {
  64. //
  65. });
  66. }
  67. public function testListenShortcut()
  68. {
  69. $writer = new Logger(m::mock(Monolog::class), $events = m::mock(DispatcherContract::class));
  70. $callback = function () {
  71. return 'success';
  72. };
  73. $events->shouldReceive('listen')->with(MessageLogged::class, $callback)->once();
  74. $writer->listen($callback);
  75. }
  76. }