mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-27 08:18:24 +03:00
Created delegator factory that injects logger on services implementing LoggerAware, and used it for locks factory
This commit is contained in:
parent
8db9962282
commit
38016b3ba3
3 changed files with 79 additions and 0 deletions
|
@ -3,6 +3,7 @@ declare(strict_types=1);
|
|||
|
||||
use Shlinkio\Shlink\Common\Cache\RedisFactory;
|
||||
use Shlinkio\Shlink\Common\Lock\RetryLockStoreDelegatorFactory;
|
||||
use Shlinkio\Shlink\Common\Logger\LoggerAwareDelegatorFactory;
|
||||
use Symfony\Component\Lock;
|
||||
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
|
||||
|
||||
|
@ -27,6 +28,9 @@ return [
|
|||
Lock\Store\RedisStore::class => [
|
||||
RetryLockStoreDelegatorFactory::class,
|
||||
],
|
||||
Lock\Factory::class => [
|
||||
LoggerAwareDelegatorFactory::class,
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
|
|
20
module/Common/src/Logger/LoggerAwareDelegatorFactory.php
Normal file
20
module/Common/src/Logger/LoggerAwareDelegatorFactory.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Common\Logger;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log;
|
||||
|
||||
class LoggerAwareDelegatorFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container, $name, callable $callback)
|
||||
{
|
||||
$instance = $callback();
|
||||
if ($instance instanceof Log\LoggerAwareInterface) {
|
||||
$instance->setLogger($container->get(Log\LoggerInterface::class));
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Common\Logger;
|
||||
|
||||
use PHPUnit\Framework\Assert;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Shlinkio\Shlink\Common\Logger\LoggerAwareDelegatorFactory;
|
||||
use stdClass;
|
||||
|
||||
class LoggerAwareDelegatorFactoryTest extends TestCase
|
||||
{
|
||||
/** @var LoggerAwareDelegatorFactory */
|
||||
private $delegator;
|
||||
/** @var ObjectProphecy */
|
||||
private $container;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->container = $this->prophesize(ContainerInterface::class);
|
||||
$this->delegator = new LoggerAwareDelegatorFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideInstances
|
||||
*/
|
||||
public function injectsLoggerOnInstanceWhenImplementingLoggerAware($instance, int $expectedCalls): void
|
||||
{
|
||||
$callback = function () use ($instance) {
|
||||
return $instance;
|
||||
};
|
||||
$getLogger = $this->container->get(Log\LoggerInterface::class)->willReturn(new Log\NullLogger());
|
||||
|
||||
$result = ($this->delegator)($this->container->reveal(), '', $callback);
|
||||
|
||||
$this->assertSame($instance, $result);
|
||||
$getLogger->shouldHaveBeenCalledTimes($expectedCalls);
|
||||
}
|
||||
|
||||
public function provideInstances(): iterable
|
||||
{
|
||||
yield 'no logger aware' => [new stdClass(), 0];
|
||||
yield 'logger aware' => [new class implements Log\LoggerAwareInterface {
|
||||
public function setLogger(LoggerInterface $logger): void
|
||||
{
|
||||
Assert::assertInstanceOf(Log\NullLogger::class, $logger);
|
||||
}
|
||||
}, 1];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue