Passed
Pull Request — master (#167)
by Jérémy
02:06
created

LoggingInteractorDecoratorTest::provideMethods()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 0
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Ekino New Relic bundle.
5
 *
6
 * (c) Ekino - Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ekino\Bundle\NewRelicBundle\Tests\NewRelic;
13
14
use Ekino\Bundle\NewRelicBundle\NewRelic\LoggingInteractorDecorator;
15
use Ekino\Bundle\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
16
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Psr\Log\LoggerInterface;
18
19
class LoggingInteractorDecoratorTest extends TestCase
20
{
21
    /**
22
     * @dataProvider provideMethods
23
     */
24
    public function testGeneric(string $method, array $arguments, $return)
25
    {
26
        $logger = $this->createMock(LoggerInterface::class);
27
        $decorated = $this->createMock(LoggingInteractorDecorator::class);
28
        $interactor = new LoggingInteractorDecorator($decorated, $logger);
29
30
        $logger->expects($this->once())->method('debug');
31
        $decorated->expects($this->once())->method($method)
32
            ->with(...$arguments)
33
            ->willReturn($return);
34
35
        $result = $interactor->$method(...$arguments);
36
37
        $this->assertSame($return, $result);
38
39
    }
40
41
    public function provideMethods()
42
    {
43
        $reflection = new \ReflectionClass(NewRelicInteractorInterface::class);
44
        foreach ($reflection->getMethods() as $method) {
45
            if (!$method->isPublic()) {
46
                continue;
47
            }
48
            if ($method->isStatic()) {
49
                continue;
50
            }
51
52
            $arguments = array_map(function(\ReflectionParameter $parameter) {
53
                return $this->getTypeStub($parameter->getType());
54
            }, $method->getParameters());
55
56
            $return = $method->hasReturnType() ? $this->getTypeStub($method->getReturnType()) : null;
57
58
            yield [$method->getName(), $arguments, $return];
59
        }
60
    }
61
62
    private function getTypeStub(?\ReflectionType $type)
63
    {
64
        if ($type === null) {
65
            return uniqid('', null);
66
        }
67
68
        switch ($type->getName()) {
69
            case 'string':
70
                return uniqid('', null);
71
            case 'bool':
72
                return (bool) rand(0, 1);
73
            case 'float':
74
                return rand(0, 100) / rand(1, 10);
75
            case 'int':
76
                return rand(0, 100);
77
            case 'void':
78
                return null;
79
            case 'Throwable':
80
                return new \Exception();
81
            case 'callable':
82
                return function() {};
83
            case 'array':
84
                return array_fill(0, 2, uniqid('', true));
85
            default:
86
                throw new \UnexpectedValueException('Unknow type. '.$type->getName());
87
        }
88
    }
89
}
90