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
|
|
|
public $disableRemote = true; |
16
|
|
|
|
17
|
26 |
|
public function __construct() |
18
|
|
|
{ |
19
|
1 |
|
parent::__construct(); |
20
|
|
|
|
21
|
|
|
$this->app = new class(getcwd(), $this) extends Application { |
22
|
1 |
|
public function __construct(string $root, Test $testInstance) |
23
|
|
|
{ |
24
|
1 |
|
parent::__construct($root); |
25
|
1 |
|
$this->testInstance = $testInstance; |
|
|
|
|
26
|
1 |
|
} |
27
|
26 |
|
public function dispatch(string $job, array $params = [], string $service = null) |
28
|
|
|
{ |
29
|
26 |
|
if (array_key_exists($job, $this->testInstance->mocks)) { |
30
|
5 |
|
$mocks = $this->testInstance->mocks[$job]; |
31
|
5 |
|
$valid = null; |
32
|
5 |
|
foreach ($mocks as $mock) { |
33
|
5 |
|
if ($mock->params == $params || (!$mock->params && !$valid)) { |
34
|
5 |
|
$valid = $mock; |
35
|
|
|
} |
36
|
|
|
} |
37
|
5 |
|
if ($valid) { |
38
|
5 |
|
$result = $valid->result; |
39
|
5 |
|
if (is_callable($result)) { |
40
|
2 |
|
$result = $result($params); |
41
|
|
|
} |
42
|
5 |
|
$valid->calls++; |
43
|
5 |
|
return $this->get(Converter::class)->toObject($result); |
44
|
|
|
} |
45
|
|
|
} |
46
|
26 |
|
if ($this->testInstance->disableRemote) { |
47
|
25 |
|
if (!$this->get(Runner::class)->hasJob($job)) { |
48
|
|
|
throw new Exception("Remote calls are disabled for tests"); |
49
|
|
|
} |
50
|
|
|
} |
51
|
26 |
|
$converter = $this->get(Converter::class); |
52
|
26 |
|
$global = $this->testInstance->params ?: []; |
53
|
26 |
|
if (!is_array($global)) { |
54
|
|
|
$global = get_object_vars($converter->toObject($this->testInstance->params)); |
55
|
|
|
} |
56
|
26 |
|
return parent::dispatch($job, array_merge($params, $global), $service); |
57
|
|
|
} |
58
|
|
|
}; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
26 |
|
public function setup() |
62
|
|
|
{ |
63
|
26 |
|
$this->dispatch('tarantool.migrate'); |
64
|
26 |
|
} |
65
|
|
|
|
66
|
19 |
|
public function tearDown() |
67
|
|
|
{ |
68
|
19 |
|
$this->dispatch('tarantool.clear'); |
69
|
19 |
|
} |
70
|
|
|
|
71
|
|
|
public $mocks = []; |
72
|
5 |
|
public function mock(string $job, array $params = []) |
73
|
|
|
{ |
74
|
5 |
|
if (!array_key_exists($job, $this->mocks)) { |
75
|
5 |
|
$this->mocks[$job] = []; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$mock = new class { |
79
|
|
|
|
80
|
|
|
public $params; |
81
|
|
|
public $result; |
82
|
|
|
public $calls = 0; |
83
|
|
|
|
84
|
|
|
public function withParams($params) |
85
|
|
|
{ |
86
|
|
|
$this->params = $params; |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
5 |
|
public function handler($result) |
91
|
|
|
{ |
92
|
5 |
|
$this->result = $result; |
93
|
5 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function willDo($result) |
97
|
|
|
{ |
98
|
1 |
|
return $this->handler($result); |
99
|
|
|
} |
100
|
|
|
|
101
|
5 |
|
public function willReturn($result) |
102
|
|
|
{ |
103
|
5 |
|
return $this->handler($result); |
104
|
|
|
} |
105
|
|
|
}; |
106
|
|
|
|
107
|
5 |
|
if (count($params)) { |
108
|
1 |
|
$mock->params = $params; |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
$this->mocks[$job][] = $mock; |
112
|
|
|
|
113
|
5 |
|
return $mock; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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: