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