Completed
Push — master ( dbb439...8abb49 )
by Jakub
01:34
created

Job::execute()   A

Complexity

Conditions 5
Paths 5

Size

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