SystemTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Logger\Test\Unit\Handler;
  7. use Magento\Framework\Filesystem\DriverInterface;
  8. use Magento\Framework\Logger\Handler\Exception;
  9. use Magento\Framework\Logger\Handler\System;
  10. use Monolog\Logger;
  11. use PHPUnit_Framework_MockObject_MockObject as Mock;
  12. class SystemTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var System
  16. */
  17. private $model;
  18. /**
  19. * @var DriverInterface|Mock
  20. */
  21. private $filesystemMock;
  22. /**
  23. * @var Exception|Mock
  24. */
  25. private $exceptionHandlerMock;
  26. /**
  27. * @inheritdoc
  28. */
  29. protected function setUp()
  30. {
  31. $this->filesystemMock = $this->getMockBuilder(DriverInterface::class)
  32. ->getMockForAbstractClass();
  33. $this->exceptionHandlerMock = $this->getMockBuilder(Exception::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->model = new System(
  37. $this->filesystemMock,
  38. $this->exceptionHandlerMock
  39. );
  40. }
  41. public function testWrite()
  42. {
  43. $this->filesystemMock->expects($this->once())
  44. ->method('getParentDirectory');
  45. $this->filesystemMock->expects($this->once())
  46. ->method('isDirectory')
  47. ->willReturn('true');
  48. $this->model->write($this->getRecord());
  49. }
  50. public function testWriteException()
  51. {
  52. $record = $this->getRecord();
  53. $record['context']['exception'] = new \Exception('Some exception');
  54. $this->exceptionHandlerMock->expects($this->once())
  55. ->method('handle')
  56. ->with($record);
  57. $this->filesystemMock->expects($this->never())
  58. ->method('getParentDirectory');
  59. $this->model->write($record);
  60. }
  61. /**
  62. * @param int $level
  63. * @param string $message
  64. * @param array $context
  65. * @return array
  66. */
  67. private function getRecord($level = Logger::WARNING, $message = 'test', $context = [])
  68. {
  69. return [
  70. 'message' => $message,
  71. 'context' => $context,
  72. 'level' => $level,
  73. 'level_name' => Logger::getLevelName($level),
  74. 'channel' => 'test',
  75. 'datetime' => \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true))),
  76. 'extra' => [],
  77. ];
  78. }
  79. }