Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"packaged/docblock": "~1.1",
"packaged/i18n": "~1.2",
"symfony/console": "~4.2||~5.0",
"psr/log": "~1.1"
"psr/log": "~1.1||^2.0||^3.0"
},
"suggest": {
"ext-xdebug": "*"
Expand Down
2 changes: 1 addition & 1 deletion src/Cubex.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function getLogger()
*
* @return void
*/
public function setLogger(LoggerInterface $logger)
public function setLogger(LoggerInterface $logger): void
{
$this->share(LoggerInterface::class, $logger);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Logger/ErrorLogLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ErrorLogLogger extends AbstractLogger
*
* @throws \Exception
*/
public function log($level, $message, array $context = [])
public function log($level, $message, array $context = []): void
{
$requestId = $this->hasContext() ? $this->getContext()->id() : Strings::randomString(10);
$now = \DateTime::createFromFormat('U.u', sprintf("%.6F", microtime(true)));
Expand Down
6 changes: 3 additions & 3 deletions tests/CubexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Cubex\Events\ShutdownEvent;
use Cubex\Logger\ErrorLogLogger;
use Cubex\Routing\Router;
use Cubex\Tests\Logger\MockLogger;
use Cubex\Tests\Supporting\Console\TestExceptionCommand;
use Cubex\Tests\Supporting\Http\TestResponse;
use Exception;
Expand All @@ -23,7 +24,6 @@
use Packaged\Routing\Handler\FuncHandler;
use Packaged\Routing\Handler\Handler;
use PHPUnit\Framework\TestCase;
use Psr\Log\Test\TestLogger;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
Expand Down Expand Up @@ -98,7 +98,7 @@ public function testHandle()
public function testHandleCompleteException()
{
$cubex = $this->_cubex();
$logger = new TestLogger();
$logger = new MockLogger();
$cubex->setLogger($logger);
$router = new Router();
$router->onPath(
Expand Down Expand Up @@ -244,7 +244,7 @@ public function _defaultShutdownProvider()
public function testLog()
{
$cubex = new Cubex(__DIR__, null, true);
$logger = new TestLogger();
$logger = new MockLogger();
$cubex->setLogger($logger);
Cubex::log()->error("TEST");
self::assertTrue($logger->hasError("TEST"));
Expand Down
147 changes: 147 additions & 0 deletions tests/Logger/MockLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Cubex\Tests\Logger;

use Psr\Log\AbstractLogger;

/**
* Used for testing purposes.
*
* It records all records and gives you access to them for verification.
*
* @method bool hasEmergency($record)
* @method bool hasAlert($record)
* @method bool hasCritical($record)
* @method bool hasError($record)
* @method bool hasWarning($record)
* @method bool hasNotice($record)
* @method bool hasInfo($record)
* @method bool hasDebug($record)
*
* @method bool hasEmergencyRecords()
* @method bool hasAlertRecords()
* @method bool hasCriticalRecords()
* @method bool hasErrorRecords()
* @method bool hasWarningRecords()
* @method bool hasNoticeRecords()
* @method bool hasInfoRecords()
* @method bool hasDebugRecords()
*
* @method bool hasEmergencyThatContains($message)
* @method bool hasAlertThatContains($message)
* @method bool hasCriticalThatContains($message)
* @method bool hasErrorThatContains($message)
* @method bool hasWarningThatContains($message)
* @method bool hasNoticeThatContains($message)
* @method bool hasInfoThatContains($message)
* @method bool hasDebugThatContains($message)
*
* @method bool hasEmergencyThatMatches($message)
* @method bool hasAlertThatMatches($message)
* @method bool hasCriticalThatMatches($message)
* @method bool hasErrorThatMatches($message)
* @method bool hasWarningThatMatches($message)
* @method bool hasNoticeThatMatches($message)
* @method bool hasInfoThatMatches($message)
* @method bool hasDebugThatMatches($message)
*
* @method bool hasEmergencyThatPasses($message)
* @method bool hasAlertThatPasses($message)
* @method bool hasCriticalThatPasses($message)
* @method bool hasErrorThatPasses($message)
* @method bool hasWarningThatPasses($message)
* @method bool hasNoticeThatPasses($message)
* @method bool hasInfoThatPasses($message)
* @method bool hasDebugThatPasses($message)
*/
class MockLogger extends AbstractLogger
{
/**
* @var array
*/
public $records = [];

public $recordsByLevel = [];

/**
* @inheritdoc
*/
public function log($level, $message, array $context = []): void
{
$record = [
'level' => $level,
'message' => $message,
'context' => $context,
];

$this->recordsByLevel[$record['level']][] = $record;
$this->records[] = $record;
}

public function hasRecords($level)
{
return isset($this->recordsByLevel[$level]);
}

public function hasRecord($record, $level)
{
if (is_string($record)) {
$record = ['message' => $record];
}
return $this->hasRecordThatPasses(function ($rec) use ($record) {
if ($rec['message'] !== $record['message']) {
return false;
}
if (isset($record['context']) && $rec['context'] !== $record['context']) {
return false;
}
return true;
}, $level);
}

public function hasRecordThatContains($message, $level)
{
return $this->hasRecordThatPasses(function ($rec) use ($message) {
return strpos($rec['message'], $message) !== false;
}, $level);
}

public function hasRecordThatMatches($regex, $level)
{
return $this->hasRecordThatPasses(function ($rec) use ($regex) {
return preg_match($regex, $rec['message']) > 0;
}, $level);
}

public function hasRecordThatPasses(callable $predicate, $level)
{
if (!isset($this->recordsByLevel[$level])) {
return false;
}
foreach ($this->recordsByLevel[$level] as $i => $rec) {
if (call_user_func($predicate, $rec, $i)) {
return true;
}
}
return false;
}

public function __call($method, $args)
{
if (preg_match('/(.*)(Debug|Info|Notice|Warning|Error|Critical|Alert|Emergency)(.*)/', $method, $matches) > 0) {
$genericMethod = $matches[1] . ('Records' !== $matches[3] ? 'Record' : '') . $matches[3];
$level = strtolower($matches[2]);
if (method_exists($this, $genericMethod)) {
$args[] = $level;
return call_user_func_array([$this, $genericMethod], $args);
}
}
throw new \BadMethodCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()');
}

public function reset()
{
$this->records = [];
$this->recordsByLevel = [];
}
}
Loading