expectsConcurrencyException()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
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) {
0 ignored issues
show
Bug introduced by
It seems like mocks() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        return $this->/** @scrutinizer ignore-call */ mocks('octane', function ($mock) use ($callback) {
Loading history...
25 2
            $mock->shouldReceive('concurrently')->andReturnUsing($callback ?: function (array $tasks) {
26 1
                array_walk($tasks, fn (&$task) => $task = $task());
27 1
                return $tasks;
28
            });
29
        });
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
        });
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
        });
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
        });
71
    }
72
}
73