Completed
Push — master ( 67e18b...1f3c92 )
by Jakub
01:44
created

Tester   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 25
Bugs 5 Features 5
Metric Value
eloc 45
c 25
b 5
f 5
dl 0
loc 79
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A findSuits() 0 24 6
A getSuits() 0 2 1
A execute() 0 25 5
1
<?php
2
declare(strict_types=1);
3
4
namespace MyTester;
5
6
use Nette\Utils\Finder;
7
use Nette\Utils\FileSystem;
8
9
/**
10
 * Automated tests runner
11
 * 
12
 * @author Jakub Konečný
13
 * @property-read string[] $suits
14
 * @method void onExecute()
15
 */
16
final class Tester {
17
  use \Nette\SmartObject;
18
  
19
  /** @var string[] */
20
  private array $suits;
21
  /** @var callable[] */
22
  public array $onExecute = [
23
    Environment::class . "::setup",
24
    Environment::class . "::printInfo",
25
  ];
26
  
27
  public function __construct(string $folder) {
28
    $this->suits = $this->findSuits($folder);
29
  }
30
  
31
  /**
32
   * Find test suits to run
33
   */
34
  private function findSuits(string $folder): array {
35
    $suits = [];
36
    $robot = new \Nette\Loaders\RobotLoader();
37
    $tempDir = "$folder/temp/cache/Robot.Loader";
38
    if(is_dir("$folder/_temp")) {
39
      $tempDir = "$folder/_temp/cache/Robot.Loader";
40
    }
41
    FileSystem::createDir($tempDir);
42
    $robot->setTempDirectory($tempDir);
43
    $robot->addDirectory($folder);
44
    $robot->acceptFiles = ["*.phpt"];
45
    $robot->rebuild();
46
    $robot->register();
47
    $classes = $robot->getIndexedClasses();
48
    foreach($classes as $class => $file) {
49
      if(!class_exists($class)) {
50
        continue;
51
      }
52
      $rc = new \ReflectionClass($class);
53
      if(!$rc->isAbstract() && $rc->isSubclassOf(TestCase::class)) {
54
        $suits[] = [$rc->getName(), $file];
55
      }
56
    }
57
    return $suits;
58
  }
59
  
60
  /**
61
   * @return string[]
62
   */
63
  protected function getSuits(): array {
64
    return $this->suits;
65
  }
66
  
67
  /**
68
   * Execute all tests
69
   */
70
  public function execute(): void {
71
    $this->onExecute();
72
    $failed = false;
73
    foreach($this->suits as $suit) {
74
      /** @var TestCase $suit */
75
      $suit = new $suit[0]();
76
      if(!$suit->run()) {
77
        $failed = true;
78
      }
79
    }
80
    Environment::printLine("");
81
    Environment::printSkipped();
82
    if($failed) {
83
      Environment::printLine("Failed");
84
      Environment::printLine("");
85
      $files = Finder::findFiles("*.errors")->in(\getTestsDirectory());
86
      /** @var \SplFileInfo $file */
87
      foreach($files as $name => $file) {
88
        Environment::printLine("--- " . substr($file->getBasename(), 0, -7));
89
        echo file_get_contents($name);
90
      }
91
    } else {
92
      Environment::printLine("OK");
93
    }
94
    exit((int) $failed);
95
  }
96
}
97
?>