|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Framework\Jobs; |
|
13
|
|
|
|
|
14
|
|
|
use Mockery as m; |
|
15
|
|
|
use Spiral\Boot\DirectoriesInterface; |
|
16
|
|
|
use Spiral\Files\FilesInterface; |
|
17
|
|
|
use Spiral\Jobs\JobDispatcher; |
|
18
|
|
|
use Spiral\RoadRunner\Worker; |
|
19
|
|
|
use Spiral\App\Job\ErrorJob; |
|
20
|
|
|
use Spiral\App\Job\TestJob; |
|
21
|
|
|
use Spiral\Tests\Framework\ConsoleTest; |
|
22
|
|
|
|
|
23
|
|
|
class DispatcherTest extends ConsoleTest |
|
24
|
|
|
{ |
|
25
|
|
|
public function testCanServe(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->assertFalse($this->app->get(JobDispatcher::class)->canServe()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testCanServe2(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->app->getEnvironment()->set('RR_JOBS', true); |
|
33
|
|
|
$this->assertTrue($this->app->get(JobDispatcher::class)->canServe()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testServe(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$w = m::mock(Worker::class); |
|
39
|
|
|
|
|
40
|
|
|
$this->app->getEnvironment()->set('RR_JOBS', true); |
|
41
|
|
|
$this->app->getContainer()->bind(Worker::class, $w); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
$this->assertNull($this->app->getEnvironment()->get('FIRED')); |
|
44
|
|
|
|
|
45
|
|
|
$w->shouldReceive('receive')->once()->with( |
|
46
|
|
|
\Mockery::on(function (&$context) { |
|
47
|
|
|
$context = $this->arrayToContextString(TestJob::class); |
|
48
|
|
|
|
|
49
|
|
|
return true; |
|
50
|
|
|
}) |
|
51
|
|
|
)->andReturn('[]'); |
|
52
|
|
|
|
|
53
|
|
|
$w->shouldReceive('send')->once()->andReturn(true); |
|
54
|
|
|
|
|
55
|
|
|
// one command only |
|
56
|
|
|
$w->shouldReceive('receive')->once()->andReturn(null); |
|
57
|
|
|
|
|
58
|
|
|
$this->app->get(JobDispatcher::class)->serve(); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertTrue($this->app->getEnvironment()->get('FIRED')); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testError(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$w = m::mock(Worker::class); |
|
66
|
|
|
|
|
67
|
|
|
$this->app->getEnvironment()->set('RR_JOBS', true); |
|
68
|
|
|
$this->app->getContainer()->bind(Worker::class, $w); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$w->shouldReceive('receive')->once()->with( |
|
71
|
|
|
\Mockery::on(function (&$context) { |
|
72
|
|
|
$context = $this->arrayToContextString(ErrorJob::class); |
|
73
|
|
|
|
|
74
|
|
|
return true; |
|
75
|
|
|
}) |
|
76
|
|
|
)->andReturn('[]'); |
|
77
|
|
|
|
|
78
|
|
|
$w->shouldReceive('error')->once()->andReturn(true); |
|
79
|
|
|
|
|
80
|
|
|
// one command only |
|
81
|
|
|
$w->shouldReceive('receive')->once()->andReturn(null); |
|
82
|
|
|
|
|
83
|
|
|
$files = $this->app->get(FilesInterface::class)->getFiles( |
|
84
|
|
|
$this->app->get(DirectoriesInterface::class)->get('runtime') . '/snapshots/' |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertCount(0, $files); |
|
88
|
|
|
|
|
89
|
|
|
$this->app->get(JobDispatcher::class)->serve(); |
|
90
|
|
|
|
|
91
|
|
|
$files = $this->app->get(FilesInterface::class)->getFiles( |
|
92
|
|
|
$this->app->get(DirectoriesInterface::class)->get('runtime') . '/snapshots/' |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertCount(1, $files); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $class |
|
100
|
|
|
* @param int $id |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function arrayToContextString(string $class, int $id = 1): string |
|
104
|
|
|
{ |
|
105
|
|
|
$signature = \explode('\\', $class); |
|
106
|
|
|
$class = \array_pop($signature); |
|
107
|
|
|
|
|
108
|
|
|
return \json_encode([ |
|
109
|
|
|
'id' => (string)$id, |
|
110
|
|
|
'job' => \strtolower(\implode('.', $signature)) . '.' . $class |
|
111
|
|
|
]); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|