3 namespace DrupalCodeGenerator;
5 use DrupalCodeGenerator\Tests\QuestionHelper;
6 use Symfony\Component\Console\Command\Command;
7 use Symfony\Component\Console\Tester\CommandTester;
10 * Eases the testing of generator commands.
12 class GeneratorTester {
17 * @var \Symfony\Component\Console\Command\Command
24 * @var \Symfony\Component\Console\Tester\CommandTester
26 protected $commandTester;
36 * An associative array representing an interaction with the generator.
40 protected $interaction = [];
47 protected $fixtures = [];
50 * GeneratorTester constructor.
52 public function __construct(Command $command) {
53 $this->command = $command;
54 $this->commandTester = new CommandTester($this->command);
56 $application = ApplicationFactory::create();
57 $helper_set = $application->getHelperSet();
58 $helper_set->set(new QuestionHelper());
59 $application->add($this->command);
61 $this->setDirectory(sys_get_temp_dir() . '/dcg_' . uniqid());
65 * Getter for the directory.
70 public function getDirectory() {
71 return $this->directory;
75 * Setter for the directory.
77 * @param string $directory
80 public function setDirectory($directory) {
81 $this->directory = $directory;
85 * Getter for the interaction.
90 public function getInteraction() {
91 return $this->interaction;
95 * Setter for the interaction.
97 * @param array $interaction
100 public function setInteraction(array $interaction) {
101 $this->interaction = $interaction;
105 * Getter for the fixtures.
110 public function getFixtures() {
111 return $this->fixtures;
115 * Setter for the fixtures.
117 * @param array $fixtures
120 public function setFixtures(array $fixtures) {
121 $this->fixtures = $fixtures;
125 * Executes the command.
128 * The command exit code
130 public function execute() {
131 return $this->commandTester
132 ->setInputs(array_values($this->interaction))
133 ->execute(['--directory' => $this->getDirectory()]);
137 * Gets the display returned by the last execution of the command.
142 public function getDisplay() {
143 return $this->commandTester->getDisplay();
147 * Gets expected display.
152 public function getExpectedDisplay() {
153 $default_name = Utils::machine2human(basename($this->directory));
155 $expected_display = "\n";
156 $name = $this->command->getName();
157 $title = "Welcome to $name generator!";
158 $expected_display .= " $title\n";
159 $expected_display .= str_repeat('–', strlen($title) + 2) . "\n";
161 foreach ($this->interaction as $question => $answer) {
162 $expected_display .= "\n";
163 $expected_display .= " $question\n";
164 $expected_display .= " ➤ \n";
167 $expected_display = str_replace('%default_name%', $default_name, $expected_display);
168 $default_machine_name = Utils::human2machine(basename($this->directory));
169 $expected_display = str_replace('%default_machine_name%', $default_machine_name, $expected_display);
171 $targets = implode("\n • ", array_keys($this->fixtures));
172 $expected_display .= "\n";
173 $expected_display .= " The following directories and files have been created or updated:\n";
174 $expected_display .= "–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––\n";
175 $expected_display .= " • $targets\n";
176 $expected_display .= "\n";
177 return $expected_display;