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
|
21 |
|
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; |
|
|
|
|
25
|
1 |
|
} |
26
|
21 |
|
public function dispatch(string $job, array $params = [], string $service = null) |
27
|
|
|
{ |
28
|
21 |
|
if (array_key_exists($job, $this->testInstance->mocks)) { |
29
|
4 |
|
$mocks = $this->testInstance->mocks[$job]; |
30
|
4 |
|
$valid = null; |
31
|
4 |
|
foreach ($mocks as $mock) { |
32
|
4 |
|
if ($mock->params == $params || (!$mock->params && !$valid)) { |
33
|
4 |
|
$valid = $mock; |
34
|
|
|
} |
35
|
|
|
} |
36
|
4 |
|
if ($valid) { |
37
|
4 |
|
$result = $valid->result; |
38
|
4 |
|
if (is_callable($result)) { |
39
|
1 |
|
$result = $result($params); |
40
|
|
|
} |
41
|
4 |
|
$valid->calls++; |
42
|
4 |
|
return $this->get(Converter::class)->toObject($result); |
43
|
|
|
} |
44
|
|
|
} |
45
|
21 |
|
if (!$this->get(Runner::class)->hasJob($job)) { |
46
|
|
|
throw new Exception("Remote calls are disabled for tests"); |
47
|
|
|
} |
48
|
21 |
|
$converter = $this->get(Converter::class); |
49
|
21 |
|
$global = $this->testInstance->params ?: []; |
50
|
21 |
|
if (!is_array($global)) { |
51
|
|
|
$global = get_object_vars($converter->toObject($this->testInstance->params)); |
52
|
|
|
} |
53
|
21 |
|
return parent::dispatch($job, array_merge($params, $global), $service); |
54
|
|
|
} |
55
|
|
|
}; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
21 |
|
public function setup() |
59
|
|
|
{ |
60
|
21 |
|
$this->dispatch('tarantool.migrate'); |
61
|
21 |
|
} |
62
|
|
|
|
63
|
14 |
|
public function tearDown() |
64
|
|
|
{ |
65
|
14 |
|
$this->dispatch('tarantool.clear'); |
66
|
14 |
|
} |
67
|
|
|
|
68
|
|
|
public $mocks = []; |
69
|
4 |
|
public function mock(string $job, array $params = []) |
70
|
|
|
{ |
71
|
4 |
|
if (!array_key_exists($job, $this->mocks)) { |
72
|
4 |
|
$this->mocks[$job] = []; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$mock = new class { |
76
|
|
|
|
77
|
|
|
public $params; |
78
|
|
|
public $result; |
79
|
|
|
public $calls = 0; |
80
|
|
|
|
81
|
|
|
public function withParams($params) |
82
|
|
|
{ |
83
|
|
|
$this->params = $params; |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
public function handler($result) |
88
|
|
|
{ |
89
|
4 |
|
$this->result = $result; |
90
|
4 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function willDo($result) |
94
|
|
|
{ |
95
|
|
|
return $this->handler($result); |
96
|
|
|
} |
97
|
|
|
|
98
|
4 |
|
public function willReturn($result) |
99
|
|
|
{ |
100
|
4 |
|
return $this->handler($result); |
101
|
|
|
} |
102
|
|
|
}; |
103
|
|
|
|
104
|
4 |
|
if (count($params)) { |
105
|
1 |
|
$mock->params = $params; |
106
|
|
|
} |
107
|
|
|
|
108
|
4 |
|
$this->mocks[$job][] = $mock; |
109
|
|
|
|
110
|
4 |
|
return $mock; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: