Passed
Push — master ( 11230f...5f5cdb )
by Jakub
12:32
created

MyTesterExtension::getConfigSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 7
c 3
b 0
f 0
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
    }
58
59 1
    public function afterCompile(\Nette\PhpGenerator\ClassType $class): void
0 ignored issues
show
introduced by
The method parameter $class is never used
Loading history...
60
    {
61 1
        $config = $this->getConfig();
62 1
        $this->initialization->addBody('$runner = $this->getService(?);', [$this->prefix(static::SERVICE_RUNNER)]);
63 1
        $this->initialization->addBody('$runner->useColors = ?;', [$config["colors"]]);
64 1
        $this->writeRunnerEventHandlers("onExecute", $config["onExecute"]);
65 1
        $this->writeRunnerEventHandlers("onFinish", $config["onFinish"]);
66
    }
67
68 1
    private function writeRunnerEventHandlers(string $eventName, array $callbacks): void
69
    {
70 1
        foreach ($callbacks as &$task) {
71
            if (!is_array($task)) {
72
                $task = explode("::", $task);
73
            } elseif (str_starts_with($task[0], "@")) {
74
                $className = substr($task[0], 1);
75
                $this->initialization->addBody('$runner->' . $eventName . '[] = [$this->getService(?), ?];', [$className, $task[1]]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 133 characters
Loading history...
76
                continue;
77
            }
78
            $this->initialization->addBody('$runner->' . $eventName . '[] = [?, ?];', [$task[0], $task[1]]);
79
        }
80
    }
81
}
82