Completed
Push — master ( d44e44...2e4620 )
by Jakub
01:27
created

Environment::checkFailed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace MyTester;
5
6
use Nette\Utils\Finder;
7
8
/**
9
 * Testing Environment
10
 *
11
 * @author Jakub Konečný
12
 */
13
final class Environment {
14
  use \Nette\StaticClass;
15
  
16
  public const NAME = "My Tester";
17
  public const VERSION = "2.0.0-dev";
18
19
  public const MODE_CLI = "cli";
20
  public const MODE_HTTP = "http";
21
22
  private static int $taskCount = 0;
23
  private static bool $set = false;
24
  private static string $mode;
25
  /** @var SkippedTest[] */
26
  private static array $skipped = [];
27
  public static string $currentJob = "";
28
  public static bool $shouldFail = false;
29
  private static string $results = "";
30
  
31
  /**
32
   * Prints result of a test
33
   */
34
  public static function testResult(string $text, bool $success = true): void {
35
    static::incCounter();
36
    if($success) {
37
      return;
38
    }
39
    static::printLine("Test " . static::$taskCount . " failed. $text");
40
  }
41
  
42
  /**
43
   * Checks if environment was set
44
   */
45
  public static function isSetUp(): bool {
46
    return static::$set;
47
  }
48
  
49
  /**
50
   * Increases task counter
51
   *
52
   * @internal
53
   */
54
  public static function incCounter(): void {
55
    static::$taskCount++;
56
  }
57
  
58
  /**
59
   * Resets task counter
60
   *
61
   * @internal
62
   */
63
  public static function resetCounter(): void {
64
    static::$taskCount = 0;
65
  }
66
  
67
  public static function getCounter(): int {
68
    return static::$taskCount;
69
  }
70
  
71
  public static function getMode(): string {
72
    return static::$mode;
73
  }
74
  
75
  /**
76
   * Prints entered text with correct line ending
77
   */
78
  public static function printLine(string $text): void {
79
    if(static::$mode === static::MODE_HTTP) {
80
      $text .= "<br>";
81
    }
82
    echo "$text\n";
83
  }
84
85
  /**
86
   * @internal
87
   */
88
  public static function addResult(string $result): void {
89
    static::$results .= $result;
90
  }
91
92
  /**
93
   * @internal
94
   */
95
  public static function addSkipped(string $jobName, string $reason = ""): void {
96
    static::$skipped[] = new SkippedTest($jobName, $reason);
97
  }
98
99
  /**
100
   * @return SkippedTest[]
101
   */
102
  public static function getSkipped(): array {
103
    return static::$skipped;
104
  }
105
  
106
  public static function getShouldFail(): bool {
107
    return static::$shouldFail;
108
  }
109
  
110
  public static function setShouldFail(bool $value): void {
111
    static::$shouldFail = $value;
112
  }
113
  
114
  /**
115
   * Print version of My Tester and PHP
116
   */
117
  public static function printInfo(): void {
118
    static::printLine(static::NAME . " " . static::VERSION);
119
    static::printLine("");
120
    static::printLine("PHP " . PHP_VERSION . "(" . PHP_SAPI . ")");
121
    static::printLine("");
122
  }
123
124
  public static function printResults(): void {
125
    $results = static::$results;
126
    static::printLine($results);
127
    static::printSkipped();
128
    static::printFailed();
129
    static::printLine("");
130
    $failed = str_contains($results, TestCase::RESULT_FAILED);
131
    if(!$failed) {
132
      echo "OK";
133
    } else {
134
      echo "Failed";
135
    }
136
    $resultsLine = " (" . strlen($results) . " tests";
137
    if($failed) {
138
      $resultsLine .= ", " . substr_count($results, TestCase::RESULT_FAILED) . " failed";
139
    }
140
    if(str_contains($results, TestCase::RESULT_SKIPPED)) {
141
      $resultsLine .= ", " . substr_count($results, TestCase::RESULT_SKIPPED) . " skipped";
142
    }
143
    $resultsLine .= ")";
144
    static::printLine($resultsLine);
145
  }
146
147
  /**
148
   * Print info about skipped tests
149
   */
150
  private static function printSkipped(): void {
151
    foreach(static::getSkipped() as $skipped) {
152
      $reason = "";
153
      if($skipped->reason) {
154
        $reason = ": {$skipped->reason}";
155
      }
156
      static::printLine("Skipped $skipped->name$reason");
157
    }
158
  }
159
160
  /**
161
   * Print info about failed tests
162
   */
163
  private static function printFailed(): void {
164
    $files = Finder::findFiles("*.errors")->in(\getTestsDirectory());
165
    /** @var \SplFileInfo $file */
166
    foreach($files as $name => $file) {
167
      static::printLine("--- " . substr($file->getBasename(), 0, -7));
168
      echo file_get_contents($name);
169
    }
170
  }
171
  
172
  /**
173
   * Sets up the environment
174
   */
175
  public static function setup(): void {
176
    if(static::$set) {
177
      static::printLine("Warning: Testing Environment was already set up.");
178
      return;
179
    }
180
    register_shutdown_function(function(): void {
181
      $time = \Tracy\Debugger::timer(static::NAME);
182
      static::printLine("");
183
      static::printLine("Total run time: $time second(s)");
184
    });
185
    \Tracy\Debugger::timer(static::NAME);
186
    static::$mode = ((PHP_SAPI === "cli") ? static::MODE_CLI : static::MODE_HTTP);
187
    static::$set = true;
188
  }
189
}
190
?>