Passed
Push — master ( b0030b...d4bf1b )
by Jakub
09:20
created

Job::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 10
Bugs 0 Features 1
Metric Value
eloc 10
c 10
b 0
f 1
dl 0
loc 14
rs 9.9332
ccs 9
cts 10
cp 0.9
cc 3
nc 3
nop 0
crap 3.009
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MyTester;
6
7
/**
8
 * One job of the test suite
9
 *
10
 * @author Jakub Konečný
11
 * @property-read string $name
12
 * @property-read callable $callback
13
 * @property-read array $params
14
 * @property-read bool|string $skip
15
 * @property-read string $result
16
 * @property-read string $output @internal
17
 */
18
final class Job
19
{
20
    use \Nette\SmartObject;
21
22
    public const RESULT_PASSED = "passed";
23
    public const RESULT_SKIPPED = "skipped";
24
    public const RESULT_FAILED = "failed";
25
26
    protected string $name;
27
    /** @var callable Task */
28
    protected $callback;
29
    protected array $params = [];
30
    protected bool|string $skip;
31
    protected string $result = self::RESULT_PASSED;
32
    protected string $output = "";
33
34 1
    public function __construct(
35
        string $name,
36
        callable $callback,
37
        array $params = [],
38
        bool|string $skip = false
39
    ) {
40 1
        $this->name = $name;
41 1
        $this->callback = $callback;
42 1
        $this->params = $params;
43 1
        $this->skip = $skip;
44 1
    }
45
46 1
    protected function getName(): string
47
    {
48 1
        return $this->name;
49
    }
50
51
    protected function getCallback(): callable
52
    {
53
        return $this->callback;
54
    }
55
56
    protected function getParams(): array
57
    {
58
        return $this->params;
59
    }
60
61 1
    protected function getSkip(): bool|string
62
    {
63 1
        return $this->skip;
64
    }
65
66 1
    protected function getResult(): string
67
    {
68 1
        return $this->result;
69
    }
70
71
    protected function getOutput(): string
72
    {
73
        return $this->output;
74
    }
75
76
    /**
77
     * Executes the task
78
     */
79 1
    public function execute(): void
80
    {
81 1
        if ($this->skip) {
82 1
            $this->result = static::RESULT_SKIPPED;
83
        } else {
84 1
            ob_start();
85 1
            call_user_func_array($this->callback, $this->params);
86
            /** @var string $output */
87 1
            $output = ob_get_clean();
88 1
            $failed = str_contains($output, " failed. ");
89 1
            if ($failed) {
90
                $this->result = static::RESULT_FAILED;
91
            }
92 1
            $this->output = $output;
93
        }
94 1
    }
95
}
96