forked from Chi-teck/drupal-code-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratorTestBase.php
More file actions
88 lines (71 loc) · 2.25 KB
/
GeneratorTestBase.php
File metadata and controls
88 lines (71 loc) · 2.25 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
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Test\Functional;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
/**
* Base class for generator tests.
*/
abstract class GeneratorTestBase extends FunctionalTestBase {
/**
* Display returned by the last execution of the command.
*/
protected string $display;
/**
* A directory to look up for fixtures.
*/
protected string $fixtureDir;
/**
* Executes the command.
*
* @psalm-param class-string $command_class
* A command class to instantiate generator.
* @psalm-param list<string> $user_input
* An array of strings representing each input passed to the command input
* stream.
*
* @psalm-return int<0, 1>
*/
protected function execute(string $command_class, array $user_input): int {
$application = self::createApplication();
$command = $application
->getContainer()
->get('class_resolver')
->getInstanceFromDefinition($command_class);
\assert($command instanceof Command);
$application->addCommand($command);
$command_tester = new CommandTester($command);
/** @psalm-var int<0, 1> $result */
$result = $command_tester
->setInputs($user_input)
->execute(['--destination' => $this->directory]);
$this->display = $command_tester->getDisplay();
return $result;
}
/**
* Asserts generated display.
*/
protected function assertDisplay(string $expected_display): void {
self::assertEquals($expected_display . \PHP_EOL, $this->display);
}
/**
* Asserts generated file.
*/
protected function assertGeneratedFile(string $file, ?string $fixture = NULL): void {
// Fixture name typically is the same as the generated file name.
$fixture ??= $file;
self::assertFileEquals($this->fixtureDir . '/' . $fixture, $this->directory . '/' . $file);
}
/**
* Asserts generated file.
*/
protected function assertGeneratedDirectory(string $directory): void {
self::assertDirectoryExists($this->directory . '/' . $directory);
}
/**
* Returns contents of the generated file.
*/
protected function getGeneratedContent(string $file): string {
return \file_get_contents($this->directory . '/' . $file);
}
}