Passed
Push — master ( 983636...0512e4 )
by Jakub
01:45
created

Job::getResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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
 * @method void onAfterExecute()
18
 */
19
final class Job
20
{
21
    use \Nette\SmartObject;
22
23
    public const RESULT_PASSED = "passed";
24
    public const RESULT_SKIPPED = "skipped";
25
    public const RESULT_FAILED = "failed";
26
    public const RESULT_PASSED_WITH_WARNINGS = "warning";
27
28
    protected string $name;
29
    /** @var callable Task */
30
    protected $callback;
31
    protected array $params = [];
32
    protected bool|string $skip;
33
    protected string $result = self::RESULT_PASSED;
34
    protected string $output = "";
35
    /** @var callable[] */
36
    public array $onAfterExecute = [];
37
38 1
    public function __construct(
39
        string $name,
40
        callable $callback,
41
        array $params = [],
42
        bool|string $skip = false,
43
        array $onAfterExecute = []
44
    ) {
45 1
        $this->name = $name;
46 1
        $this->callback = $callback;
47 1
        $this->params = $params;
48 1
        $this->skip = $skip;
49 1
        $this->onAfterExecute = $onAfterExecute;
50 1
    }
51
52 1
    protected function getName(): string
53
    {
54 1
        return $this->name;
55
    }
56
57
    protected function getCallback(): callable
58
    {
59
        return $this->callback;
60
    }
61
62
    protected function getParams(): array
63
    {
64
        return $this->params;
65
    }
66
67 1
    protected function getSkip(): bool|string
68
    {
69 1
        return $this->skip;
70
    }
71
72 1
    protected function getResult(): string
73
    {
74 1
        return $this->result;
75
    }
76
77 1
    protected function getOutput(): string
78
    {
79 1
        return $this->output;
80
    }
81
82
    /**
83
     * Executes the task
84
     */
85 1
    public function execute(): void
86
    {
87 1
        if ($this->skip) {
88 1
            $this->result = static::RESULT_SKIPPED;
89
        } else {
90 1
            ob_start();
91 1
            call_user_func_array($this->callback, $this->params);
92 1
            $this->onAfterExecute();
93
            /** @var string $output */
94 1
            $output = ob_get_clean();
95 1
            if (str_contains($output, " failed. ")) {
96
                $this->result = static::RESULT_FAILED;
97 1
            } elseif (str_starts_with($output, "Warning: ")) {
98 1
                $this->result = static::RESULT_PASSED_WITH_WARNINGS;
99
            }
100 1
            $this->output = $output;
101
        }
102 1
    }
103
}
104