Completed
Push — master ( 2d3d31...7e893d )
by Dmitry
03:49
created

Test::__construct()   D

Complexity

Conditions 9
Paths 1

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9.0117

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 18
cts 19
cp 0.9474
rs 4.909
c 0
b 0
f 0
cc 9
crap 9.0117
eloc 18
nc 1
nop 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Test.php$0 ➔ __construct() 0 5 1
C Test.php$0 ➔ dispatch() 0 19 9
1
<?php
2
3
namespace Basis;
4
5
use Basis\Runner;
6
use Exception;
7
use PHPUnit\Framework\TestCase;
8
9
abstract class Test extends TestCase
10
{
11
    use Toolkit;
12
13
    public $params = [];
14
15 1
    public function __construct()
16
    {
17 1
        parent::__construct();
18
19
        $this->app = new class(getcwd(), $this) extends Application {
20 1
            public function __construct(string $root, Test $testInstance)
21
            {
22 1
                parent::__construct($root);
23 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...
24 1
            }
25 1
            public function dispatch(string $job, array $params = [], string $service = null)
26
            {
27 1
                if (array_key_exists($job, $this->testInstance->mocks)) {
28 1
                    $mocks = $this->testInstance->mocks[$job];
29 1
                    $valid = null;
30 1
                    foreach ($mocks as $mock) {
31 1
                        if ($mock->params == $params || (!$mock->params && !$valid)) {
32 1
                            $valid = $mock;
33
                        }
34
                    }
35 1
                    if ($valid) {
36 1
                        return is_callable($valid->result) ? ($valid->result)() : $valid->result;
37
                    }
38
                }
39 1
                if (!$this->get(Runner::class)->hasJob($job)) {
40
                    throw new Exception("Remote calls are disabled for tests");
41
                }
42 1
                return parent::dispatch($job, $params, $service);
43
            }
44
        };
45 1
    }
46
47 1
    public function setup()
48
    {
49 1
        $this->dispatch('tarantool.migrate');
50 1
    }
51
52 1
    public function dispatch(string $job, array $params = [])
53
    {
54 1
        return $this->app->dispatch($job, array_merge($params, $this->params));
55
    }
56
57
    public function tearDown()
58
    {
59
        $this->dispatch('tarantool.clear');
60
    }
61
62
    public $mocks = [];
63 1
    public function mock(string $job, array $params = [])
64
    {
65 1
        if (!array_key_exists($job, $this->mocks)) {
66 1
            $this->mocks[$job] = [];
67
        }
68
69
        $mock = new class {
70
            public $params;
71
            public $result;
72
            public function withParams($params)
73
            {
74
                $this->params = $params;
75
                return $this;
76
            }
77 1
            public function willReturn($result)
78
            {
79 1
                $this->result = $result;
80 1
                return $this;
81
            }
82
        };
83
84 1
        if (count($params)) {
85 1
            $mock->params = $params;
86
        }
87
88 1
        $this->mocks[$job][] = $mock;
89
90 1
        return $mock;
91
    }
92
}
93