cb111be69e0b0077dc94b032cde3841318fcd30d
[yaffs-website] / vendor / chi-teck / drupal-code-generator / src / GeneratorTester.php
1 <?php
2
3 namespace DrupalCodeGenerator;
4
5 use DrupalCodeGenerator\Tests\QuestionHelper;
6 use Symfony\Component\Console\Command\Command;
7 use Symfony\Component\Console\Tester\CommandTester;
8
9 /**
10  * Eases the testing of generator commands.
11  */
12 class GeneratorTester {
13
14   /**
15    * Command to test.
16    *
17    * @var \Symfony\Component\Console\Command\Command
18    */
19   protected $command;
20
21   /**
22    * Command tester.
23    *
24    * @var \Symfony\Component\Console\Tester\CommandTester
25    */
26   protected $commandTester;
27
28   /**
29    * Working directory.
30    *
31    * @var string
32    */
33   protected $directory;
34
35   /**
36    * An associative array representing an interaction with the generator.
37    *
38    * @var array
39    */
40   protected $interaction = [];
41
42   /**
43    * The fixtures.
44    *
45    * @var array
46    */
47   protected $fixtures = [];
48
49   /**
50    * GeneratorTester constructor.
51    */
52   public function __construct(Command $command) {
53     $this->command = $command;
54     $this->commandTester = new CommandTester($this->command);
55
56     $application = ApplicationFactory::create();
57     $helper_set = $application->getHelperSet();
58     $helper_set->set(new QuestionHelper());
59     $application->add($this->command);
60
61     $this->setDirectory(sys_get_temp_dir() . '/dcg_' . uniqid());
62   }
63
64   /**
65    * Getter for the directory.
66    *
67    * @return string
68    *   The directory.
69    */
70   public function getDirectory() {
71     return $this->directory;
72   }
73
74   /**
75    * Setter for the directory.
76    *
77    * @param string $directory
78    *   The directory.
79    */
80   public function setDirectory($directory) {
81     $this->directory = $directory;
82   }
83
84   /**
85    * Getter for the interaction.
86    *
87    * @return array
88    *   The interaction.
89    */
90   public function getInteraction() {
91     return $this->interaction;
92   }
93
94   /**
95    * Setter for the interaction.
96    *
97    * @param array $interaction
98    *   The interaction.
99    */
100   public function setInteraction(array $interaction) {
101     $this->interaction = $interaction;
102   }
103
104   /**
105    * Getter for the fixtures.
106    *
107    * @return array
108    *   The fixtures.
109    */
110   public function getFixtures() {
111     return $this->fixtures;
112   }
113
114   /**
115    * Setter for the fixtures.
116    *
117    * @param array $fixtures
118    *   The fixtures.
119    */
120   public function setFixtures(array $fixtures) {
121     $this->fixtures = $fixtures;
122   }
123
124   /**
125    * Executes the command.
126    *
127    * @return int
128    *   The command exit code
129    */
130   public function execute() {
131     return $this->commandTester
132       ->setInputs(array_values($this->interaction))
133       ->execute(['--directory' => $this->getDirectory()]);
134   }
135
136   /**
137    * Gets the display returned by the last execution of the command.
138    *
139    * @return string
140    *   The display.
141    */
142   public function getDisplay() {
143     return $this->commandTester->getDisplay();
144   }
145
146   /**
147    * Gets expected display.
148    *
149    * @return string
150    *   Expected display.
151    */
152   public function getExpectedDisplay() {
153     $default_name = Utils::machine2human(basename($this->directory));
154
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";
160
161     foreach ($this->interaction as $question => $answer) {
162       $expected_display .= "\n";
163       $expected_display .= " $question\n";
164       $expected_display .= " ➤ \n";
165     }
166
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);
170
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;
178   }
179
180 }