|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Ekino New Relic bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Ekino - Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Ekino\NewRelicBundle\Tests\NewRelic; |
|
15
|
|
|
|
|
16
|
|
|
use Ekino\NewRelicBundle\NewRelic\LoggingInteractorDecorator; |
|
17
|
|
|
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
use Psr\Log\LoggerInterface; |
|
20
|
|
|
|
|
21
|
|
|
class LoggingInteractorDecoratorTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @dataProvider provideMethods |
|
25
|
|
|
*/ |
|
26
|
|
|
public function testGeneric(string $method, array $arguments, $return) |
|
27
|
|
|
{ |
|
28
|
|
|
$logger = $this->createMock(LoggerInterface::class); |
|
29
|
|
|
$decorated = $this->createMock(LoggingInteractorDecorator::class); |
|
30
|
|
|
$interactor = new LoggingInteractorDecorator($decorated, $logger); |
|
31
|
|
|
|
|
32
|
|
|
$logger->expects($this->once())->method('debug'); |
|
33
|
|
|
$decorated->expects($this->once())->method($method) |
|
34
|
|
|
->with(...$arguments) |
|
35
|
|
|
->willReturn($return); |
|
36
|
|
|
|
|
37
|
|
|
$result = $interactor->$method(...$arguments); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertSame($return, $result); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function provideMethods() |
|
43
|
|
|
{ |
|
44
|
|
|
$reflection = new \ReflectionClass(NewRelicInteractorInterface::class); |
|
45
|
|
|
foreach ($reflection->getMethods() as $method) { |
|
46
|
|
|
if (!$method->isPublic()) { |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
if ($method->isStatic()) { |
|
50
|
|
|
continue; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$arguments = \array_map(function (\ReflectionParameter $parameter) { |
|
54
|
|
|
return $this->getTypeStub($parameter->getType()); |
|
55
|
|
|
}, $method->getParameters()); |
|
56
|
|
|
|
|
57
|
|
|
$return = $method->hasReturnType() ? $this->getTypeStub($method->getReturnType()) : null; |
|
58
|
|
|
|
|
59
|
|
|
yield [$method->getName(), $arguments, $return]; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function getTypeStub(?\ReflectionType $type) |
|
64
|
|
|
{ |
|
65
|
|
|
if (null === $type) { |
|
66
|
|
|
return \uniqid('', true); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
switch ($type->getName()) { |
|
70
|
|
|
case 'string': |
|
71
|
|
|
return \uniqid('', true); |
|
72
|
|
|
case 'bool': |
|
73
|
|
|
return (bool) \rand(0, 1); |
|
74
|
|
|
case 'float': |
|
75
|
|
|
return \rand(0, 100) / \rand(1, 10); |
|
76
|
|
|
case 'int': |
|
77
|
|
|
return \rand(0, 100); |
|
78
|
|
|
case 'void': |
|
79
|
|
|
return null; |
|
80
|
|
|
case 'Throwable': |
|
81
|
|
|
return new \Exception(); |
|
82
|
|
|
case 'callable': |
|
83
|
|
|
return function () { |
|
84
|
|
|
}; |
|
85
|
|
|
case 'array': |
|
86
|
|
|
return \array_fill(0, 2, \uniqid('', true)); |
|
87
|
|
|
default: |
|
88
|
|
|
throw new \UnexpectedValueException('Unknow type. '.$type->getName()); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|