Completed
Push — master ( 6c6445...a19924 )
by Tobias
02:10
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
cc 10
eloc 21
nc 10
nop 1
dl 0
loc 25
rs 4.8196
c 0
b 0
f 0

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
/*
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
    public function provideMethods()
41
    {
42
        $reflection = new \ReflectionClass(NewRelicInteractorInterface::class);
43
        foreach ($reflection->getMethods() as $method) {
44
            if (!$method->isPublic()) {
45
                continue;
46
            }
47
            if ($method->isStatic()) {
48
                continue;
49
            }
50
51
            $arguments = array_map(function (\ReflectionParameter $parameter) {
52
                return $this->getTypeStub($parameter->getType());
53
            }, $method->getParameters());
54
55
            $return = $method->hasReturnType() ? $this->getTypeStub($method->getReturnType()) : null;
56
57
            yield [$method->getName(), $arguments, $return];
58
        }
59
    }
60
61
    private function getTypeStub(?\ReflectionType $type)
62
    {
63
        if (null === $type) {
64
            return uniqid('', null);
65
        }
66
67
        switch ($type->getName()) {
68
            case 'string':
69
                return uniqid('', null);
70
            case 'bool':
71
                return (bool) rand(0, 1);
72
            case 'float':
73
                return rand(0, 100) / rand(1, 10);
74
            case 'int':
75
                return rand(0, 100);
76
            case 'void':
77
                return null;
78
            case 'Throwable':
79
                return new \Exception();
80
            case 'callable':
81
                return function () {};
82
            case 'array':
83
                return array_fill(0, 2, uniqid('', true));
84
            default:
85
                throw new \UnexpectedValueException('Unknow type. '.$type->getName());
86
        }
87
    }
88
}
89