Skip to content

Commit 31530b9

Browse files
committed
ACMS-3639: Refactored DRS tests to remove RoboIo dependency.
1 parent ffa0f76 commit 31530b9

File tree

4 files changed

+229
-14
lines changed

4 files changed

+229
-14
lines changed

src/Robo/Tasks/LoadTasks.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected function taskDrush(): CollectionBuilder {
2424
/** @var \Acquia\Drupal\RecommendedSettings\Robo\Tasks\DrushTask $task */
2525
$task = $this->task($this->drushTaskClass);
2626
/** @var \Symfony\Component\Console\Output\OutputInterface $output */
27-
$output = $this->output();
27+
$output = $this->getOutput();
2828
$task->setVerbosityThreshold($output->getVerbosity());
2929

3030
return $task;

tests/src/CommandsTestBase.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
use Acquia\Drupal\RecommendedSettings\Robo\Config\ConfigAwareTrait;
66
use Acquia\Drupal\RecommendedSettings\Tests\Helpers\NullLogOutputStylers;
7+
use Acquia\Drupal\RecommendedSettings\Tests\Traits\DrsIO;
78
use Consolidation\Log\Logger;
89
use League\Container\Container;
910
use PHPUnit\Framework\TestCase;
1011
use Psr\Container\ContainerInterface;
1112
use Robo\Collection\CollectionBuilder;
1213
use Robo\Common\BuilderAwareTrait;
13-
use Robo\Common\OutputAwareTrait;
1414
use Robo\Config\Config;
1515
use Robo\Robo;
1616
use Robo\Tasks;
@@ -21,7 +21,7 @@
2121
*/
2222
abstract class CommandsTestBase extends TestCase {
2323
use BuilderAwareTrait;
24-
use OutputAwareTrait;
24+
use DrsIO;
2525
use ConfigAwareTrait;
2626

2727
/**
@@ -51,7 +51,7 @@ protected function createContainer(?ContainerInterface $container = NULL): void
5151

5252
$config = new Config();
5353
$this->setConfig($config);
54-
$logger = new Logger($this->output());
54+
$logger = new Logger($this->getOutput());
5555
$null_log_output = new NullLogOutputStylers;
5656
$logger->setLogOutputStyler($null_log_output);
5757
$container->add("logger", $logger);

tests/src/Traits/DrsIO.php

+221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<?php
2+
3+
namespace Acquia\Drupal\RecommendedSettings\Tests\Traits;
4+
5+
use Acquia\Drupal\RecommendedSettings\Common\ArrayManipulator;
6+
use Symfony\Component\Console\Helper\Table;
7+
use Symfony\Component\Console\Output\NullOutput;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
use Symfony\Component\Console\Question\ChoiceQuestion;
10+
use Symfony\Component\Console\Question\Question;
11+
12+
/**
13+
* An extension of \Robo\Common\IO.
14+
*
15+
* @phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
16+
*/
17+
trait DrsIO {
18+
19+
/**
20+
* Writes text to screen, without decoration.
21+
*
22+
* @param string $text
23+
* The text to write.
24+
*/
25+
protected function say($text): void {
26+
$this->writeln($text);
27+
}
28+
29+
/**
30+
* Writes text to screen with big, loud decoration.
31+
*
32+
* @param string $text
33+
* The text to write.
34+
* @param int $length
35+
* The length at which text should be wrapped.
36+
* @param string $color
37+
* The color of the text.
38+
*/
39+
protected function yell($text, $length = 40, $color = 'green'): void {
40+
$format = "<fg=white;bg=$color;options=bold>%s</fg=white;bg=$color;options=bold>";
41+
$this->formattedOutput($text, $length, $format);
42+
}
43+
44+
/**
45+
* Prints the message to terminal.
46+
*
47+
* @param string $message
48+
* Given message to print.
49+
* @param string $type
50+
* Type of message. Ex: error, success, info etc.
51+
* @param string $color
52+
* Given background color.
53+
*/
54+
protected function print(string $message, string $type = "success", string $color = ""): void {
55+
if (!$color) {
56+
$color = match ($type) {
57+
"warning" => "yellow",
58+
"notice" => "cyan",
59+
"error" => "red",
60+
default => "green",
61+
};
62+
}
63+
$message = " <fg=white;bg=$color;options=bold>[$type]</fg=white;bg=$color;options=bold> " . $message;
64+
$this->say($message);
65+
}
66+
67+
/**
68+
* Format text as a question.
69+
*
70+
* @param string $message
71+
* The question text.
72+
*
73+
* @return string
74+
* The formatted question text.
75+
*/
76+
protected function formatQuestion($message): string {
77+
return "<question> $message</question> ";
78+
}
79+
80+
/**
81+
* Asks the user a multiple-choice question.
82+
*
83+
* @param string $question
84+
* The question text.
85+
* @param string[] $options
86+
* An array of available options.
87+
* @param mixed $default
88+
* Default.
89+
*
90+
* @return string
91+
* The chosen option.
92+
*/
93+
protected function askChoice(string $question, array $options, mixed $default = NULL): string {
94+
return $this->doAsk(new ChoiceQuestion($this->formatQuestion($question),
95+
$options, $default));
96+
}
97+
98+
/**
99+
* Asks a required question.
100+
*
101+
* @param string $message
102+
* The question text.
103+
*
104+
* @return string
105+
* The response.
106+
*/
107+
protected function askRequired(string $message): string {
108+
$question = new Question($this->formatQuestion($message));
109+
$question->setValidator(function ($answer) {
110+
if (empty($answer)) {
111+
throw new \RuntimeException(
112+
'You must enter a value!'
113+
);
114+
}
115+
116+
return $answer;
117+
});
118+
return $this->doAsk($question);
119+
}
120+
121+
/**
122+
* Writes an array to the screen as a formatted table.
123+
*
124+
* @param string[] $array
125+
* The unformatted array.
126+
* @param string[] $headers
127+
* The headers for the array. Defaults to ['Property','Value'].
128+
*/
129+
protected function printArrayAsTable(
130+
array $array,
131+
array $headers = ['Property', 'Value']
132+
): void {
133+
$table = new Table($this->output);
134+
$table->setHeaders($headers)
135+
->setRows(ArrayManipulator::convertArrayToFlatTextArray($array))
136+
->render();
137+
}
138+
139+
/**
140+
* Writes a particular configuration key's value to the log.
141+
*
142+
* @param string[] $array
143+
* The configuration.
144+
* @param string $prefix
145+
* A prefix to add to each row in the configuration.
146+
* @param int $verbosity
147+
* The verbosity level at which to display the logged message.
148+
*/
149+
protected function logConfig(array $array, string $prefix = '', int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): void {
150+
if ($this->getOutput()->getVerbosity() >= $verbosity) {
151+
if ($prefix) {
152+
$this->getOutput()->writeln("<comment>Configuration for $prefix:</comment>");
153+
foreach ($array as $key => $value) {
154+
$array["$prefix.$key"] = $value;
155+
unset($array[$key]);
156+
}
157+
}
158+
$this->printArrayAsTable($array);
159+
}
160+
}
161+
162+
/**
163+
* Asks a required question.
164+
*
165+
* @param string $text
166+
* Prints the output text.
167+
*/
168+
protected function writeln($text): void {
169+
$this->getOutput()->writeln($text);
170+
}
171+
172+
/**
173+
* Formats the output message to terminal.
174+
*
175+
* @param string $text
176+
* Output message to format.
177+
* @param int $length
178+
* Length of the output.
179+
* @param string $format
180+
* Given string format.
181+
*/
182+
protected function formattedOutput($text, $length, $format): void {
183+
$lines = explode("\n", trim($text, "\n"));
184+
$maxLineLength = array_reduce(array_map('strlen', $lines), 'max');
185+
$length = max($length, $maxLineLength);
186+
$len = $length + 2;
187+
$space = str_repeat(' ', $len);
188+
$this->writeln(sprintf($format, $space));
189+
foreach ($lines as $line) {
190+
$line = str_pad($line, $length, ' ', STR_PAD_BOTH);
191+
$this->writeln(sprintf($format, " $line "));
192+
}
193+
$this->writeln(sprintf($format, $space));
194+
}
195+
196+
/**
197+
*
198+
* @return $this
199+
*
200+
* @see \Robo\Contract\OutputAwareInterface::setOutput()
201+
*/
202+
public function setOutput(OutputInterface $output) {
203+
$this->output = $output;
204+
205+
return $this;
206+
}
207+
208+
/**
209+
*
210+
* @return $this
211+
*
212+
* @see \Robo\Contract\OutputAwareInterface::getOutput()
213+
*/
214+
public function getOutput(): OutputInterface {
215+
if (!isset($this->output)) {
216+
$this->setOutput(new NullOutput());
217+
}
218+
return $this->output;
219+
}
220+
221+
}

tests/src/unit/Common/IOTest.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace Acquia\Drupal\RecommendedSettings\Tests\Unit\Common;
44

5-
use Acquia\Drupal\RecommendedSettings\Common\IO;
5+
use Acquia\Drupal\RecommendedSettings\Tests\Traits\DrsIO;
66
use PHPUnit\Framework\TestCase;
7-
use Robo\Common\IO as RoboIO;
87
use Symfony\Component\Console\Input\InputInterface;
98
use Symfony\Component\Console\Output\OutputInterface;
109
use Symfony\Component\Console\Question\Question;
@@ -15,12 +14,7 @@
1514
* @covers \Acquia\Drupal\RecommendedSettings\Common\IO
1615
*/
1716
class IOTest extends TestCase {
18-
19-
use IO, RoboIO {
20-
IO::say insteadof RoboIO;
21-
IO::formatQuestion insteadof RoboIO;
22-
IO::yell insteadof RoboIO;
23-
}
17+
use DrsIO;
2418

2519
/**
2620
* Stores the messages to print.
@@ -46,7 +40,7 @@ protected function setUp(): void {
4640
parent::setUp();
4741
$this->print = [];
4842
$this->answer = "";
49-
$this->output = $this->output();
43+
$this->output = $this->getOutput();
5044
}
5145

5246
/**
@@ -239,7 +233,7 @@ public function testLogConfig(): void {
239233
/**
240234
* Returns the mocked output object.
241235
*/
242-
protected function output(): OutputInterface {
236+
protected function getOutput(): OutputInterface {
243237
$output = $this->createMock(OutputInterface::class);
244238
$output->method("writeln")->willReturnCallback(fn ($input) => $this->mockPrint($input));
245239
$output->method("getVerbosity")->willReturn(OutputInterface::VERBOSITY_VERY_VERBOSE);

0 commit comments

Comments
 (0)