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
|
1 |
|
public function __construct() |
14
|
|
|
{ |
15
|
1 |
|
parent::__construct(); |
16
|
|
|
|
17
|
|
|
$this->app = new class(getcwd(), $this) extends Application { |
18
|
1 |
|
public function __construct(string $root, Test $testInstance) |
19
|
|
|
{ |
20
|
1 |
|
parent::__construct($root); |
21
|
1 |
|
$this->testInstance = $testInstance; |
|
|
|
|
22
|
1 |
|
} |
23
|
1 |
|
public function dispatch(string $job, array $params = [], string $service = null) |
24
|
|
|
{ |
25
|
1 |
|
if (array_key_exists($job, $this->testInstance->mocks)) { |
26
|
1 |
|
$mocks = $this->testInstance->mocks[$job]; |
27
|
1 |
|
$valid = null; |
28
|
1 |
|
foreach ($mocks as $mock) { |
29
|
1 |
|
if ($mock->params == $params || (!$mock->params && !$valid)) { |
30
|
1 |
|
$valid = $mock; |
31
|
|
|
} |
32
|
|
|
} |
33
|
1 |
|
if ($valid) { |
34
|
1 |
|
return is_callable($valid->result) ? ($valid->result)() : $valid->result; |
35
|
|
|
} |
36
|
|
|
} |
37
|
1 |
|
return parent::dispatch($job, $params, $service); |
38
|
|
|
} |
39
|
|
|
}; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
1 |
|
public function setup() |
43
|
|
|
{ |
44
|
1 |
|
$this->dispatch('tarantool.migrate'); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
1 |
|
public function dispatch(string $job, array $params = []) |
48
|
|
|
{ |
49
|
1 |
|
return $this->app->dispatch($job, array_merge($params, $this->params)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function tearDown() |
53
|
|
|
{ |
54
|
|
|
$this->dispatch('tarantool.clear'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public $mocks = []; |
58
|
1 |
|
public function mock(string $job, array $params = []) |
59
|
|
|
{ |
60
|
1 |
|
if (!array_key_exists($job, $this->mocks)) { |
61
|
1 |
|
$this->mocks[$job] = []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$mock = new class { |
65
|
|
|
public $params; |
66
|
|
|
public $result; |
67
|
|
|
public function withParams($params) |
68
|
|
|
{ |
69
|
|
|
$this->params = $params; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
1 |
|
public function willReturn($result) |
73
|
|
|
{ |
74
|
1 |
|
$this->result = $result; |
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
}; |
78
|
|
|
|
79
|
1 |
|
if (count($params)) { |
80
|
1 |
|
$mock->params = $params; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$this->mocks[$job][] = $mock; |
84
|
|
|
|
85
|
1 |
|
return $mock; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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: