1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Common\Logger; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\Assert; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
9
|
|
|
use Psr\Container\ContainerInterface; |
10
|
|
|
use Psr\Log; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
use Shlinkio\Shlink\Common\Logger\LoggerAwareDelegatorFactory; |
13
|
|
|
use stdClass; |
14
|
|
|
|
15
|
|
|
class LoggerAwareDelegatorFactoryTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var LoggerAwareDelegatorFactory */ |
18
|
|
|
private $delegator; |
19
|
|
|
/** @var ObjectProphecy */ |
20
|
|
|
private $container; |
21
|
|
|
|
22
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
$this->container = $this->prophesize(ContainerInterface::class); |
25
|
|
|
$this->delegator = new LoggerAwareDelegatorFactory(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @test |
30
|
|
|
* @dataProvider provideInstances |
31
|
|
|
*/ |
32
|
|
|
public function injectsLoggerOnInstanceWhenImplementingLoggerAware($instance, int $expectedCalls): void |
33
|
|
|
{ |
34
|
|
|
$callback = function () use ($instance) { |
35
|
|
|
return $instance; |
36
|
|
|
}; |
37
|
|
|
$getLogger = $this->container->get(Log\LoggerInterface::class)->willReturn(new Log\NullLogger()); |
38
|
|
|
|
39
|
|
|
$result = ($this->delegator)($this->container->reveal(), '', $callback); |
40
|
|
|
|
41
|
|
|
$this->assertSame($instance, $result); |
42
|
|
|
$getLogger->shouldHaveBeenCalledTimes($expectedCalls); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function provideInstances(): iterable |
46
|
|
|
{ |
47
|
|
|
yield 'no logger aware' => [new stdClass(), 0]; |
48
|
|
|
yield 'logger aware' => [new class implements Log\LoggerAwareInterface { |
49
|
|
|
public function setLogger(LoggerInterface $logger): void |
50
|
|
|
{ |
51
|
|
|
Assert::assertInstanceOf(Log\NullLogger::class, $logger); |
52
|
|
|
} |
53
|
|
|
}, 1]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|