annotate vendor/chi-teck/drupal-code-generator/src/GeneratorTester.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace DrupalCodeGenerator;
Chris@0 4
Chris@0 5 use DrupalCodeGenerator\Tests\QuestionHelper;
Chris@0 6 use Symfony\Component\Console\Command\Command;
Chris@0 7 use Symfony\Component\Console\Tester\CommandTester;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Eases the testing of generator commands.
Chris@0 11 */
Chris@0 12 class GeneratorTester {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Command to test.
Chris@0 16 *
Chris@0 17 * @var \Symfony\Component\Console\Command\Command
Chris@0 18 */
Chris@0 19 protected $command;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Command tester.
Chris@0 23 *
Chris@0 24 * @var \Symfony\Component\Console\Tester\CommandTester
Chris@0 25 */
Chris@0 26 protected $commandTester;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Working directory.
Chris@0 30 *
Chris@0 31 * @var string
Chris@0 32 */
Chris@0 33 protected $directory;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * An associative array representing an interaction with the generator.
Chris@0 37 *
Chris@0 38 * @var array
Chris@0 39 */
Chris@0 40 protected $interaction = [];
Chris@0 41
Chris@0 42 /**
Chris@0 43 * The fixtures.
Chris@0 44 *
Chris@0 45 * @var array
Chris@0 46 */
Chris@0 47 protected $fixtures = [];
Chris@0 48
Chris@0 49 /**
Chris@0 50 * GeneratorTester constructor.
Chris@0 51 */
Chris@0 52 public function __construct(Command $command) {
Chris@0 53 $this->command = $command;
Chris@0 54 $this->commandTester = new CommandTester($this->command);
Chris@0 55
Chris@0 56 $application = ApplicationFactory::create();
Chris@0 57 $helper_set = $application->getHelperSet();
Chris@0 58 $helper_set->set(new QuestionHelper());
Chris@0 59 $application->add($this->command);
Chris@0 60
Chris@0 61 $this->setDirectory(sys_get_temp_dir() . '/dcg_' . uniqid());
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Getter for the directory.
Chris@0 66 *
Chris@0 67 * @return string
Chris@0 68 * The directory.
Chris@0 69 */
Chris@0 70 public function getDirectory() {
Chris@0 71 return $this->directory;
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * Setter for the directory.
Chris@0 76 *
Chris@0 77 * @param string $directory
Chris@0 78 * The directory.
Chris@0 79 */
Chris@0 80 public function setDirectory($directory) {
Chris@0 81 $this->directory = $directory;
Chris@0 82 }
Chris@0 83
Chris@0 84 /**
Chris@0 85 * Getter for the interaction.
Chris@0 86 *
Chris@0 87 * @return array
Chris@0 88 * The interaction.
Chris@0 89 */
Chris@0 90 public function getInteraction() {
Chris@0 91 return $this->interaction;
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * Setter for the interaction.
Chris@0 96 *
Chris@0 97 * @param array $interaction
Chris@0 98 * The interaction.
Chris@0 99 */
Chris@0 100 public function setInteraction(array $interaction) {
Chris@0 101 $this->interaction = $interaction;
Chris@0 102 }
Chris@0 103
Chris@0 104 /**
Chris@0 105 * Getter for the fixtures.
Chris@0 106 *
Chris@0 107 * @return array
Chris@0 108 * The fixtures.
Chris@0 109 */
Chris@0 110 public function getFixtures() {
Chris@0 111 return $this->fixtures;
Chris@0 112 }
Chris@0 113
Chris@0 114 /**
Chris@0 115 * Setter for the fixtures.
Chris@0 116 *
Chris@0 117 * @param array $fixtures
Chris@0 118 * The fixtures.
Chris@0 119 */
Chris@0 120 public function setFixtures(array $fixtures) {
Chris@0 121 $this->fixtures = $fixtures;
Chris@0 122 }
Chris@0 123
Chris@0 124 /**
Chris@0 125 * Executes the command.
Chris@0 126 *
Chris@0 127 * @return int
Chris@0 128 * The command exit code
Chris@0 129 */
Chris@0 130 public function execute() {
Chris@0 131 return $this->commandTester
Chris@0 132 ->setInputs(array_values($this->interaction))
Chris@0 133 ->execute(['--directory' => $this->getDirectory()]);
Chris@0 134 }
Chris@0 135
Chris@0 136 /**
Chris@0 137 * Gets the display returned by the last execution of the command.
Chris@0 138 *
Chris@0 139 * @return string
Chris@0 140 * The display.
Chris@0 141 */
Chris@0 142 public function getDisplay() {
Chris@0 143 return $this->commandTester->getDisplay();
Chris@0 144 }
Chris@0 145
Chris@0 146 /**
Chris@0 147 * Gets expected display.
Chris@0 148 *
Chris@0 149 * @return string
Chris@0 150 * Expected display.
Chris@0 151 */
Chris@0 152 public function getExpectedDisplay() {
Chris@0 153 $default_name = Utils::machine2human(basename($this->directory));
Chris@0 154
Chris@0 155 $expected_display = "\n";
Chris@0 156 $name = $this->command->getName();
Chris@0 157 $title = "Welcome to $name generator!";
Chris@0 158 $expected_display .= " $title\n";
Chris@0 159 $expected_display .= str_repeat('–', strlen($title) + 2) . "\n";
Chris@0 160
Chris@0 161 foreach ($this->interaction as $question => $answer) {
Chris@4 162 $question = preg_replace('/^<\d*> /', '', $question);
Chris@0 163 $expected_display .= "\n";
Chris@0 164 $expected_display .= " $question\n";
Chris@4 165 // Regular question.
Chris@4 166 if (strpos($question, "\n") === FALSE) {
Chris@4 167 $expected_display .= " ➤ \n";
Chris@4 168 }
Chris@4 169 // Choice question.
Chris@4 170 else {
Chris@4 171 $expected_display .= " ➤➤➤ \n";
Chris@4 172 }
Chris@0 173 }
Chris@0 174
Chris@0 175 $expected_display = str_replace('%default_name%', $default_name, $expected_display);
Chris@0 176 $default_machine_name = Utils::human2machine(basename($this->directory));
Chris@0 177 $expected_display = str_replace('%default_machine_name%', $default_machine_name, $expected_display);
Chris@0 178
Chris@0 179 $targets = implode("\n • ", array_keys($this->fixtures));
Chris@0 180 $expected_display .= "\n";
Chris@0 181 $expected_display .= " The following directories and files have been created or updated:\n";
Chris@0 182 $expected_display .= "–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––\n";
Chris@0 183 $expected_display .= " • $targets\n";
Chris@0 184 $expected_display .= "\n";
Chris@0 185 return $expected_display;
Chris@0 186 }
Chris@0 187
Chris@0 188 }