Completed
Push — code-coverage ( 92d39b...48bbe4 )
by Jakub
02:13
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
    $failed = str_contains($results, TestCase::RESULT_FAILED);
129
    if(!$failed) {
130
      static::printLine();
131
      echo "OK";
132
    } else {
133
      static::printFailed();
134
      static::printLine();
135
      echo "Failed";
136
    }
137
    $resultsLine = " (" . strlen($results) . " tests";
138
    if($failed) {
139
      $resultsLine .= ", " . substr_count($results, TestCase::RESULT_FAILED) . " failed";
140
    }
141
    if(str_contains($results, TestCase::RESULT_SKIPPED)) {
142
      $resultsLine .= ", " . substr_count($results, TestCase::RESULT_SKIPPED) . " skipped";
143
    }
144
    $time = \Tracy\Debugger::timer(static::NAME);
145
    $resultsLine .= ", $time second(s))";
146
    static::printLine($resultsLine);
147
  }
148
149
  /**
150
   * Print info about skipped tests
151
   */
152
  private static function printSkipped(): void {
153
    foreach(static::getSkipped() as $skipped) {
154
      $reason = "";
155
      if($skipped->reason) {
156
        $reason = ": {$skipped->reason}";
157
      }
158
      static::printLine("Skipped $skipped->name$reason");
159
    }
160
  }
161
162
  /**
163
   * Print info about failed tests
164
   */
165
  private static function printFailed(): void {
166
    $filenameSuffix = ".errors";
167
    $files = Finder::findFiles("*$filenameSuffix")->in(\getTestsDirectory());
168
    /** @var \SplFileInfo $file */
169
    foreach($files as $name => $file) {
170
      static::printLine("--- " . $file->getBasename($filenameSuffix));
171
      echo file_get_contents($name);
172
    }
173
  }
174
  
175
  /**
176
   * Sets up the environment
177
   */
178
  public static function setup(): void {
179
    if(static::$set) {
180
      static::printLine("Warning: Testing Environment was already set up.");
181
      return;
182
    }
183
    \Tracy\Debugger::timer(static::NAME);
184
    static::$mode = ((PHP_SAPI === "cli") ? static::MODE_CLI : static::MODE_HTTP);
185
    static::$set = true;
186
  }
187
}
188
?>