|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MyTester\Bridges\NetteDI; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use MyTester\Bridges\NetteRobotLoader\TestSuitesFinder; |
|
9
|
|
|
use MyTester\Tester; |
|
10
|
|
|
use Nette\DI\Helpers; |
|
11
|
|
|
use Nette\Schema\Expect; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* MyTester Extension for Nette DIC |
|
15
|
|
|
* |
|
16
|
|
|
* @author Jakub Konečný |
|
17
|
|
|
* @method array getConfig() |
|
18
|
|
|
*/ |
|
19
|
|
|
final class MyTesterExtension extends \Nette\DI\CompilerExtension |
|
20
|
|
|
{ |
|
21
|
|
|
public const TAG = "mytester.test"; |
|
22
|
|
|
|
|
23
|
1 |
|
public function getConfigSchema(): \Nette\Schema\Schema |
|
24
|
|
|
{ |
|
25
|
1 |
|
$params = $this->getContainerBuilder()->parameters; |
|
26
|
1 |
|
return Expect::structure([ |
|
27
|
1 |
|
"folder" => Expect::string(Helpers::expand("%appDir%/../tests", $params)), |
|
28
|
1 |
|
"onExecute" => Expect::array()->default([]), |
|
29
|
1 |
|
"onFinish" => Expect::array()->default([]), |
|
30
|
1 |
|
"colors" => Expect::bool(false), |
|
31
|
1 |
|
])->castTo("array"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @throws Exception |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function loadConfiguration(): void |
|
38
|
|
|
{ |
|
39
|
1 |
|
$config = $this->getConfig(); |
|
40
|
1 |
|
$builder = $this->getContainerBuilder(); |
|
41
|
1 |
|
if (!is_dir($config["folder"])) { |
|
42
|
|
|
throw new Exception("Invalid folder {$config["folder"]} for $this->name.folder"); |
|
43
|
|
|
} |
|
44
|
1 |
|
$builder->addDefinition($this->prefix("runner")) |
|
45
|
1 |
|
->setFactory(Tester::class, [$config["folder"]]); |
|
46
|
1 |
|
$builder->addDefinition($this->prefix("suiteFactory")) |
|
47
|
1 |
|
->setType(ContainerSuiteFactory::class); |
|
48
|
1 |
|
$suites = (new TestSuitesFinder())->getSuites($config["folder"]); |
|
49
|
1 |
|
foreach ($suites as $index => $suite) { |
|
50
|
1 |
|
$builder->addDefinition($this->prefix("test." . ($index + 1))) |
|
51
|
1 |
|
->setType($suite) |
|
52
|
1 |
|
->addTag(self::TAG); |
|
53
|
|
|
} |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public function afterCompile(\Nette\PhpGenerator\ClassType $class): void |
|
57
|
|
|
{ |
|
58
|
1 |
|
$config = $this->getConfig(); |
|
59
|
1 |
|
$initialize = $class->methods["initialize"]; |
|
60
|
1 |
|
$initialize->addBody('$runner = $this->getService(?);', [$this->prefix("runner")]); |
|
61
|
1 |
|
$initialize->addBody('$runner->useColors = ?;', [$config["colors"]]); |
|
62
|
1 |
|
foreach ($config["onExecute"] as &$task) { |
|
63
|
|
|
if (!is_array($task)) { |
|
64
|
|
|
$task = explode("::", $task); |
|
65
|
|
|
} elseif (str_starts_with($task[0], "@")) { |
|
66
|
|
|
$className = substr($task[0], 1); |
|
67
|
|
|
$initialize->addBody('$runner->onExecute[] = [$this->getService(?), ?];', [$className, $task[1]]); |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
$initialize->addBody('$runner->onExecute[] = [?, ?];', [$task[0], $task[1]]); |
|
71
|
|
|
} |
|
72
|
1 |
|
foreach ($config["onFinish"] as &$task) { |
|
73
|
|
|
if (!is_array($task)) { |
|
74
|
|
|
$task = explode("::", $task); |
|
75
|
|
|
} elseif (str_starts_with($task[0], "@")) { |
|
76
|
|
|
$className = substr($task[0], 1); |
|
77
|
|
|
$initialize->addBody('$runner->onFinish[] = [$this->getService(?), ?];', [$className, $task[1]]); |
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
$initialize->addBody('$runner->onFinish[] = [?, ?];', [$task[0], $task[1]]); |
|
81
|
|
|
} |
|
82
|
1 |
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|