Completed
Push — master ( 7e893d...300d86 )
by Dmitry
02:36
created

anonymous//src/Test.php$0   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 10
cbo 1
dl 0
loc 30
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0
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 1
    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 1
            public function dispatch(string $job, array $params = [], string $service = null)
27
            {
28 1
                if (array_key_exists($job, $this->testInstance->mocks)) {
29 1
                    $mocks = $this->testInstance->mocks[$job];
30 1
                    $valid = null;
31 1
                    foreach ($mocks as $mock) {
32 1
                        if ($mock->params == $params || (!$mock->params && !$valid)) {
33 1
                            $valid = $mock;
34
                        }
35
                    }
36 1
                    if ($valid) {
37 1
                        $result = $valid->result;
38 1
                        if (is_callable($result)) {
39
                            $result = $result();
40
                        }
41 1
                        return $this->get(Converter::class)->toObject($result);
42
                    }
43
                }
44 1
                if (!$this->get(Runner::class)->hasJob($job)) {
45
                    throw new Exception("Remote calls are disabled for tests");
46
                }
47 1
                return parent::dispatch($job, $params, $service);
48
            }
49
        };
50 1
    }
51
52 1
    public function setup()
53
    {
54 1
        $this->dispatch('tarantool.migrate');
55 1
    }
56
57 1
    public function dispatch(string $job, array $params = [])
58
    {
59 1
        return $this->app->dispatch($job, array_merge($params, $this->params));
60
    }
61
62
    public function tearDown()
63
    {
64
        $this->dispatch('tarantool.clear');
65
    }
66
67
    public $mocks = [];
68 1
    public function mock(string $job, array $params = [])
69
    {
70 1
        if (!array_key_exists($job, $this->mocks)) {
71 1
            $this->mocks[$job] = [];
72
        }
73
74
        $mock = new class {
75
            public $params;
76
            public $result;
77
            public function withParams($params)
78
            {
79
                $this->params = $params;
80
                return $this;
81
            }
82 1
            public function willReturn($result)
83
            {
84 1
                $this->result = $result;
85 1
                return $this;
86
            }
87
        };
88
89 1
        if (count($params)) {
90 1
            $mock->params = $params;
91
        }
92
93 1
        $this->mocks[$job][] = $mock;
94
95 1
        return $mock;
96
    }
97
}
98