]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Logger/StreamLoggerTest.php
Merge pull request #11334 from annando/guid-style
[friendica.git] / tests / src / Core / Logger / StreamLoggerTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Core\Logger\Exception\LogLevelException;
27 use Friendica\Util\FileSystem;
28 use Friendica\Test\Util\VFSTrait;
29 use Friendica\Core\Logger\Type\StreamLogger;
30 use org\bovigo\vfs\vfsStream;
31 use org\bovigo\vfs\vfsStreamFile;
32 use Psr\Log\LogLevel;
33
34 class StreamLoggerTest extends AbstractLoggerTest
35 {
36         use VFSTrait;
37
38         /**
39          * @var vfsStreamFile
40          */
41         private $logfile;
42
43         /**
44          * @var Filesystem
45          */
46         private $fileSystem;
47
48         protected function setUp(): void
49         {
50                 parent::setUp();
51
52                 $this->setUpVfsDir();
53
54                 $this->fileSystem = new FileSystem();
55         }
56
57         /**
58          * {@@inheritdoc}
59          */
60         protected function getInstance($level = LogLevel::DEBUG)
61         {
62                 $this->logfile = vfsStream::newFile('friendica.log')
63                         ->at($this->root);
64
65                 $logger = new StreamLogger('test', $this->logfile->url(), $this->introspection, $this->fileSystem, $level);
66
67                 return $logger;
68         }
69
70         /**
71          * {@inheritdoc}
72          */
73         protected function getContent()
74         {
75                 return $this->logfile->getContent();
76         }
77
78         /**
79          * Test if a stream is working
80          */
81         public function testStream()
82         {
83                 $logfile = vfsStream::newFile('friendica.log')
84                         ->at($this->root);
85
86                 $filehandler = fopen($logfile->url(), 'ab');
87
88                 $logger = new \Friendica\Core\Logger\Type\StreamLogger('test', $filehandler, $this->introspection, $this->fileSystem);
89                 $logger->emergency('working');
90
91                 $text = $logfile->getContent();
92
93                 self::assertLogline($text);
94         }
95
96         /**
97          * Test if the close statement is working
98          */
99         public function testClose()
100         {
101                 $logfile = vfsStream::newFile('friendica.log')
102                         ->at($this->root);
103
104                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
105                 $logger->emergency('working');
106                 $logger->close();
107                 // close doesn't affect
108                 $logger->emergency('working too');
109
110                 $text = $logfile->getContent();
111
112                 self::assertLoglineNums(2, $text);
113         }
114
115         /**
116          * Test when a file isn't set
117          */
118         public function testNoUrl()
119         {
120                 $this->expectException(LoggerArgumentException::class);
121                 $this->expectExceptionMessage("Missing stream URL.");
122
123                 $logger = new StreamLogger('test', '', $this->introspection, $this->fileSystem);
124
125                 $logger->emergency('not working');
126         }
127
128         /**
129          * Test when a file cannot be opened
130          */
131         public function testWrongUrl()
132         {
133                 $this->expectException(LoggerException::class);
134                 $this->expectExceptionMessage("Cannot create stream.");
135
136                 $logfile = vfsStream::newFile('friendica.log')
137                         ->at($this->root)->chmod(0);
138
139                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
140
141                 $logger->emergency('not working');
142         }
143
144         /**
145          * Test when the directory cannot get created
146          */
147         public function testWrongDir()
148         {
149                 $this->expectException(\UnexpectedValueException::class);
150                 $this->expectExceptionMessageMatches("/Directory .* cannot get created: .* /");
151
152                 static::markTestIncomplete('We need a platform independent way to set directory to readonly');
153
154                 $logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection, $this->fileSystem);
155
156                 $logger->emergency('not working');
157         }
158
159         /**
160          * Test when the minimum level is not valid
161          */
162         public function testWrongMinimumLevel()
163         {
164                 $this->expectException(LogLevelException::class);
165                 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
166
167                 $logger = new StreamLogger('test', 'file.text', $this->introspection, $this->fileSystem, 'NOPE');
168         }
169
170         /**
171          * Test when the minimum level is not valid
172          */
173         public function testWrongLogLevel()
174         {
175                 $this->expectException(LogLevelException::class);
176                 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
177
178                 $logfile = vfsStream::newFile('friendica.log')
179                         ->at($this->root);
180
181                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
182
183                 $logger->log('NOPE', 'a test');
184         }
185
186         /**
187          * Test when the file is null
188          */
189         public function testWrongFile()
190         {
191                 $this->expectException(LoggerArgumentException::class);
192                 $this->expectExceptionMessage("A stream must either be a resource or a string.");
193
194                 $logger = new StreamLogger('test', null, $this->introspection, $this->fileSystem);
195         }
196
197         /**
198          * Test a relative path
199          * @doesNotPerformAssertions
200          */
201         public function testRealPath()
202         {
203                 static::markTestSkipped('vfsStream isn\'t compatible with chdir, so not testable.');
204
205                 $logfile = vfsStream::newFile('friendica.log')
206                                     ->at($this->root);
207
208                 chdir($this->root->getChild('logs')->url());
209
210                 $logger = new StreamLogger('test', '../friendica.log' , $this->introspection, $this->fileSystem);
211
212                 $logger->info('Test');
213         }
214 }