Completed
Push — master ( 43fa8f...18b93e )
by Dmitry
02:38
created

Test   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 79
ccs 30
cts 36
cp 0.8333
rs 10
c 0
b 0
f 0

9 Methods

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