2019-02-22 19:10:27 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-02-22 19:10:27 +00:00
|
|
|
|
2019-02-27 11:32:56 +00:00
|
|
|
namespace Friendica\Test\src\Util\Logger;
|
2019-02-22 19:10:27 +00:00
|
|
|
|
|
|
|
use Friendica\Test\MockedTest;
|
|
|
|
use Friendica\Util\Logger\WorkerLogger;
|
2019-02-24 12:40:54 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-02-22 19:10:27 +00:00
|
|
|
|
|
|
|
class WorkerLoggerTest extends MockedTest
|
|
|
|
{
|
|
|
|
private function assertUid($uid, $length = 7)
|
|
|
|
{
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertRegExp('/^[a-zA-Z0-9]{' . $length . '}+$/', $uid);
|
2019-02-22 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the a id with length zero
|
2019-02-27 17:32:25 +00:00
|
|
|
* @expectedException \Error
|
2019-02-22 19:10:27 +00:00
|
|
|
*/
|
|
|
|
public function testGetWorkerIdZero()
|
|
|
|
{
|
2019-02-24 12:40:54 +00:00
|
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
2019-02-22 19:10:27 +00:00
|
|
|
new WorkerLogger($logger, 'test', 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the generated Uid
|
|
|
|
*/
|
|
|
|
public function testGetWorkerId()
|
|
|
|
{
|
2019-02-24 12:40:54 +00:00
|
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
2019-02-22 19:10:27 +00:00
|
|
|
for ($i = 1; $i < 14; $i++) {
|
|
|
|
$workLogger = new WorkerLogger($logger, 'test', $i);
|
|
|
|
$uid = $workLogger->getWorkerId();
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertUid($uid, $i);
|
2019-02-22 19:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTest()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'info' => [
|
|
|
|
'func' => 'info',
|
|
|
|
'msg' => 'the alert',
|
|
|
|
'context' => [],
|
|
|
|
],
|
|
|
|
'alert' => [
|
|
|
|
'func' => 'alert',
|
|
|
|
'msg' => 'another alert',
|
|
|
|
'context' => ['test' => 'it'],
|
|
|
|
],
|
|
|
|
'critical' => [
|
|
|
|
'func' => 'critical',
|
|
|
|
'msg' => 'Critical msg used',
|
|
|
|
'context' => ['test' => 'it', 'more' => 0.24545],
|
|
|
|
],
|
|
|
|
'error' => [
|
|
|
|
'func' => 'error',
|
|
|
|
'msg' => 21345623,
|
|
|
|
'context' => ['test' => 'it', 'yet' => true],
|
|
|
|
],
|
|
|
|
'warning' => [
|
|
|
|
'func' => 'warning',
|
|
|
|
'msg' => 'another alert' . 123523 . 324.54534 . 'test',
|
|
|
|
'context' => ['test' => 'it', 2 => 'nope'],
|
|
|
|
],
|
|
|
|
'notice' => [
|
|
|
|
'func' => 'notice',
|
|
|
|
'msg' => 'Notice' . ' alert' . true . 'with' . '\'strange\'' . 1.24. 'behavior',
|
|
|
|
'context' => ['test' => 'it'],
|
|
|
|
],
|
|
|
|
'debug' => [
|
|
|
|
'func' => 'debug',
|
|
|
|
'msg' => 'at last a debug',
|
|
|
|
'context' => ['test' => 'it'],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the WorkerLogger with different log calls
|
|
|
|
* @dataProvider dataTest
|
|
|
|
*/
|
|
|
|
public function testEmergency($func, $msg, $context = [])
|
|
|
|
{
|
2019-02-24 12:40:54 +00:00
|
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
2019-02-22 19:10:27 +00:00
|
|
|
$workLogger = new WorkerLogger($logger, 'test');
|
|
|
|
$testContext = $context;
|
|
|
|
$testContext['worker_id'] = $workLogger->getWorkerId();
|
|
|
|
$testContext['worker_cmd'] = 'test';
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertUid($testContext['worker_id']);
|
2019-02-22 19:10:27 +00:00
|
|
|
$logger
|
|
|
|
->shouldReceive($func)
|
|
|
|
->with($msg, $testContext)
|
|
|
|
->once();
|
|
|
|
$workLogger->$func($msg, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the WorkerLogger with
|
|
|
|
*/
|
|
|
|
public function testLog()
|
|
|
|
{
|
2019-02-24 12:40:54 +00:00
|
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
2019-02-22 19:10:27 +00:00
|
|
|
$workLogger = new WorkerLogger($logger, 'test');
|
|
|
|
$context = $testContext = ['test' => 'it'];
|
|
|
|
$testContext['worker_id'] = $workLogger->getWorkerId();
|
|
|
|
$testContext['worker_cmd'] = 'test';
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertUid($testContext['worker_id']);
|
2019-02-22 19:10:27 +00:00
|
|
|
$logger
|
|
|
|
->shouldReceive('log')
|
|
|
|
->with('debug', 'a test', $testContext)
|
|
|
|
->once();
|
|
|
|
$workLogger->log('debug', 'a test', $context);
|
|
|
|
}
|
|
|
|
}
|