Completed
Push — master ( 283965...5ee203 )
by Jakub
01:42
created

Job::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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