Passed
Push — master ( 1af2e0...649593 )
by Jakub
09:49
created

Job::getParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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