Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WebLoggerTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Setup\Model\WebLogger;
10 
11 class WebLoggerTest extends \PHPUnit\Framework\TestCase
12 {
16  private $directoryWriteMock;
17 
21  private $filesystemMock;
22 
26  private static $log;
27 
31  private $webLogger;
32 
33  public function setUp()
34  {
35  self::$log = '';
36 
37  $this->directoryWriteMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
38  $this->directoryWriteMock
39  ->expects($this->any())
40  ->method('readFile')
41  ->with('install.log')
42  ->will($this->returnCallback([\Magento\Setup\Test\Unit\Model\WebLoggerTest::class, 'readLog']));
43  $this->directoryWriteMock
44  ->expects($this->any())
45  ->method('writeFile')
46  ->with('install.log')
47  ->will($this->returnCallback([\Magento\Setup\Test\Unit\Model\WebLoggerTest::class, 'writeToLog']));
48  $this->directoryWriteMock
49  ->expects($this->any())
50  ->method('isExist')
51  ->will($this->returnCallback([\Magento\Setup\Test\Unit\Model\WebLoggerTest::class, 'isExist']));
52 
53  $this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
54  $this->filesystemMock
55  ->expects($this->once())
56  ->method('getDirectoryWrite')
57  ->will($this->returnValue($this->directoryWriteMock));
58 
59  $this->webLogger = new WebLogger($this->filesystemMock);
60  }
61 
63  {
64  $logFile = 'custom.log';
65  $directoryWriteMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
66  $directoryWriteMock->expects($this->once())->method('readFile')->with($logFile);
67  $directoryWriteMock->expects($this->once())->method('writeFile')->with($logFile);
68 
69  $filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
70  $filesystemMock
71  ->expects($this->once())
72  ->method('getDirectoryWrite')
73  ->will($this->returnValue($directoryWriteMock));
74 
75  $webLogger = new WebLogger($filesystemMock, $logFile);
76 
77  $webLogger->log('Message');
78  $webLogger->get();
79  }
80 
81  public function testLogSuccess()
82  {
83  $this->webLogger->logSuccess('Success1');
84  $this->assertEquals('<span class="text-success">[SUCCESS] ' . 'Success1' . '</span><br>', self::$log);
85 
86  $this->webLogger->logSuccess('Success2');
87  $this->assertEquals(
88  '<span class="text-success">[SUCCESS] ' . 'Success1' . '</span><br>' .
89  '<span class="text-success">[SUCCESS] ' . 'Success2' . '</span><br>',
90  self::$log
91  );
92  }
93 
94  public function testLogError()
95  {
96  $e1 = new \Exception('Dummy Exception1');
97  $e2 = new \Exception('Dummy Exception2');
98 
99  $this->webLogger->logError($e1);
100  $this->assertContains('[ERROR]', self::$log);
101  $this->assertContains('Exception', self::$log);
102  $this->assertContains($e1->getMessage(), self::$log);
103 
104  $this->webLogger->logError($e2);
105  $this->assertContains('[ERROR]', self::$log);
106  $this->assertContains('Exception', self::$log);
107  $this->assertContains($e1->getMessage(), self::$log);
108  $this->assertContains($e2->getMessage(), self::$log);
109  }
110 
111  public function testLog()
112  {
113  $this->webLogger->log('Message1');
114  $this->assertEquals('<span class="text-info">Message1</span><br>', self::$log);
115 
116  $this->webLogger->log('Message2');
117  $this->assertEquals(
118  '<span class="text-info">Message1</span><br><span class="text-info">Message2</span><br>',
119  self::$log
120  );
121  }
122 
123  public function testLogAfterInline()
124  {
125  $this->webLogger->logInline('*');
126  $this->webLogger->log('Message');
127  $this->assertEquals(
128  '<span class="text-info">*</span><br><span class="text-info">Message</span><br>',
129  self::$log
130  );
131  }
132 
133  public function testLogInline()
134  {
135  $this->webLogger->logInline('*');
136  $this->assertEquals('<span class="text-info">*</span>', self::$log);
137 
138  $this->webLogger->logInline('*');
139  $this->assertEquals('<span class="text-info">*</span><span class="text-info">*</span>', self::$log);
140  }
141 
142  public function testLogMeta()
143  {
144  $this->webLogger->logMeta('Meta1');
145  $this->assertEquals('<span class="hidden">Meta1</span><br>', self::$log);
146 
147  $this->webLogger->logMeta('Meta2');
148  $this->assertEquals('<span class="hidden">Meta1</span><br><span class="hidden">Meta2</span><br>', self::$log);
149  }
150 
151  public function testGet()
152  {
153  $this->webLogger->log('Message1' . PHP_EOL);
154  $this->webLogger->log('Message2');
155 
156  $expected = [
157  '<span class="text-info">Message1',
158  '</span><br><span class="text-info">Message2</span><br>',
159  ];
160 
161  $this->assertEquals($expected, $this->webLogger->get());
162  }
163 
164  public function testClear()
165  {
166  $this->directoryWriteMock
167  ->expects($this->once())
168  ->method('delete')
169  ->will($this->returnCallback([\Magento\Setup\Test\Unit\Model\WebLoggerTest::class, 'deleteLog']));
170 
171  $this->webLogger->log('Message1');
172  $this->assertEquals('<span class="text-info">Message1</span><br>', self::$log);
173 
174  $this->webLogger->clear();
175  $this->assertEquals('', self::$log);
176  }
177 
178  public function testClearNotExist()
179  {
180  $this->directoryWriteMock
181  ->expects($this->never())
182  ->method('delete');
183 
184  $this->webLogger->clear();
185  }
186 
190  public static function readLog()
191  {
192  return self::$log;
193  }
194 
198  public static function writeToLog($logFile, $message)
199  {
200  self::$log .= $message;
201  }
202 
203  public static function deleteLog()
204  {
205  self::$log = '';
206  }
207 
211  public static function isExist()
212  {
213  return self::$log != '';
214  }
215 }
$message