Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TestLoggingUtil.php
Go to the documentation of this file.
1 <?php
7 namespace tests\unit\Util;
8 
9 use AspectMock\Test as AspectMock;
12 use Monolog\Handler\TestHandler;
13 use Monolog\Logger;
14 use PHPUnit\Framework\Assert;
15 
16 class TestLoggingUtil extends Assert
17 {
21  private static $INSTANCE;
22 
26  private $testLogHandler;
27 
31  private function __construct()
32  {
33  // private constructor
34  }
35 
41  public static function getInstance()
42  {
43  if (self::$INSTANCE == null) {
44  self::$INSTANCE = new TestLoggingUtil();
45  }
46 
47  return self::$INSTANCE;
48  }
49 
55  public function setMockLoggingUtil()
56  {
57  $this->testLogHandler = new TestHandler();
58  $testLogger = new MftfLogger('testLogger');
59  $testLogger->pushHandler($this->testLogHandler);
60  $mockLoggingUtil = AspectMock::double(
61  LoggingUtil::class,
62  ['getLogger' => $testLogger]
63  )->make();
64  $property = new \ReflectionProperty(LoggingUtil::class, 'INSTANCE');
65  $property->setAccessible(true);
66  $property->setValue($mockLoggingUtil);
67  }
68 
77  public function validateMockLogStatement($type, $message, $context)
78  {
79  $records = $this->testLogHandler->getRecords();
80  $record = $records[count($records)-1]; // we assume the latest record is what requires validation
81  $this->assertEquals(strtoupper($type), $record['level_name']);
82  $this->assertEquals($message, $record['message']);
83  $this->assertEquals($context, $record['context']);
84  }
85 
86  public function validateMockLogStatmentRegex($type, $regex, $context)
87  {
88  $records = $this->testLogHandler->getRecords();
89  $record = $records[count($records)-1]; // we assume the latest record is what requires validation
90  $this->assertEquals(strtoupper($type), $record['level_name']);
91  $this->assertRegExp($regex, $record['message']);
92  $this->assertEquals($context, $record['context']);
93  }
94 
101  public function clearMockLoggingUtil()
102  {
103  AspectMock::clean(LoggingUtil::class);
104  }
105 }
validateMockLogStatmentRegex($type, $regex, $context)
$message
$type
Definition: item.phtml:13
validateMockLogStatement($type, $message, $context)