|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Basis; |
|
4
|
|
|
|
|
5
|
|
|
use Basis\Runner; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
abstract class Test extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
use Toolkit; |
|
12
|
|
|
|
|
13
|
|
|
public $params = []; |
|
14
|
|
|
|
|
15
|
1 |
|
public function __construct() |
|
16
|
|
|
{ |
|
17
|
1 |
|
parent::__construct(); |
|
18
|
|
|
|
|
19
|
|
|
$this->app = new class(getcwd(), $this) extends Application { |
|
20
|
1 |
|
public function __construct(string $root, Test $testInstance) |
|
21
|
|
|
{ |
|
22
|
1 |
|
parent::__construct($root); |
|
23
|
1 |
|
$this->testInstance = $testInstance; |
|
|
|
|
|
|
24
|
1 |
|
} |
|
25
|
1 |
|
public function dispatch(string $job, array $params = [], string $service = null) |
|
26
|
|
|
{ |
|
27
|
1 |
|
if (array_key_exists($job, $this->testInstance->mocks)) { |
|
28
|
1 |
|
$mocks = $this->testInstance->mocks[$job]; |
|
29
|
1 |
|
$valid = null; |
|
30
|
1 |
|
foreach ($mocks as $mock) { |
|
31
|
1 |
|
if ($mock->params == $params || (!$mock->params && !$valid)) { |
|
32
|
1 |
|
$valid = $mock; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
1 |
|
if ($valid) { |
|
36
|
1 |
|
return is_callable($valid->result) ? ($valid->result)() : $valid->result; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
1 |
|
if (!$this->get(Runner::class)->hasJob($job)) { |
|
40
|
|
|
throw new Exception("Remote calls are disabled for tests"); |
|
41
|
|
|
} |
|
42
|
1 |
|
return parent::dispatch($job, $params, $service); |
|
43
|
|
|
} |
|
44
|
|
|
}; |
|
45
|
1 |
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
public function setup() |
|
48
|
|
|
{ |
|
49
|
1 |
|
$this->dispatch('tarantool.migrate'); |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function dispatch(string $job, array $params = []) |
|
53
|
|
|
{ |
|
54
|
1 |
|
return $this->app->dispatch($job, array_merge($params, $this->params)); |
|
55
|
|
|
} |
|
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
|
|
|
|
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: