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