]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Logger/StreamLoggerTest.php
spelling: forums
[friendica.git] / tests / src / Core / Logger / StreamLoggerTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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                 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($this->logfile->url())->once();
66
67                 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem, $level);
68
69                 return $logger;
70         }
71
72         /**
73          * {@inheritdoc}
74          */
75         protected function getContent()
76         {
77                 return $this->logfile->getContent();
78         }
79
80         /**
81          * Test when a file isn't set
82          */
83         public function testNoUrl()
84         {
85                 $this->expectException(LoggerArgumentException::class);
86                 $this->expectExceptionMessage("Missing stream URL.");
87
88                 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('')->once();
89
90                 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
91
92                 $logger->emergency('not working');
93         }
94
95         /**
96          * Test when a file cannot be opened
97          */
98         public function testWrongUrl()
99         {
100                 $this->expectException(LoggerArgumentException::class);
101
102                 $logfile = vfsStream::newFile('friendica.log')
103                         ->at($this->root)->chmod(0);
104
105                 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($logfile->url())->once();
106
107                 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
108
109                 $logger->emergency('not working');
110         }
111
112         /**
113          * Test when the directory cannot get created
114          */
115         public function testWrongDir()
116         {
117                 $this->expectException(\UnexpectedValueException::class);
118                 $this->expectExceptionMessageMatches("/Directory .* cannot get created: .* /");
119
120                 static::markTestIncomplete('We need a platform independent way to set directory to readonly');
121
122                 $logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection, $this->fileSystem);
123
124                 $logger->emergency('not working');
125         }
126
127         /**
128          * Test when the minimum level is not valid
129          */
130         public function testWrongMinimumLevel()
131         {
132                 $this->expectException(LogLevelException::class);
133                 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
134
135                 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('file.text')->once();
136
137                 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem, 'NOPE');
138         }
139
140         /**
141          * Test when the minimum level is not valid
142          */
143         public function testWrongLogLevel()
144         {
145                 $this->expectException(LogLevelException::class);
146                 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
147
148                 $logfile = vfsStream::newFile('friendica.log')
149                         ->at($this->root);
150
151                 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($logfile->url())->once();
152
153                 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
154
155                 $logger->log('NOPE', 'a test');
156         }
157
158         /**
159          * Test when the file is null
160          */
161         public function testWrongFile()
162         {
163                 $this->expectException(LoggerArgumentException::class);
164                 $this->expectExceptionMessage("A stream must either be a resource or a string.");
165
166                 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn(null)->once();
167
168                 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
169         }
170
171         /**
172          * Test a relative path
173          * @doesNotPerformAssertions
174          */
175         public function testRealPath()
176         {
177                 static::markTestSkipped('vfsStream isn\'t compatible with chdir, so not testable.');
178
179                 $logfile = vfsStream::newFile('friendica.log')
180                                     ->at($this->root);
181
182                 chdir($this->root->getChild('logs')->url());
183
184                 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('../friendica.log')->once();
185
186                 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
187
188                 $logger->info('Test');
189         }
190 }