Completed
Push — master ( b663ee...50d896 )
by Dmitry
06:21
created

Test::__construct()   C

Complexity

Conditions 11
Paths 1

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 11.0492

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 25
cts 27
cp 0.9259
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 26
nc 1
nop 0
crap 11.0492

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Test.php$0 ➔ __construct() 0 5 1
F Test.php$0 ➔ dispatch() 0 29 11

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
namespace Basis;
4
5
use Basis\Converter;
6
use Basis\Runner;
7
use Exception;
8
use PHPUnit\Framework\TestCase;
9
10
abstract class Test extends TestCase
11
{
12
    use Toolkit;
13
14
    public $params = [];
15
16 21
    public function __construct()
17
    {
18 1
        parent::__construct();
19
20
        $this->app = new class(getcwd(), $this) extends Application {
21 1
            public function __construct(string $root, Test $testInstance)
22
            {
23 1
                parent::__construct($root);
24 1
                $this->testInstance = $testInstance;
0 ignored issues
show
Bug introduced by
The property testInstance does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25 1
            }
26 21
            public function dispatch(string $job, array $params = [], string $service = null)
27
            {
28 21
                if (array_key_exists($job, $this->testInstance->mocks)) {
29 4
                    $mocks = $this->testInstance->mocks[$job];
30 4
                    $valid = null;
31 4
                    foreach ($mocks as $mock) {
32 4
                        if ($mock->params == $params || (!$mock->params && !$valid)) {
33 4
                            $valid = $mock;
34
                        }
35
                    }
36 4
                    if ($valid) {
37 4
                        $result = $valid->result;
38 4
                        if (is_callable($result)) {
39 1
                            $result = $result($params);
40
                        }
41 4
                        $valid->calls++;
42 4
                        return $this->get(Converter::class)->toObject($result);
43
                    }
44
                }
45 21
                if (!$this->get(Runner::class)->hasJob($job)) {
46
                    throw new Exception("Remote calls are disabled for tests");
47
                }
48 21
                $converter = $this->get(Converter::class);
49 21
                $global = $this->testInstance->params ?: [];
50 21
                if (!is_array($global)) {
51
                    $global = get_object_vars($converter->toObject($this->testInstance->params));
52
                }
53 21
                return parent::dispatch($job, array_merge($params, $global), $service);
54
            }
55
        };
56 1
    }
57
58 21
    public function setup()
59
    {
60 21
        $this->dispatch('tarantool.migrate');
61 21
    }
62
63 14
    public function tearDown()
64
    {
65 14
        $this->dispatch('tarantool.clear');
66 14
    }
67
68
    public $mocks = [];
69 4
    public function mock(string $job, array $params = [])
70
    {
71 4
        if (!array_key_exists($job, $this->mocks)) {
72 4
            $this->mocks[$job] = [];
73
        }
74
75
        $mock = new class {
76
77
            public $params;
78
            public $result;
79
            public $calls = 0;
80
81
            public function withParams($params)
82
            {
83
                $this->params = $params;
84
                return $this;
85
            }
86
87 4
            public function handler($result)
88
            {
89 4
                $this->result = $result;
90 4
                return $this;
91
            }
92
93
            public function willDo($result)
94
            {
95
                return $this->handler($result);
96
            }
97
98 4
            public function willReturn($result)
99
            {
100 4
                return $this->handler($result);
101
            }
102
        };
103
104 4
        if (count($params)) {
105 1
            $mock->params = $params;
106
        }
107
108 4
        $this->mocks[$job][] = $mock;
109
110 4
        return $mock;
111
    }
112
}
113