Completed
Push — master ( b25a31...347863 )
by Jakub
01:41
created

Job::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 3
b 0
f 0
nc 1
nop 5
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace MyTester;
5
6 1
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 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
  /**
75
   * Executes the task
76
   */
77 1
  public function execute(): void {
78 1
    if($this->skip) {
79 1
      $this->result = static::RESULT_SKIPPED;
80
    } else {
81 1
      ob_start();
82 1
      if(isset($this->callback)) {
83 1
        call_user_func_array($this->callback, $this->params);
84
      }
85
      /** @var string $output */
86 1
      $output = ob_get_clean();
87 1
      $failed = str_contains($output, " failed. ");
88 1
      if($failed && !$this->shouldFail) {
89
        $this->result = static::RESULT_FAILED;
90
      }
91 1
      if(strlen($output) && $this->result === static::RESULT_FAILED) {
92
        file_put_contents(\getTestsDirectory() . "/$this->name.errors", $output);
93
      }
94
    }
95 1
  }
96
}
97
?>