Completed
Push — master ( 74f8b0...5a393e )
by Dmitry
08:43 queued 06:34
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 94.74%

Importance

Changes 0
Metric Value
wmc 10
cbo 1
dl 0
loc 30
ccs 18
cts 19
cp 0.9474
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 1
                            $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, array_merge($params, $this->testInstance->params), $service);
48
            }
49
        };
50 1
    }
51
52 1
    public function setup()
53
    {
54 1
        $this->dispatch('tarantool.migrate');
55 1
    }
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