Skip to content

Commit

Permalink
Merge pull request #58 from acquia/ACMS-3639-1
Browse files Browse the repository at this point in the history
ACMS-3639: Refactored DRS tests to remove RoboIo dependency.
  • Loading branch information
vishalkhode1 authored May 21, 2024
2 parents 2877658 + b02403d commit 034bde5
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 37 deletions.
38 changes: 19 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
cacheResultFile="./var/phpunit/test-results"
bootstrap="vendor/autoload.php"
failOnWarning="true"
failOnRisky="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
failOnDeprecation="true"
failOnNotice="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerNotices="true"
cacheDirectory=".phpunit.cache"
>

<php>
<ini name="error_reporting" value="-1"/>
<env name="COLUMNS" value="80" force="true" />
<!-- <env name="ORCA_FIXTURE_DIR" value="/Applications/MAMP/htdocs/acquia_cms" force="true" />-->
<env name="COLUMNS" value="80" force="true"/>
<!-- <env name="ORCA_FIXTURE_DIR" value="/Applications/MAMP/htdocs/acquia_cms" force="true" />-->
</php>

<testsuites>
<testsuite name="DRS">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory="var/phpunit/coverage-cache"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<!-- <coverage cacheDirectory="var/phpunit/coverage-cache" includeUncoveredFiles="true">-->
<!-- <report>-->
<!-- <html outputDirectory="coverage" lowUpperBound="50" highLowerBound="90"/>-->
<!-- </report>-->
</coverage>
<!-- <logging>-->
<!-- <testdoxHtml outputFile="testdox.html"/>-->
<!-- </logging>-->
<!-- </coverage>-->
<!-- <logging>-->
<!-- <testdoxHtml outputFile="testdox.html"/>-->
<!-- </logging>-->
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
2 changes: 2 additions & 0 deletions settings/site/default.local.settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@
@ini_set('zend.assertions', 1);
}
else {
// phpcs:disable
assert_options(ASSERT_ACTIVE, TRUE);
assert_options(ASSERT_EXCEPTION, TRUE);
// phpcs:enable
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/Common/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait IO {
/**
* Writes text to screen, without decoration.
*
* @param string $text
* @param string|iterable[string] $text
* The text to write.
*/
protected function say($text): void {
Expand Down Expand Up @@ -145,9 +145,12 @@ protected function printArrayAsTable(
* The verbosity level at which to display the logged message.
*/
protected function logConfig(array $array, string $prefix = '', int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): void {
if ($this->output()->getVerbosity() >= $verbosity) {
// We can't directly use $this->output() as it throws error with PHPUnit 10
// because it defines output() method with a different method signature.
$output = ($this->output() instanceof OutputInterface) ? $this->output() : $this->getOutput();
if ($output->getVerbosity() >= $verbosity) {
if ($prefix) {
$this->output()->writeln("<comment>Configuration for $prefix:</comment>");
$output->writeln("<comment>Configuration for $prefix:</comment>");
foreach ($array as $key => $value) {
$array["$prefix.$key"] = $value;
unset($array[$key]);
Expand Down
2 changes: 0 additions & 2 deletions src/Drush/Commands/MultisiteDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
use Consolidation\AnnotatedCommand\Hooks\HookManager;
use Drupal\Core\Database\Database;
use Drush\Attributes as CLI;
use Drush\Boot\BootstrapManager;
use Drush\Boot\DrupalBootLevels;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Psr\Container\ContainerInterface as DrushContainer;

/**
* A Drush command to generate settings.php for Multisite.
Expand Down
6 changes: 5 additions & 1 deletion src/Robo/Tasks/LoadTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Acquia\Drupal\RecommendedSettings\Robo\Tasks;

use Robo\Collection\CollectionBuilder;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Load Settings's custom Robo tasks.
Expand All @@ -23,8 +24,11 @@ trait LoadTasks {
protected function taskDrush(): CollectionBuilder {
/** @var \Acquia\Drupal\RecommendedSettings\Robo\Tasks\DrushTask $task */
$task = $this->task($this->drushTaskClass);

// We can't directly use $this->output() as it throws error with PHPUnit 10
// because it defines output() method with a different method signature.
/** @var \Symfony\Component\Console\Output\OutputInterface $output */
$output = $this->output();
$output = ($this->output() instanceof OutputInterface) ? $this->output() : $this->getOutput();
$task->setVerbosityThreshold($output->getVerbosity());

return $task;
Expand Down
4 changes: 2 additions & 2 deletions tests/src/CommandsTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use Acquia\Drupal\RecommendedSettings\Robo\Config\ConfigAwareTrait;
use Acquia\Drupal\RecommendedSettings\Tests\Helpers\NullLogOutputStylers;
use Acquia\Drupal\RecommendedSettings\Tests\Traits\OutputAwareTrait;
use Consolidation\Log\Logger;
use League\Container\Container;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Robo\Collection\CollectionBuilder;
use Robo\Common\BuilderAwareTrait;
use Robo\Common\OutputAwareTrait;
use Robo\Config\Config;
use Robo\Robo;
use Robo\Tasks;
Expand Down Expand Up @@ -51,7 +51,7 @@ protected function createContainer(?ContainerInterface $container = NULL): void

$config = new Config();
$this->setConfig($config);
$logger = new Logger($this->output());
$logger = new Logger($this->getOutput());
$null_log_output = new NullLogOutputStylers;
$logger->setLogOutputStyler($null_log_output);
$container->add("logger", $logger);
Expand Down
51 changes: 51 additions & 0 deletions tests/src/Traits/DrsIO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Acquia\Drupal\RecommendedSettings\Tests\Traits;

use Robo\Common\InputAwareTrait;

/**
* An extension of \Robo\Common\IO.
*/
trait DrsIO {

use OutputAwareTrait;
use InputAwareTrait;

/**
* Writes the text to screen.
*
* @param string|iterable[string] $text
* Prints the output text.
*/
protected function writeln(string|iterable $text): void {
$this->getOutput()->writeln($text);
}

/**
* Formats the output message to terminal.
*
* @param string $text
* Output message to format.
* @param int $length
* Length of the output.
* @param string $format
* Given string format.
*
* @see \Robo\Common\IO::formattedOutput()
*/
protected function formattedOutput(string $text, int $length, string $format): void {
$lines = explode("\n", trim($text, "\n"));
$maxLineLength = array_reduce(array_map('strlen', $lines), 'max');
$length = max($length, $maxLineLength);
$len = $length + 2;
$space = str_repeat(' ', $len);
$this->writeln(sprintf($format, $space));
foreach ($lines as $line) {
$line = str_pad($line, $length, ' ', STR_PAD_BOTH);
$this->writeln(sprintf($format, " $line "));
}
$this->writeln(sprintf($format, $space));
}

}
74 changes: 74 additions & 0 deletions tests/src/Traits/OutputAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Acquia\Drupal\RecommendedSettings\Tests\Traits;

use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Custom OutputAwareTrait defined by the DRS without defining
* method output() as it throws error with PHPUnit 10.
*
* @see \Robo\Common\OutputAwareTrait
*/
trait OutputAwareTrait {

/**
* Holds Console output object.
*/
protected OutputInterface $output;

/**
*
* @return $this
*
* @see \Robo\Contract\OutputAwareInterface::setOutput()
*/
public function setOutput(OutputInterface $output): static {
$this->output = $output;
return $this;
}

/**
*/
protected function stderr(): OutputInterface {
$output = $this->getOutput();
if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput();
}
return $output;
}

/**
* Returns an instance of OutputInterface object.
* This method is deprecated in Robo IO.php, hence defined here.
*/
protected function getOutput(): OutputInterface {
if (!isset($this->output)) {
$this->setOutput(new NullOutput());
}
return $this->output;
}

/**
* We are using our own IO (DrsIO) instead of the IO trait provided by Robo.
* The PHPUnit 9 doesn't have output() method, so when DrsIO's logConfig
* method calls output(), it throws a "Call to undefined method" error.
* We are handling that case here.
*
* @todo: Remove the method below once support for the PHPUnit 9 is dropped.
*
* @param string name
* The method name to call.
* @param array<string> $arguments
* An array of arguments to pass to method.
*/
public function __call(string $name, array $arguments): OutputInterface {
if ($name == "output") {
return $this->getOutput();
}
throw new \BadMethodCallException("Call to undefined method " . __NAMESPACE__ . "::$name()");
}

}
15 changes: 5 additions & 10 deletions tests/src/unit/Common/IOTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Acquia\Drupal\RecommendedSettings\Tests\Unit\Common;
namespace Acquia\Drupal\RecommendedSettings\Tests\unit\Common;

use Acquia\Drupal\RecommendedSettings\Common\IO;
use Acquia\Drupal\RecommendedSettings\Tests\Traits\DrsIO;
use PHPUnit\Framework\TestCase;
use Robo\Common\IO as RoboIO;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
Expand All @@ -15,12 +15,7 @@
* @covers \Acquia\Drupal\RecommendedSettings\Common\IO
*/
class IOTest extends TestCase {

use IO, RoboIO {
IO::say insteadof RoboIO;
IO::formatQuestion insteadof RoboIO;
IO::yell insteadof RoboIO;
}
use IO, DrsIO;

/**
* Stores the messages to print.
Expand All @@ -46,7 +41,7 @@ protected function setUp(): void {
parent::setUp();
$this->print = [];
$this->answer = "";
$this->output = $this->output();
$this->output = $this->getOutput();
}

/**
Expand Down Expand Up @@ -239,7 +234,7 @@ public function testLogConfig(): void {
/**
* Returns the mocked output object.
*/
protected function output(): OutputInterface {
protected function getOutput(): OutputInterface {
$output = $this->createMock(OutputInterface::class);
$output->method("writeln")->willReturnCallback(fn ($input) => $this->mockPrint($input));
$output->method("getVerbosity")->willReturn(OutputInterface::VERBOSITY_VERY_VERBOSE);
Expand Down

0 comments on commit 034bde5

Please sign in to comment.