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