]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/Logger/AbstractLoggerTest.php
Merge pull request #7728 from MrPetovan/task/7682-better-insert-link-button
[friendica.git] / tests / src / Util / Logger / AbstractLoggerTest.php
1 <?php
2
3 namespace Friendica\Test\src\Util\Logger;
4
5 use Friendica\Test\MockedTest;
6 use Friendica\Util\Introspection;
7 use Mockery\MockInterface;
8 use Psr\Log\LoggerInterface;
9 use Psr\Log\LogLevel;
10
11 abstract class AbstractLoggerTest extends MockedTest
12 {
13         use LoggerDataTrait;
14
15         const LOGLINE = '/.* \[.*\]: .* \{.*\"file\":\".*\".*,.*\"line\":\d*,.*\"function\":\".*\".*,.*\"uid\":\".*\".*}/';
16
17         const FILE = 'test';
18         const LINE = 666;
19         const FUNC = 'myfunction';
20
21         /**
22          * @var Introspection|MockInterface
23          */
24         protected $introspection;
25
26         /**
27          * Returns the content of the current logger instance
28          *
29          * @return string
30          */
31         abstract protected function getContent();
32
33         /**
34          * Returns the current logger instance
35          *
36          * @param string $level the default loglevel
37          *
38          * @return LoggerInterface
39          */
40         abstract protected function getInstance($level = LogLevel::DEBUG);
41
42         protected function setUp()
43         {
44                 parent::setUp();
45
46                 $this->introspection = \Mockery::mock(Introspection::class);
47                 $this->introspection->shouldReceive('getRecord')->andReturn([
48                         'file'     => self::FILE,
49                         'line'     => self::LINE,
50                         'function' => self::FUNC
51                 ]);
52         }
53
54         public function assertLogline($string)
55         {
56                 $this->assertRegExp(self::LOGLINE, $string);
57         }
58
59         public function assertLoglineNums($assertNum, $string)
60         {
61                 $this->assertEquals($assertNum, preg_match_all(self::LOGLINE, $string));
62         }
63
64         /**
65          * Test if the logger works correctly
66          */
67         public function testNormal()
68         {
69                 $logger = $this->getInstance();
70                 $logger->emergency('working!');
71                 $logger->alert('working too!');
72                 $logger->debug('and now?');
73                 $logger->notice('message', ['an' => 'context']);
74
75                 $text = $this->getContent();
76                 $this->assertLogline($text);
77                 $this->assertLoglineNums(4, $text);
78         }
79
80         /**
81          * Test if a log entry is correctly interpolated
82          */
83         public function testPsrInterpolate()
84         {
85                 $logger = $this->getInstance();
86
87                 $logger->emergency('A {psr} test', ['psr' => 'working']);
88                 $logger->alert('An {array} test', ['array' => ['it', 'is', 'working']]);
89                 $text = $this->getContent();
90                 $this->assertContains('A working test', $text);
91                 $this->assertContains('An ["it","is","working"] test', $text);
92         }
93
94         /**
95          * Test if a log entry contains all necessary information
96          */
97         public function testContainsInformation()
98         {
99                 $logger = $this->getInstance();
100                 $logger->emergency('A test');
101
102                 $text = $this->getContent();
103                 $this->assertContains('"file":"' . self::FILE . '"', $text);
104                 $this->assertContains('"line":' . self::LINE, $text);
105                 $this->assertContains('"function":"' . self::FUNC . '"', $text);
106         }
107
108         /**
109          * Test if the minimum level is working
110          */
111         public function testMinimumLevel()
112         {
113                 $logger = $this->getInstance(LogLevel::NOTICE);
114
115                 $logger->emergency('working');
116                 $logger->alert('working');
117                 $logger->error('working');
118                 $logger->warning('working');
119                 $logger->notice('working');
120                 $logger->info('not working');
121                 $logger->debug('not working');
122
123                 $text = $this->getContent();
124
125                 $this->assertLoglineNums(5, $text);
126         }
127
128         /**
129          * Test with different logging data
130          * @dataProvider dataTests
131          */
132         public function testDifferentTypes($function, $message, array $context)
133         {
134                 $logger = $this->getInstance();
135                 $logger->$function($message, $context);
136
137                 $text = $this->getContent();
138
139                 $this->assertLogline($text);
140
141                 $this->assertContains(@json_encode($context), $text);
142         }
143 }