Completed
Push — master ( e41582...106686 )
by Jakub
01:35
created

Tester.php$0 ➔ printSkipped()   A

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace MyTester;
5
6
use Ayesh\PHP_Timer\Timer;
7
use Jean85\PrettyVersions;
8
use MyTester\Bridges\NetteRobotLoader\TestSuitsFinder;
9
use Nette\Utils\Finder;
10
11
/**
12
 * Automated tests runner
13
 * 
14
 * @author Jakub Konečný
15
 * @property-read string[] $suits
16
 * @method void onExecute()
17
 */
18
final class Tester {
19
  use \Nette\SmartObject;
20
21
  private const PACKAGE_NAME = "konecnyjakub/mytester";
22
  private const TIMER_NAME = "My Tester";
23
  
24
  /** @var string[] */
25
  private array $suits;
26
  /** @var callable[] */
27
  public array $onExecute = [
28
    Environment::class . "::setup",
29
  ];
30
  public ITestSuitFactory $testSuitFactory;
31
  private string $folder;
32
  /** @var SkippedTest[] */
33
  private array $skipped = [];
34
  private string $results = "";
35
  
36
  public function __construct(string $folder) {
37
    $this->onExecute[] = [$this, "printInfo"];
38
    $this->suits = (new TestSuitsFinder())->getSuits($folder);
39
    $this->testSuitFactory = new class implements ITestSuitFactory {
40
      public function create(string $className): TestCase {
41
        return new $className();
42
      }
43
    };
44
    $this->folder = $folder;
45
  }
46
  
47
  /**
48
   * @return string[]
49
   */
50
  protected function getSuits(): array {
51
    return $this->suits;
52
  }
53
  
54
  /**
55
   * Execute all tests
56
   */
57
  public function execute(): void {
58
    $this->onExecute();
59
    $failed = false;
60
    foreach($this->suits as $suit) {
61
      $suit = $this->testSuitFactory->create($suit[0]);
62
      if(!$suit->run()) {
63
        $failed = true;
64
      }
65
      $this->saveResults($suit);
66
    }
67
    $this->printResults();
68
    exit((int) $failed);
69
  }
70
71
  /**
72
   * Print version of My Tester and PHP
73
   */
74
  private function printInfo(): void {
75
    echo "My Tester " . PrettyVersions::getVersion(static::PACKAGE_NAME) . "\n";
76
    echo "\n";
77
    echo "PHP " . PHP_VERSION . "(" . PHP_SAPI . ")\n";
78
    echo "\n";
79
  }
80
81
  private function printResults(): void {
82
    $results = $this->results;
83
    echo $results . "\n";
84
    $this->printSkipped();
85
    $failed = str_contains($results, TestCase::RESULT_FAILED);
86
    if(!$failed) {
87
      echo "\n";
88
      echo "OK";
89
    } else {
90
      static::printFailed();
0 ignored issues
show
Bug Best Practice introduced by
The method MyTester\Tester::printFailed() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
      static::/** @scrutinizer ignore-call */ 
91
              printFailed();
Loading history...
91
      echo "\n";
92
      echo "Failed";
93
    }
94
    $resultsLine = " (" . strlen($results) . " tests";
95
    if($failed) {
96
      $resultsLine .= ", " . substr_count($results, TestCase::RESULT_FAILED) . " failed";
97
    }
98
    if(str_contains($results, TestCase::RESULT_SKIPPED)) {
99
      $resultsLine .= ", " . substr_count($results, TestCase::RESULT_SKIPPED) . " skipped";
100
    }
101
    Timer::stop(static::TIMER_NAME);
102
    $time = Timer::read(static::TIMER_NAME, Timer::FORMAT_HUMAN);
103
    $resultsLine .= ", $time)";
104
    echo $resultsLine . "\n";
105
  }
106
107
  /**
108
   * Print info about skipped tests
109
   */
110
  private function printSkipped(): void {
111
    foreach($this->skipped as $skipped) {
112
      $reason = "";
113
      if($skipped->reason) {
114
        $reason = ": {$skipped->reason}";
115
      }
116
      echo "Skipped $skipped->name$reason\n";
117
    }
118
  }
119
120
  /**
121
   * Print info about failed tests
122
   */
123
  private function printFailed(): void {
124
    $filenameSuffix = ".errors";
125
    $files = Finder::findFiles("*$filenameSuffix")->in($this->folder);
126
    /** @var \SplFileInfo $file */
127
    foreach($files as $name => $file) {
128
      echo "--- " . $file->getBasename($filenameSuffix) . "\n";
129
      echo file_get_contents($name);
130
    }
131
  }
132
133
  private function saveResults(TestCase $testCase): void {
134
    $jobs = $testCase->jobs;
135
    foreach($jobs as $job) {
136
      switch($job->result) {
137
        case Job::RESULT_PASSED:
138
          $result = TestCase::RESULT_PASSED;
139
          break;
140
        case Job::RESULT_SKIPPED:
141
          $result = TestCase::RESULT_SKIPPED;
142
          $this->skipped[] = new SkippedTest($job->name, (is_string($job->skip) ? $job->skip : ""));
143
          break;
144
        case Job::RESULT_FAILED:
145
          $result = TestCase::RESULT_FAILED;
146
          break;
147
        default:
148
          $result = "";
149
          break;
150
      }
151
      $this->results .= $result;
152
    }
153
  }
154
}
155
?>