Completed
Push — master ( c85e73...41284f )
by Dmitry
02:55
created

Test.php$0 ➔ dispatch()   F

Complexity

Conditions 12

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 12.1242

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 19
cts 21
cp 0.9048
rs 2.7855
c 0
b 0
f 0
cc 12
crap 12.1242

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