|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\OctaneTestbench\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Laravel\Octane\Exceptions\TaskExceptionResult; |
|
7
|
|
|
use Laravel\Octane\Exceptions\TaskTimeoutException; |
|
8
|
|
|
use Throwable; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* The trait to provide concurrency support. |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
trait ProvidesConcurrencySupport |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Expect Octane to run tasks concurrently |
|
18
|
|
|
* |
|
19
|
|
|
* @param callable|null $callback |
|
20
|
|
|
* @return static |
|
21
|
|
|
*/ |
|
22
|
2 |
|
public function expectsConcurrency(callable $callback = null): static |
|
23
|
|
|
{ |
|
24
|
2 |
|
return $this->mocks('octane', function ($mock) use ($callback) { |
|
|
|
|
|
|
25
|
2 |
|
$mock->shouldReceive('concurrently')->andReturnUsing($callback ?: function (array $tasks) { |
|
26
|
1 |
|
array_walk($tasks, fn (&$task) => $task = $task()); |
|
27
|
1 |
|
return $tasks; |
|
28
|
2 |
|
}); |
|
29
|
2 |
|
}); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Expect Octane to run tasks concurrently |
|
34
|
|
|
* |
|
35
|
|
|
* @param array ...$results |
|
36
|
|
|
* @return static |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function expectsConcurrencyResults(array ...$results): static |
|
39
|
|
|
{ |
|
40
|
1 |
|
return $this->mocks('octane', function ($mock) use ($results) { |
|
41
|
1 |
|
$mock->shouldReceive('concurrently')->andReturn(...$results); |
|
42
|
1 |
|
}); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Expect Octane to throw an exception when running tasks concurrently |
|
47
|
|
|
* |
|
48
|
|
|
* @param Throwable|null $e |
|
49
|
|
|
* @return static |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function expectsConcurrencyException(Throwable $e = null): static |
|
52
|
|
|
{ |
|
53
|
1 |
|
$exception = TaskExceptionResult::from($e ?: new Exception())->getOriginal(); |
|
54
|
|
|
|
|
55
|
1 |
|
return $this->mocks('octane', function ($mock) use ($exception) { |
|
56
|
1 |
|
$mock->shouldReceive('concurrently')->andThrow($exception); |
|
57
|
1 |
|
}); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Expect Octane to timeout when running tasks concurrently |
|
62
|
|
|
* |
|
63
|
|
|
* @param int $milliseconds |
|
64
|
|
|
* @return static |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function expectsConcurrencyTimeout(int $milliseconds = 100): static |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->mocks('octane', function ($mock) use ($milliseconds) { |
|
69
|
1 |
|
$mock->shouldReceive('concurrently')->andThrow(TaskTimeoutException::after($milliseconds)); |
|
70
|
1 |
|
}); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|