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

Job   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 78.13%

Importance

Changes 14
Bugs 1 Features 2
Metric Value
wmc 13
eloc 35
dl 0
loc 78
rs 10
c 14
b 1
f 2
ccs 25
cts 32
cp 0.7813

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSkip() 0 2 1
A isShouldFail() 0 2 1
A getName() 0 2 1
A getResult() 0 2 1
A getCallback() 0 2 1
A getParams() 0 2 1
A getOutput() 0 2 1
A execute() 0 15 5
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
?>