Passed
Push — master ( da9ebc...eab3b6 )
by Tobias
02:14
created

LoggingInteractorDecoratorTest::getTypeStub()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 21
nc 10
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
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...
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
            case 'array':
85
                return \array_fill(0, 2, \uniqid('', true));
86
            default:
87
                throw new \UnexpectedValueException('Unknow type. '.$type->getName());
88
        }
89
    }
90
}
91