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