Completed
Push — master ( b48e27...d83119 )
by Jakub
06:58
created

Environment::addResult()   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
c 0
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
  public static function checkFailed(string $results): bool {
43
    return str_contains($results, " failed. ");
44
  }
45
  
46
  /**
47
   * Checks if environment was set
48
   */
49
  public static function isSetUp(): bool {
50
    return static::$set;
51
  }
52
  
53
  /**
54
   * Increases task counter
55
   */
56
  public static function incCounter(): void {
57
    static::$taskCount++;
58
  }
59
  
60
  /**
61
   * Resets task counter
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
  public static function addResult(string $result): void {
86
    static::$results .= $result;
87
  }
88
89
  public static function addSkipped(string $jobName, string $reason = ""): void {
90
    static::$skipped[] = new SkippedTest($jobName, $reason);
91
  }
92
93
  /**
94
   * @return SkippedTest[]
95
   */
96
  public static function getSkipped(): array {
97
    return static::$skipped;
98
  }
99
  
100
  public static function getShouldFail(): bool {
101
    return static::$shouldFail;
102
  }
103
  
104
  public static function setShouldFail(bool $value): void {
105
    static::$shouldFail = $value;
106
  }
107
  
108
  /**
109
   * Print version of My Tester and PHP
110
   */
111
  public static function printInfo(): void {
112
    static::printLine(static::NAME . " " . static::VERSION);
113
    static::printLine("");
114
    static::printLine("PHP " . PHP_VERSION . "(" . PHP_SAPI . ")");
115
    static::printLine("");
116
  }
117
118
  public static function printResults(): void {
119
    $results = static::$results;
120
    static::printLine($results);
121
    static::printSkipped();
122
    static::printFailed();
123
    static::printLine("");
124
    $failed = str_contains($results, TestCase::RESULT_FAILED);
125
    if(!$failed) {
126
      echo "OK";
127
    } else {
128
      echo "Failed";
129
    }
130
    $resultsLine = " (" . strlen($results) . " tests";
131
    if($failed) {
132
      $resultsLine .= ", " . substr_count($results, TestCase::RESULT_FAILED) . " failed";
133
    }
134
    if(str_contains($results, TestCase::RESULT_SKIPPED)) {
135
      $resultsLine .= ", " . substr_count($results, TestCase::RESULT_SKIPPED) . " skipped";
136
    }
137
    $resultsLine .= ")";
138
    static::printLine($resultsLine);
139
  }
140
141
  /**
142
   * Print info about skipped tests
143
   * @internal
144
   */
145
  public static function printSkipped(): void {
146
    foreach(static::getSkipped() as $skipped) {
147
      $reason = "";
148
      if($skipped->reason) {
149
        $reason = ": {$skipped->reason}";
150
      }
151
      static::printLine("Skipped $skipped->name$reason");
152
    }
153
  }
154
155
  /**
156
   * Print info about failed tests
157
   * @internal
158
   */
159
  public static function printFailed(): void {
160
    $files = Finder::findFiles("*.errors")->in(\getTestsDirectory());
161
    /** @var \SplFileInfo $file */
162
    foreach($files as $name => $file) {
163
      static::printLine("--- " . substr($file->getBasename(), 0, -7));
164
      echo file_get_contents($name);
165
    }
166
  }
167
  
168
  /**
169
   * Sets up the environment
170
   */
171
  public static function setup(): void {
172
    if(static::$set) {
173
      static::printLine("Warning: Testing Environment was already set up.");
174
      return;
175
    }
176
    register_shutdown_function(function(): void {
177
      $time = \Tracy\Debugger::timer(static::NAME);
178
      static::printLine("");
179
      static::printLine("Total run time: $time second(s)");
180
    });
181
    \Tracy\Debugger::timer(static::NAME);
182
    static::$mode = ((PHP_SAPI === "cli") ? static::MODE_CLI : static::MODE_HTTP);
183
    static::$set = true;
184
  }
185
}
186
?>