Completed
Push — master ( d1ea0c...72efe0 )
by Dmitry
03:12
created

Test::setup()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 5.3846
cc 8
crap 8
eloc 16
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 16 8
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 setup()
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
        $this->dispatch('tarantool.migrate');
39 1
    }
40
41
    public function tearDown()
42
    {
43
        $this->dispatch('tarantool.clear');
44
    }
45
46
    public $mocks = [];
47 1
    public function mock(string $job, array $params = [])
48
    {
49 1
        if (!array_key_exists($job, $this->mocks)) {
50 1
            $this->mocks[$job] = [];
51
        }
52
53
        $mock = new class {
54
            public $params;
55
            public $result;
56
            public function withParams($params)
57
            {
58
                $this->params = $params;
59
                return $this;
60
            }
61 1
            public function willReturn($result)
62
            {
63 1
                $this->result = $result;
64 1
                return $this;
65
            }
66
        };
67
68 1
        if (count($params)) {
69 1
            $mock->params = $params;
70
        }
71
72 1
        $this->mocks[$job][] = $mock;
73
74 1
        return $mock;
75
    }
76
}
77