]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Logger/StreamLoggerTest.php
Replace own VoidLogger with PSR-Standard NullLogger()
[friendica.git] / tests / src / Core / Logger / StreamLoggerTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Core\Logger;
23
24 use Friendica\Core\Logger\Exception\LoggerArgumentException;
25 use Friendica\Core\Logger\Exception\LoggerException;
26 use Friendica\Util\FileSystem;
27 use Friendica\Test\Util\VFSTrait;
28 use Friendica\Core\Logger\Type\StreamLogger;
29 use org\bovigo\vfs\vfsStream;
30 use org\bovigo\vfs\vfsStreamFile;
31 use Psr\Log\LogLevel;
32
33 class StreamLoggerTest extends AbstractLoggerTest
34 {
35         use VFSTrait;
36
37         /**
38          * @var vfsStreamFile
39          */
40         private $logfile;
41
42         /**
43          * @var Filesystem
44          */
45         private $fileSystem;
46
47         protected function setUp(): void
48         {
49                 parent::setUp();
50
51                 $this->setUpVfsDir();
52
53                 $this->fileSystem = new FileSystem();
54         }
55
56         /**
57          * {@@inheritdoc}
58          */
59         protected function getInstance($level = LogLevel::DEBUG)
60         {
61                 $this->logfile = vfsStream::newFile('friendica.log')
62                         ->at($this->root);
63
64                 $logger = new StreamLogger('test', $this->logfile->url(), $this->introspection, $this->fileSystem, $level);
65
66                 return $logger;
67         }
68
69         /**
70          * {@inheritdoc}
71          */
72         protected function getContent()
73         {
74                 return $this->logfile->getContent();
75         }
76
77         /**
78          * Test if a stream is working
79          */
80         public function testStream()
81         {
82                 $logfile = vfsStream::newFile('friendica.log')
83                         ->at($this->root);
84
85                 $filehandler = fopen($logfile->url(), 'ab');
86
87                 $logger = new \Friendica\Core\Logger\Type\StreamLogger('test', $filehandler, $this->introspection, $this->fileSystem);
88                 $logger->emergency('working');
89
90                 $text = $logfile->getContent();
91
92                 self::assertLogline($text);
93         }
94
95         /**
96          * Test if the close statement is working
97          */
98         public function testClose()
99         {
100                 $logfile = vfsStream::newFile('friendica.log')
101                         ->at($this->root);
102
103                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
104                 $logger->emergency('working');
105                 $logger->close();
106                 // close doesn't affect
107                 $logger->emergency('working too');
108
109                 $text = $logfile->getContent();
110
111                 self::assertLoglineNums(2, $text);
112         }
113
114         /**
115          * Test when a file isn't set
116          */
117         public function testNoUrl()
118         {
119                 $this->expectException(LoggerArgumentException::class);
120                 $this->expectExceptionMessage("Missing stream URL.");
121
122                 $logger = new StreamLogger('test', '', $this->introspection, $this->fileSystem);
123
124                 $logger->emergency('not working');
125         }
126
127         /**
128          * Test when a file cannot be opened
129          */
130         public function testWrongUrl()
131         {
132                 $this->expectException(LoggerException::class);
133                 $this->expectExceptionMessage("Cannot create stream.");
134
135                 $logfile = vfsStream::newFile('friendica.log')
136                         ->at($this->root)->chmod(0);
137
138                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
139
140                 $logger->emergency('not working');
141         }
142
143         /**
144          * Test when the directory cannot get created
145          */
146         public function testWrongDir()
147         {
148                 $this->expectException(\UnexpectedValueException::class);
149                 $this->expectExceptionMessageMatches("/Directory .* cannot get created: .* /");
150
151                 static::markTestIncomplete('We need a platform independent way to set directory to readonly');
152
153                 $logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection, $this->fileSystem);
154
155                 $logger->emergency('not working');
156         }
157
158         /**
159          * Test when the minimum level is not valid
160          */
161         public function testWrongMinimumLevel()
162         {
163                 $this->expectException(LoggerArgumentException::class);
164                 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
165
166                 $logger = new StreamLogger('test', 'file.text', $this->introspection, $this->fileSystem, 'NOPE');
167         }
168
169         /**
170          * Test when the minimum level is not valid
171          */
172         public function testWrongLogLevel()
173         {
174                 $this->expectException(LoggerArgumentException::class);
175                 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
176
177                 $logfile = vfsStream::newFile('friendica.log')
178                         ->at($this->root);
179
180                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
181
182                 $logger->log('NOPE', 'a test');
183         }
184
185         /**
186          * Test when the file is null
187          */
188         public function testWrongFile()
189         {
190                 $this->expectException(LoggerArgumentException::class);
191                 $this->expectExceptionMessage("A stream must either be a resource or a string.");
192
193                 $logger = new StreamLogger('test', null, $this->introspection, $this->fileSystem);
194         }
195
196         /**
197          * Test a relative path
198          * @doesNotPerformAssertions
199          */
200         public function testRealPath()
201         {
202                 static::markTestSkipped('vfsStream isn\'t compatible with chdir, so not testable.');
203
204                 $logfile = vfsStream::newFile('friendica.log')
205                                     ->at($this->root);
206
207                 chdir($this->root->getChild('logs')->url());
208
209                 $logger = new StreamLogger('test', '../friendica.log' , $this->introspection, $this->fileSystem);
210
211                 $logger->info('Test');
212         }
213 }