forked from laravel/horizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegrationTest.php
144 lines (129 loc) · 2.99 KB
/
IntegrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Laravel\Horizon\Tests;
use Illuminate\Queue\WorkerOptions;
use Illuminate\Support\Facades\Redis;
use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\Contracts\TagRepository;
use Laravel\Horizon\Horizon;
use Laravel\Horizon\SupervisorCommandString;
use Laravel\Horizon\WorkerCommandString;
use Orchestra\Testbench\TestCase;
abstract class IntegrationTest extends TestCase
{
/**
* Setup the test case.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
Redis::flushall();
}
/**
* Tear down the test case.
*
* @return void
*/
protected function tearDown(): void
{
parent::tearDown();
Redis::flushall();
WorkerCommandString::reset();
SupervisorCommandString::reset();
Horizon::$authUsing = null;
}
/**
* Run the given assertion callback with a retry loop.
*
* @param \Closure $callback
* @return void
*/
public function wait($callback)
{
retry(10, $callback, 1000);
}
/**
* Get the total number of recent jobs.
*
* @return int
*/
protected function recentJobs()
{
return app(JobRepository::class)->totalRecent();
}
/**
* Get the total number of monitored jobs for a given tag.
*
* @param string $tag
* @return int
*/
protected function monitoredJobs($tag)
{
return app(TagRepository::class)->count($tag);
}
/**
* Get the total number of failed jobs.
*
* @return int
*/
protected function failedJobs()
{
return app(JobRepository::class)->totalFailed();
}
/**
* Run the next job on the queue.
*
* @param int $times
* @return void
*/
protected function work($times = 1)
{
for ($i = 0; $i < $times; $i++) {
$this->worker()->runNextJob(
'redis', 'default', $this->workerOptions()
);
}
}
/**
* Get the queue worker instance.
*
* @return \Illuminate\Queue\Worker
*/
protected function worker()
{
return app('queue.worker');
}
/**
* Get the options for the worker.
*
* @return \Illuminate\Queue\WorkerOptions
*/
protected function workerOptions()
{
return tap(new WorkerOptions, function ($options) {
$options->sleep = 0;
$options->maxTries = 1;
});
}
/**
* Get the service providers for the package.
*
* @param \Illuminate\Foundation\Application $app
* @return array
*/
protected function getPackageProviders($app)
{
return ['Laravel\Horizon\HorizonServiceProvider'];
}
/**
* Configure the environment.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('queue.default', 'redis');
}
}