Completed
Push — code-coverage ( 571de7...0d23d4 )
by Jakub
03:03 queued 01:26
created

Environment::getShouldFail()   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
/**
7
 * Testing Environment
8
 *
9
 * @author Jakub Konečný
10
 */
11
final class Environment {
12
  use \Nette\StaticClass;
13
  
14
  public const NAME = "My Tester";
15
  public const VERSION = "2.0.0-dev";
16
17
  public const MODE_CLI = "cli";
18
  public const MODE_HTTP = "http";
19
20
  private static int $taskCount = 0;
21
  private static bool $set = false;
22
  private static string $mode;
23
  private static array $skipped = [];
24
  public static string $currentJob = "";
25
  public static bool $shouldFail = false;
26
  
27
  /**
28
   * Prints result of a test
29
   */
30
  public static function testResult(string $text, bool $success = true): void {
31
    static::incCounter();
32
    if($success) {
33
      return;
34
    }
35
    $output = "Test " . static::$taskCount . " failed";
36
    static::printLine($output . ". $text");
37
  }
38
  
39
  public static function checkFailed(string $results): bool {
40
    $testsFailed = substr_count($results, " failed. ");
41
    return (bool) $testsFailed;
42
  }
43
  
44
  /**
45
   * Checks if environment was set
46
   */
47
  public static function isSetUp(): bool {
48
    return static::$set;
49
  }
50
  
51
  /**
52
   * Increases task counter
53
   */
54
  public static function incCounter(): void {
55
    static::$taskCount++;
56
  }
57
  
58
  /**
59
   * Resets task counter
60
   */
61
  public static function resetCounter(): void {
62
    static::$taskCount = 0;
63
  }
64
  
65
  public static function getCounter(): int {
66
    return static::$taskCount;
67
  }
68
  
69
  public static function getMode(): string {
70
    return static::$mode;
71
  }
72
  
73
  /**
74
   * Prints entered text with correct line ending
75
   */
76
  public static function printLine(string $text): void {
77
    if(static::$mode === static::MODE_HTTP) {
78
      $text .= "<br>";
79
    }
80
    echo "$text\n";
81
  }
82
  
83
  /**
84
   * @param string|bool $reason
85
   */
86
  public static function addSkipped(string $jobName, $reason = ""): void {
87
    static::$skipped[] = ["name" => $jobName, "reason" => $reason];
88
  }
89
  
90
  public static function getSkipped(): array {
91
    return static::$skipped;
92
  }
93
  
94
  public static function getShouldFail(): bool {
95
    return static::$shouldFail;
96
  }
97
  
98
  public static function setShouldFail(bool $value): void {
99
    static::$shouldFail = $value;
100
  }
101
  
102
  /**
103
   * Print version of My Tester and PHP
104
   */
105
  public static function printInfo(): void {
106
    static::printLine(static::NAME . " " . static::VERSION);
107
    static::printLine("");
108
    static::printLine("PHP " . PHP_VERSION . "(" . PHP_SAPI . ")");
109
    static::printLine("");
110
  }
111
112
  /**
113
   * Print info about skipped tests
114
   */
115
  public static function printSkipped(): void {
116
    foreach(static::getSkipped() as $skipped) {
117
      $reason = "";
118
      if($skipped["reason"]) {
119
        $reason = ": {$skipped["reason"]}";
120
      }
121
      static::printLine("Skipped {$skipped["name"]}$reason");
122
    }
123
  }
124
  
125
  /**
126
   * Sets up the environment
127
   */
128
  public static function setup(): void {
129
    if(static::$set) {
130
      static::printLine("Warning: Testing Environment was already set up.");
131
      return;
132
    }
133
    register_shutdown_function(function(): void {
134
      $time = \Tracy\Debugger::timer(static::NAME);
135
      static::printLine("");
136
      static::printLine("Total run time: $time second(s)");
137
    });
138
    \Tracy\Debugger::timer(static::NAME);
139
    static::$mode = ((PHP_SAPI === "cli") ? static::MODE_CLI : static::MODE_HTTP);
140
    static::$set = true;
141
  }
142
}
143
?>