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