Completed
Push — master ( c8c0c1...47af09 )
by Jakub
01:44
created

Job::execute()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

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