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; |
|
|
|
|
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
|
1 |
|
public function dispatch(string $job, array $params = []) |
42
|
|
|
{ |
43
|
1 |
|
return $this->app->dispatch($job, array_merge($params, $this->params)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function tearDown() |
47
|
|
|
{ |
48
|
|
|
$this->dispatch('tarantool.clear'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public $mocks = []; |
52
|
1 |
|
public function mock(string $job, array $params = []) |
53
|
|
|
{ |
54
|
1 |
|
if (!array_key_exists($job, $this->mocks)) { |
55
|
1 |
|
$this->mocks[$job] = []; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$mock = new class { |
59
|
|
|
public $params; |
60
|
|
|
public $result; |
61
|
|
|
public function withParams($params) |
62
|
|
|
{ |
63
|
|
|
$this->params = $params; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
1 |
|
public function willReturn($result) |
67
|
|
|
{ |
68
|
1 |
|
$this->result = $result; |
69
|
1 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
}; |
72
|
|
|
|
73
|
1 |
|
if (count($params)) { |
74
|
1 |
|
$mock->params = $params; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$this->mocks[$job][] = $mock; |
78
|
|
|
|
79
|
1 |
|
return $mock; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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: