Passed
Push — master ( 4286a4...290de6 )
by Carlos
05:27
created

MiniLogTest::testMessageCountWithSameContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 14
rs 9.9
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of FacturaScripts
4
 * Copyright (C) 2017-2021 Carlos Garcia Gomez <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace FacturaScripts\Test\Core\Base;
21
22
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
23
use FacturaScripts\Core\Base\MiniLog;
24
use FacturaScripts\Core\Base\Translator;
25
use FacturaScripts\Core\Model\LogMessage;
26
use FacturaScripts\Test\Core\LogErrorsTrait;
27
use PHPUnit\Framework\TestCase;
28
29
/**
30
 * Description of MiniLogTest
31
 *
32
 * @author Carlos Carlos Garcia Gomez <[email protected]>
33
 * @covers \FacturaScripts\Core\Base\MiniLog
34
 */
35
final class MiniLogTest extends TestCase
36
{
37
    use LogErrorsTrait;
38
39
    const CHANNEL = 'test';
40
41
    public function testCritical()
42
    {
43
        MiniLog::clear(self::CHANNEL);
44
        $logger = new MiniLog(self::CHANNEL);
45
        $message = 'test-critical';
46
        $context = ['test-key' => 'FAIL', 'other-key' => 'other', 'more' => 1234];
47
        $logger->critical($message, $context);
48
49
        $data = MiniLog::read(self::CHANNEL);
50
        $this->assertNotEmpty($data, 'log-empty');
51
        $this->assertEquals(1, count($data), 'more-than-one-message');
52
        $this->assertEquals($message, $data[0]['message']);
53
        $this->assertNotEmpty($data[0]['context'], 'empty-context');
54
        $this->assertGreaterThanOrEqual(3, count($data[0]['context']), 'bad-context');
55
        $this->assertEquals('other', $data[0]['context']['other-key'], 'bad-context-key');
56
        $this->assertEquals('critical', $data[0]['level']);
57
        $this->assertEquals(self::CHANNEL, $data[0]['channel']);
58
    }
59
60
    public function testError()
61
    {
62
        MiniLog::clear(self::CHANNEL);
63
        $logger = new MiniLog(self::CHANNEL);
64
        $message = 'test-error';
65
        $context = ['test-key' => '78687687681111'];
66
        $logger->error($message, $context);
67
68
        $data = MiniLog::read(self::CHANNEL);
69
        $this->assertNotEmpty($data, 'log-empty');
70
        $this->assertEquals(1, count($data), 'more-than-one-message');
71
        $this->assertEquals($message, $data[0]['message']);
72
        $this->assertNotEmpty($data[0]['context'], 'empty-context');
73
        $this->assertEquals('error', $data[0]['level']);
74
        $this->assertEquals(self::CHANNEL, $data[0]['channel']);
75
    }
76
77
    public function testInfo()
78
    {
79
        MiniLog::clear(self::CHANNEL);
80
        $logger = new MiniLog(self::CHANNEL);
81
        $message = 'test-info';
82
        $context = ['test-key' => '78686867'];
83
        $logger->info($message, $context);
84
85
        $data = MiniLog::read(self::CHANNEL);
86
        $this->assertNotEmpty($data, 'log-empty');
87
        $this->assertEquals(1, count($data), 'more-than-one-message');
88
        $this->assertEquals($message, $data[0]['message']);
89
        $this->assertNotEmpty($data[0]['context'], 'empty-context');
90
        $this->assertEquals('info', $data[0]['level']);
91
        $this->assertEquals(self::CHANNEL, $data[0]['channel']);
92
    }
93
94
    public function testNotice()
95
    {
96
        MiniLog::clear(self::CHANNEL);
97
        $logger = new MiniLog(self::CHANNEL);
98
        $message = 'test-notice';
99
        $context = ['test-key' => '3232324'];
100
        $logger->notice($message, $context);
101
102
        $data = MiniLog::read(self::CHANNEL);
103
        $this->assertNotEmpty($data, 'log-empty');
104
        $this->assertEquals(1, count($data), 'more-than-one-message');
105
        $this->assertEquals($message, $data[0]['message']);
106
        $this->assertNotEmpty($data[0]['context'], 'empty-context');
107
        $this->assertEquals('notice', $data[0]['level']);
108
        $this->assertEquals(self::CHANNEL, $data[0]['channel']);
109
    }
110
111
    public function testWarning()
112
    {
113
        MiniLog::clear(self::CHANNEL);
114
        $logger = new MiniLog(self::CHANNEL);
115
        $message = 'test-war';
116
        $context = ['test-key' => 'war'];
117
        $logger->warning($message, $context);
118
119
        $data = MiniLog::read(self::CHANNEL);
120
        $this->assertNotEmpty($data, 'log-empty');
121
        $this->assertEquals(1, count($data), 'more-than-one-message');
122
        $this->assertEquals($message, $data[0]['message']);
123
        $this->assertNotEmpty($data[0]['context'], 'empty-context');
124
        $this->assertEquals('warning', $data[0]['level']);
125
        $this->assertEquals(self::CHANNEL, $data[0]['channel']);
126
    }
127
128
    public function testClearChannel()
129
    {
130
        $defaultLogger = new MiniLog();
131
        $defaultLogger->info('test-default');
132
        $data = MiniLog::read(MiniLog::DEFAULT_CHANNEL);
133
        $this->assertNotEmpty($data, 'default-log-empty');
134
        $this->assertEquals(MiniLog::DEFAULT_CHANNEL, $data[0]['channel']);
135
136
        $logger = new MiniLog(self::CHANNEL);
137
        $logger->info('test');
138
        $this->assertNotEmpty(MiniLog::read(self::CHANNEL), 'log-empty');
139
140
        MiniLog::clear(self::CHANNEL);
141
        $this->assertEmpty(MiniLog::read(self::CHANNEL), 'log-not-empty');
142
        $this->assertNotEmpty(MiniLog::read(MiniLog::DEFAULT_CHANNEL), 'default-channel-log-empty');
143
        $this->assertNotEmpty(MiniLog::read(), 'all-channels-log-empty');
144
    }
145
146
    public function testClearAllChannels()
147
    {
148
        $defaultLogger = new MiniLog();
149
        $defaultLogger->info('test-default');
150
151
        $logger = new MiniLog(self::CHANNEL);
152
        $logger->info('test');
153
154
        MiniLog::clear();
155
        $this->assertEmpty(MiniLog::read(self::CHANNEL), 'log-not-empty');
156
        $this->assertEmpty(MiniLog::read(), 'default-channel-log-not-empty');
157
    }
158
159
    public function testMessageCount()
160
    {
161
        MiniLog::clear();
162
        $defaultLogger = new MiniLog();
163
        $defaultLogger->info('test');
164
165
        $logger = new MiniLog(self::CHANNEL);
166
        $logger->info('test');
167
        $logger->info('test');
168
169
        $data = MiniLog::read(self::CHANNEL);
170
        $this->assertNotEmpty($data, 'log-empty');
171
        $this->assertEquals(1, count($data), 'more-than-one-message');
172
        $this->assertEquals('test', $data[0]['message']);
173
        $this->assertEquals(2, $data[0]['count']);
174
    }
175
176
    public function testMessageCountWithSameContext()
177
    {
178
        MiniLog::clear();
179
        $logger = new MiniLog(self::CHANNEL);
180
        $msg = 'test-msg';
181
        $context = ['key1' => 1, 'key2' => 'val-2'];
182
        $logger->info($msg, $context);
183
        $logger->info($msg, $context);
184
185
        $data = MiniLog::read(self::CHANNEL);
186
        $this->assertNotEmpty($data, 'log-empty');
187
        $this->assertEquals(1, count($data), 'more-than-one-message');
188
        $this->assertEquals($msg, $data[0]['message']);
189
        $this->assertEquals(2, $data[0]['count']);
190
    }
191
192
    public function testMessageCountWithDifferentContext()
193
    {
194
        MiniLog::clear();
195
        $logger = new MiniLog(self::CHANNEL);
196
        $msg = 'test-msg-2';
197
        $context1 = ['key1' => 1, 'key2' => 'val-2'];
198
        $context2 = ['key1' => 2, 'key-7' => 'val-999'];
199
        $logger->info($msg, $context1);
200
        $logger->info($msg, $context2);
201
202
        $data = MiniLog::read(self::CHANNEL);
203
        $this->assertNotEmpty($data, 'log-empty');
204
        $this->assertEquals(2, count($data), 'not-two-messages');
205
        $this->assertEquals($msg, $data[0]['message']);
206
        $this->assertEquals(1, $data[0]['count']);
207
    }
208
209
    public function testMessageCountTranslated()
210
    {
211
        // test translation
212
        $i18n = new Translator();
213
        $key = 'field-can-not-be-null';
214
        $trans1 = $i18n->trans($key, ['%fieldName%' => 1]);
215
        $trans2 = $i18n->trans($key, ['%fieldName%' => 2]);
216
        $this->assertNotEquals($trans1, $trans2, 'translations-equals');
217
218
        // add log messages
219
        MiniLog::clear();
220
        $logger = new MiniLog(self::CHANNEL, $i18n);
221
        $logger->info($key, ['%fieldName%' => 1]);
222
        $logger->info($key, ['%fieldName%' => 2]);
223
224
        // check log
225
        $data = MiniLog::read(self::CHANNEL);
226
        $this->assertNotEmpty($data, 'log-empty');
227
        $this->assertEquals(2, count($data), 'not-two-messages');
228
        $this->assertEquals($trans1, $data[0]['message']);
229
        $this->assertEquals(1, $data[0]['count']);
230
    }
231
232
    public function testGlobalContext()
233
    {
234
        $key = 'tk-8787';
235
        $value1 = 'val-54545';
236
        $value2 = 'value-2';
237
        MiniLog::setContext($key, $value1);
238
        $this->assertEquals($value1, MiniLog::getContext($key));
239
        MiniLog::setContext($key, $value2);
240
        $this->assertEquals($value2, MiniLog::getContext($key));
241
    }
242
243
    public function testRead()
244
    {
245
        MiniLog::clear(self::CHANNEL);
246
        $logger = new MiniLog(self::CHANNEL);
247
        $logger->info('test');
248
249
        $this->assertNotEmpty(MiniLog::read(), 'full-log-empty');
250
        $this->assertNotEmpty(MiniLog::read(self::CHANNEL), 'channel-log-empty');
251
        $this->assertNotEmpty(MiniLog::read(self::CHANNEL, ['info']), 'channel-level-log-empty');
252
        $this->assertEmpty(MiniLog::read(self::CHANNEL, ['error']), 'channel-level-error-not-empty');
253
    }
254
255
    public function testSave()
256
    {
257
        // crear data
258
        MiniLog::clear();
259
        $logModel = new LogMessage();
260
        $where = [new DataBaseWhere('channel', self::CHANNEL)];
261
        foreach ($logModel->all($where, [], 0, 0) as $item) {
262
            $item->delete();
263
        }
264
265
        $logger = new MiniLog(self::CHANNEL);
266
        $logger->error('test', ['more' => 'one']);
267
        $this->assertTrue(MiniLog::save(), 'cant-save-log');
268
        $this->assertEmpty(MiniLog::read(self::CHANNEL), 'log-not-empty');
269
270
        // verify data from database
271
        $items = $logModel->all($where, [], 0, 0);
272
        $this->assertNotEmpty($items, 'log-item-not-found-in-db');
273
        $this->assertEquals(1, count($items), 'more-than-one-log-item-in-db');
274
        $this->assertEquals('test', $items[0]->message);
275
        $this->assertEquals('error', $items[0]->level);
276
        $this->assertEquals(self::CHANNEL, $items[0]->channel);
277
278
        // check context
279
        $context = $items[0]->context();
0 ignored issues
show
Bug introduced by
The method context() does not exist on FacturaScripts\Core\Model\Base\ModelClass. It seems like you code against a sub-type of said class. However, the method does not exist in FacturaScripts\Core\Model\Base\Contact or FacturaScripts\Core\Model\Base\Address or FacturaScripts\Dinamic\Model\Base\ModelClass or FacturaScripts\Core\Model\Base\ModelOnChangeClass or FacturaScripts\Core\Model\Base\BankAccount or FacturaScripts\Core\Model\Base\Payment or FacturaScripts\Core\Model\Base\ReportAccounting or FacturaScripts\Dinamic\Model\Base\Contact or FacturaScripts\Core\Model\Base\ComercialContact or FacturaScripts\Dinamic\Model\Base\ComercialContact or FacturaScripts\Dinamic\Model\Base\Address or FacturaScripts\Core\Model\Base\Receipt or FacturaScripts\Core\Mode...se\BusinessDocumentLine or FacturaScripts\Dinamic\M...Base\ModelOnChangeClass or FacturaScripts\Core\Model\Base\BusinessDocument or FacturaScripts\Dinamic\Model\Base\Receipt or FacturaScripts\Dinamic\M...se\BusinessDocumentLine or FacturaScripts\Core\Mode...se\PurchaseDocumentLine or FacturaScripts\Core\Model\Base\SalesDocumentLine or FacturaScripts\Dinamic\M...se\PurchaseDocumentLine or FacturaScripts\Dinamic\M...\Base\SalesDocumentLine or FacturaScripts\Dinamic\Model\Base\BusinessDocument or FacturaScripts\Core\Model\Base\TransformerDocument or FacturaScripts\Core\Model\Base\PurchaseDocument or FacturaScripts\Dinamic\M...ase\TransformerDocument or FacturaScripts\Core\Model\Base\SalesDocument or FacturaScripts\Dinamic\Model\Base\PurchaseDocument or FacturaScripts\Dinamic\Model\Base\SalesDocument or FacturaScripts\Dinamic\Model\Base\BankAccount or FacturaScripts\Dinamic\Model\Base\Payment or FacturaScripts\Dinamic\Model\Base\ReportAccounting. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

279
        /** @scrutinizer ignore-call */ 
280
        $context = $items[0]->context();
Loading history...
280
        $this->assertIsArray($context, 'db-context-is-not-an-array');
281
        $this->assertEquals('one', $context['more'], 'bad-db-context-key');
282
    }
283
284
    protected function tearDown()
285
    {
286
        $this->logErrors();
287
    }
288
}
289