1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MyTester\Bridges\NetteDI; |
5
|
|
|
|
6
|
|
|
use Nette\DI\Helpers; |
7
|
|
|
use Nette\Schema\Expect; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* MyTester Extension for Nette DIC |
11
|
|
|
* |
12
|
|
|
* @author Jakub Konečný |
13
|
|
|
* @copyright (c) 2016-2019, Jakub Konečný |
14
|
|
|
* @license https://spdx.org/licenses/BSD-3-Clause.html BSD-3-Clause |
15
|
|
|
*/ |
16
|
|
|
final class MyTesterExtension extends \Nette\DI\CompilerExtension { |
17
|
|
|
public const TAG = "mytester.test"; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
private $suits; |
21
|
|
|
|
22
|
|
|
public function getConfigSchema(): \Nette\Schema\Schema { |
23
|
|
|
$params = $this->getContainerBuilder()->parameters; |
24
|
|
|
return Expect::structure([ |
25
|
|
|
"folder" => Expect::string(Helpers::expand("%appDir%/../tests", $params)), |
26
|
|
|
"onExecute" => Expect::array()->default([]), |
27
|
|
|
])->castTo("array"); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
|
|
public function loadConfiguration(): void { |
34
|
|
|
$config = $this->getConfig(); |
35
|
|
|
$builder = $this->getContainerBuilder(); |
36
|
|
|
$builder->addDefinition($this->prefix("runner")) |
37
|
|
|
->setType(TestsRunner::class); |
38
|
|
|
if(!is_dir($config["folder"])) { |
39
|
|
|
throw new \Exception("Invalid folder {$config["folder"]} for $this->name.folder"); |
40
|
|
|
} |
41
|
|
|
$tester = new \MyTester\Tester($config["folder"]); |
42
|
|
|
$this->suits = $tester->getSuits(); |
43
|
|
|
foreach($this->suits as $index => $suit) { |
44
|
|
|
$builder->addDefinition($this->prefix("test." . ($index + 1))) |
45
|
|
|
->setType($suit[0]) |
46
|
|
|
->addTag(self::TAG); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function afterCompile(\Nette\PhpGenerator\ClassType $class): void { |
51
|
|
|
$config = $this->getConfig(); |
52
|
|
|
$container = $this->getContainerBuilder(); |
53
|
|
|
$initialize = $class->methods["initialize"]; |
54
|
|
|
$initialize->addBody('$runner = $this->getService(?);', [$this->prefix("runner")]); |
55
|
|
|
$initialize->addBody('spl_autoload_extensions(spl_autoload_extensions() . ",.phpt"); |
56
|
|
|
MyTester\Bridges\NetteDI\TestsRunner::$autoloader = ?; |
57
|
|
|
spl_autoload_register(?);', [$this->suits, TestsRunner::class . "::autoload"]); |
58
|
|
|
foreach($container->findByTag(self::TAG) as $suit => $foo) { |
59
|
|
|
$initialize->addBody('$runner->addSuit($this->getService(?));', [$suit]); |
60
|
|
|
} |
61
|
|
|
$onExecute = array_merge(['MyTester\Environment::setup', 'MyTester\Environment::printInfo'], $config["onExecute"]); |
62
|
|
|
foreach($onExecute as &$task) { |
63
|
|
|
if(!is_array($task)) { |
64
|
|
|
$task = explode("::", $task); |
65
|
|
|
} elseif(substr($task[0], 0, 1) === "@") { |
66
|
|
|
$initialize->addBody('$runner->onExecute[] = [$this->getService(?), ?];', [substr($task[0], 1), $task[1]]); |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
$initialize->addBody('$runner->onExecute[] = [?, ?];', [$task[0], $task[1]]); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
?> |