1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis\Test; |
4
|
|
|
|
5
|
|
|
use Basis\Application as BaseApplication; |
6
|
|
|
use Basis\Cache; |
7
|
|
|
use Basis\Context; |
8
|
|
|
use Basis\Converter; |
9
|
|
|
use Basis\Runner; |
10
|
|
|
use Basis\Storage as BaseStorage; |
11
|
|
|
use Basis\Test; |
12
|
|
|
use Exception; |
13
|
|
|
|
14
|
|
|
class Application extends BaseApplication |
15
|
|
|
{ |
16
|
|
|
private $test; |
17
|
|
|
|
18
|
|
|
public function __construct(Test $test) |
19
|
|
|
{ |
20
|
|
|
parent::__construct(getcwd()); |
21
|
|
|
$this->share(BaseStorage::class, $this->get(Storage::class)); |
22
|
|
|
$this->test = $test; |
23
|
|
|
} |
24
|
|
|
|
25
|
56 |
|
public function dispatch(string $job, array $params = [], string $service = null) |
26
|
|
|
{ |
27
|
56 |
|
if (array_key_exists('context', $this->test->params)) { |
28
|
|
|
$this->get(Context::class)->apply($this->test->params['context']); |
29
|
|
|
} |
30
|
56 |
|
if (array_key_exists($job, $this->test->mockInstances)) { |
31
|
8 |
|
$mocks = $this->test->mockInstances[$job]; |
32
|
8 |
|
$valid = null; |
33
|
8 |
|
foreach ($mocks as $mock) { |
34
|
8 |
|
if ($mock->params == $params || (!$mock->params && !$valid)) { |
35
|
8 |
|
$valid = $mock; |
36
|
|
|
} |
37
|
|
|
} |
38
|
8 |
|
if ($valid) { |
39
|
8 |
|
return $this->get(Cache::class) |
40
|
|
|
->wrap([$job, $params, $service], function () use ($valid, $params) { |
41
|
8 |
|
$result = $valid->result; |
42
|
8 |
|
if (is_callable($result)) { |
43
|
5 |
|
$result = $result($params); |
44
|
|
|
} |
45
|
8 |
|
$valid->calls++; |
46
|
8 |
|
return $this->get(Converter::class)->toObject($result); |
47
|
8 |
|
}); |
48
|
|
|
} |
49
|
|
|
} |
50
|
56 |
|
if ($this->test->disableRemote) { |
51
|
54 |
|
if (!$this->get(Runner::class)->hasJob($job)) { |
52
|
1 |
|
throw new Exception("Remote calls ($job) are disabled for tests"); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
56 |
|
$converter = $this->get(Converter::class); |
57
|
|
|
|
58
|
56 |
|
$global = $this->test->params ?: []; |
59
|
56 |
|
$global = $converter->toObject($global); |
60
|
56 |
|
if (is_object($global)) { |
61
|
2 |
|
$global = get_object_vars($global); |
62
|
|
|
} |
63
|
|
|
|
64
|
56 |
|
return parent::dispatch($job, array_merge($params, $global), $service); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|