-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathGeneratorTest.php
More file actions
118 lines (97 loc) · 3.15 KB
/
GeneratorTest.php
File metadata and controls
118 lines (97 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Test;
use DrupalCodeGenerator\Application;
use DrupalCodeGenerator\Helper\Renderer\TwigRenderer;
use DrupalCodeGenerator\Twig\TwigEnvironment;
use DrupalCodeGenerator\Utils;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Filesystem\Filesystem;
use Twig\Loader\FilesystemLoader;
/**
* Base class for generator tests.
*/
abstract class GeneratorTest extends TestCase {
/**
* Display returned by the last execution of the command.
*/
protected string $display;
/**
* A directory to look up for fixtures.
*/
protected string $fixtureDir;
/**
* Working directory.
*/
private string $directory;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
$this->directory = \sys_get_temp_dir() . '/dcg_sandbox';
}
/**
* {@inheritdoc}
*/
protected function tearDown(): void {
(new Filesystem())->remove($this->directory);
}
/**
* Executes the command.
*
* @psalm-param \Symfony\Component\Console\Command\Command $command
* A command to execute.
* @psalm-param list<string> $user_input
* An array of strings representing each input passed to the command input
* stream.
*/
protected function execute(Command $command, array $user_input): int {
$this->createApplication()->addCommand($command);
$command_tester = new CommandTester($command);
$result = $command_tester
->setInputs($user_input)
->execute(['--destination' => $this->directory, '--working-dir' => $this->directory]);
$this->display = $command_tester->getDisplay();
return $result;
}
/**
* Asserts generated display.
*/
protected function assertDisplay(string $expected_display): void {
$default_name = Utils::machine2human(\basename($this->directory), TRUE);
$expected_display = \str_replace('%default_name%', $default_name, $expected_display);
self::assertEquals($expected_display, $this->display);
}
/**
* Asserts generated file.
*/
protected function assertGeneratedFile(string $file, string $fixture): void {
self::assertFileEquals($this->fixtureDir . '/' . $fixture, $this->directory . '/' . $file);
}
/**
* Asserts generated file.
*/
protected function assertGeneratedDirectory(string $directory): void {
self::assertDirectoryExists($this->directory . '/' . $directory);
}
/**
* Creates DCG application.
*
* @psalm-suppress UndefinedClass
* @psalm-suppress TooFewArguments
* @psalm-suppress InvalidArgument
* @todo Fix this.
*/
protected function createApplication(): Application {
$application = Application::create();
$helper_set = $application->getHelperSet();
// Replace default question helper to ease parsing output.
$helper_set->set(new QuestionHelper());
// Replace default renderer to enable 'strict_variables' in tests.
$twig_environment = new TwigEnvironment(new FilesystemLoader([Application::TEMPLATE_PATH]), ['strict_variables' => TRUE]);
$helper_set->set(new TwigRenderer($twig_environment));
return $application;
}
}