TestLoggingUtil.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace tests\unit\Util;
  7. use AspectMock\Test as AspectMock;
  8. use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
  9. use Magento\FunctionalTestingFramework\Util\Logger\MftfLogger;
  10. use Monolog\Handler\TestHandler;
  11. use Monolog\Logger;
  12. use PHPUnit\Framework\Assert;
  13. class TestLoggingUtil extends Assert
  14. {
  15. /**
  16. * @var TestLoggingUtil
  17. */
  18. private static $instance;
  19. /**
  20. * @var TestHandler
  21. */
  22. private $testLogHandler;
  23. /**
  24. * TestLoggingUtil constructor.
  25. */
  26. private function __construct()
  27. {
  28. // private constructor
  29. }
  30. /**
  31. * Static singleton get function
  32. *
  33. * @return TestLoggingUtil
  34. */
  35. public static function getInstance()
  36. {
  37. if (self::$instance == null) {
  38. self::$instance = new TestLoggingUtil();
  39. }
  40. return self::$instance;
  41. }
  42. /**
  43. * Function which sets a mock instance of the logger for testing purposes.
  44. *
  45. * @return void
  46. */
  47. public function setMockLoggingUtil()
  48. {
  49. $this->testLogHandler = new TestHandler();
  50. $testLogger = new MftfLogger('testLogger');
  51. $testLogger->pushHandler($this->testLogHandler);
  52. $mockLoggingUtil = AspectMock::double(
  53. LoggingUtil::class,
  54. ['getLogger' => $testLogger]
  55. )->make();
  56. $property = new \ReflectionProperty(LoggingUtil::class, 'instance');
  57. $property->setAccessible(true);
  58. $property->setValue($mockLoggingUtil);
  59. }
  60. /**
  61. * Function which validates messages have been logged as intended during test execution.
  62. *
  63. * @param string $type
  64. * @param string $message
  65. * @param array $context
  66. * @return void
  67. */
  68. public function validateMockLogStatement($type, $message, $context)
  69. {
  70. $records = $this->testLogHandler->getRecords();
  71. $record = $records[count($records)-1]; // we assume the latest record is what requires validation
  72. $this->assertEquals(strtoupper($type), $record['level_name']);
  73. $this->assertEquals($message, $record['message']);
  74. $this->assertEquals($context, $record['context']);
  75. }
  76. public function validateMockLogStatmentRegex($type, $regex, $context)
  77. {
  78. $records = $this->testLogHandler->getRecords();
  79. $record = $records[count($records)-1]; // we assume the latest record is what requires validation
  80. $this->assertEquals(strtoupper($type), $record['level_name']);
  81. $this->assertRegExp($regex, $record['message']);
  82. $this->assertEquals($context, $record['context']);
  83. }
  84. /**
  85. * Function which clears the test logger context from the LogginUtil class. Should be run after a test class has
  86. * executed.
  87. *
  88. * @return void
  89. */
  90. public function clearMockLoggingUtil()
  91. {
  92. AspectMock::clean(LoggingUtil::class);
  93. }
  94. }