Skip to content

Commit

Permalink
Initial code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed May 2, 2018
1 parent a4826c4 commit 6391ed7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/CallableThrowableLogger.php
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace WyriHaximus\PSR3\CallableThrowableLogger;

use Psr\Log\LoggerInterface;
use Throwable;

final class CallableThrowableLogger
{
const MESSAGE = 'Uncaught Throwable %s: "%s" at %s line %s';

public static function create(LoggerInterface $logger, string $level = 'error'): callable
{
return function (Throwable $throwable) use ($logger, $level) {
$logger->log(
$level,
\sprintf(
self::MESSAGE,
\get_class($throwable),
$throwable->getMessage(),
$throwable->getFile(),
$throwable->getLine()
),
[
'throwable' => $throwable,
]
);
};
}
}
42 changes: 42 additions & 0 deletions tests/CallableThrowableLoggerTest.php
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace WyriHaximus\Tests\PSR3\CallableThrowableLogger;

use Exception;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Throwable;
use WyriHaximus\PSR3\CallableThrowableLogger\CallableThrowableLogger;

final class CallableThrowableLoggerTest extends TestCase
{
public function provideThrowables()
{
yield [new Exception('foo.bar')];
yield [new CallableThrowableLoggerTestException('bar.foo')];
}

/**
* @dataProvider provideThrowables
*/
public function testCallable(Throwable $throwable)
{
$logger = $this->prophesize(LoggerInterface::class);
$logger->log(
'error',
\sprintf(
CallableThrowableLogger::MESSAGE,
\get_class($throwable),
$throwable->getMessage(),
$throwable->getFile(),
$throwable->getLine()
),
[
'throwable' => $throwable,
]
)->shouldBeCalled();

$callable = CallableThrowableLogger::create($logger->reveal());
$callable($throwable);
}
}
9 changes: 9 additions & 0 deletions tests/CallableThrowableLoggerTestException.php
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace WyriHaximus\Tests\PSR3\CallableThrowableLogger;

use Exception;

final class CallableThrowableLoggerTestException extends Exception
{
}

0 comments on commit 6391ed7

Please sign in to comment.