Completed
Push — master ( b25a31...347863 )
by Jakub
01:41
created

Tester::printInfo()

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 5
cts 5
cp 1
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\TestSuitesFinder;
9
use Nette\Utils\Finder;
10
11
/**
12
 * Automated tests runner
13
 * 
14
 * @author Jakub Konečný
15
 * @property-read string[] $suites
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 $suites;
26
  /** @var callable[] */
27
  public array $onExecute = [];
28
  public ITestSuiteFactory $testSuiteFactory;
29
  private string $folder;
30
  /** @var SkippedTest[] */
31
  private array $skipped = [];
32
  private string $results = "";
33
34 1
  public function __construct(string $folder) {
35 1
    $this->onExecute[] = [$this, "setup"];
36 1
    $this->onExecute[] = [$this, "printInfo"];
37 1
    $this->suites = (new TestSuitesFinder())->getSuites($folder);
38 1
    $this->testSuiteFactory = new class implements ITestSuiteFactory {
39
      public function create(string $className): TestCase {
40 1
        return new $className();
41
      }
42
    };
43 1
    $this->folder = $folder;
44 1
  }
45
  
46
  /**
47
   * @return string[]
48
   */
49
  protected function getSuites(): array {
50
    return $this->suites;
51
  }
52
  
53
  /**
54
   * Execute all tests
55
   */
56 1
  public function execute(): void {
57 1
    $this->onExecute();
58 1
    $failed = false;
59 1
    foreach($this->suites as $suite) {
60 1
      $suite = $this->testSuiteFactory->create($suite);
61 1
      if(!$suite->run()) {
62
        $failed = true;
63
      }
64 1
      $this->saveResults($suite);
65
    }
66 1
    $this->printResults();
67 1
    exit((int) $failed);
68
  }
69
70 1
  private function setup(): void {
71 1
    Timer::start(static::TIMER_NAME);
72 1
  }
73
74
  /**
75
   * Print version of My Tester and PHP
76
   */
77 1
  private function printInfo(): void {
78 1
    echo "My Tester " . PrettyVersions::getVersion(static::PACKAGE_NAME) . "\n";
79 1
    echo "\n";
80 1
    echo "PHP " . PHP_VERSION . "(" . PHP_SAPI . ")\n";
81 1
    echo "\n";
82 1
  }
83
84 1
  private function printResults(): void {
85 1
    $results = $this->results;
86 1
    echo $results . "\n";
87 1
    $this->printSkipped();
88 1
    $failed = str_contains($results, TestCase::RESULT_FAILED);
89 1
    if(!$failed) {
90 1
      echo "\n";
91 1
      echo "OK";
92
    } else {
93
      $this->printFailed();
94
      echo "\n";
95
      echo "Failed";
96
    }
97 1
    $resultsLine = " (" . strlen($results) . " tests";
98 1
    if($failed) {
99
      $resultsLine .= ", " . substr_count($results, TestCase::RESULT_FAILED) . " failed";
100
    }
101 1
    if(str_contains($results, TestCase::RESULT_SKIPPED)) {
102 1
      $resultsLine .= ", " . substr_count($results, TestCase::RESULT_SKIPPED) . " skipped";
103
    }
104 1
    Timer::stop(static::TIMER_NAME);
105 1
    $time = Timer::read(static::TIMER_NAME, Timer::FORMAT_HUMAN);
106 1
    $resultsLine .= ", $time)";
107 1
    echo $resultsLine . "\n";
108 1
  }
109
110
  /**
111
   * Print info about skipped tests
112
   */
113 1
  private function printSkipped(): void {
114 1
    foreach($this->skipped as $skipped) {
115 1
      $reason = "";
116 1
      if($skipped->reason) {
117 1
        $reason = ": {$skipped->reason}";
118
      }
119 1
      echo "Skipped $skipped->name$reason\n";
120
    }
121 1
  }
122
123
  /**
124
   * Print info about failed tests
125
   */
126
  private function printFailed(): void {
127
    $filenameSuffix = ".errors";
128
    $files = Finder::findFiles("*$filenameSuffix")->in($this->folder);
129
    /** @var \SplFileInfo $file */
130
    foreach($files as $name => $file) {
131
      echo "--- " . $file->getBasename($filenameSuffix) . "\n";
132
      echo file_get_contents($name);
133
    }
134
  }
135
136 1
  private function saveResults(TestCase $testCase): void {
137 1
    $jobs = $testCase->jobs;
138 1
    foreach($jobs as $job) {
139 1
      switch($job->result) {
140 1
        case Job::RESULT_PASSED:
141 1
          $result = TestCase::RESULT_PASSED;
142 1
          break;
143 1
        case Job::RESULT_SKIPPED:
144 1
          $result = TestCase::RESULT_SKIPPED;
145 1
          $this->skipped[] = new SkippedTest($job->name, (is_string($job->skip) ? $job->skip : ""));
146 1
          break;
147
        case Job::RESULT_FAILED:
148
          $result = TestCase::RESULT_FAILED;
149
          break;
150
        default:
151
          $result = "";
152
          break;
153
      }
154 1
      $this->results .= $result;
155
    }
156 1
  }
157
}
158
?>