Skip to content

Commit

Permalink
ACMS-3658: More PHPUnit tests added.
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalkhode1 committed Mar 26, 2024
1 parent 8fda0c9 commit 24da387
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 25 deletions.
45 changes: 21 additions & 24 deletions src/Robo/Tasks/DrushTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DrushTask extends CommandStack {
/**
* Indicates if the command output should be verbose.
*/
protected bool $verbose = FALSE;
protected bool|int $verbose = FALSE;

/**
* Indicates if the command output should be very verbose.
Expand All @@ -71,7 +71,7 @@ class DrushTask extends CommandStack {
/**
* Add or not the --ansi option.
*/
protected bool $ansi = FALSE;
protected bool|NULL $ansi = NULL;

/**
* Drush commands to execute when task is run.
Expand All @@ -90,24 +90,24 @@ class DrushTask extends CommandStack {
/**
* Adds the given drush command to a stack.
*
* @param string $command
* @param string|array<string> $commands
* The drush command to execute. Do NOT include "drush" prefix.
*
* @return $this
*/
public function drush(string $command) {
public function drush(string|array $commands) {
// Clear out options associated with previous drush command.
$this->setOptionsForLastCommand();

if (!$this->defaultsInitialized) {
$this->init();
}

if (is_array($command)) {
$command = implode(' ', array_filter($command));
if (is_array($commands)) {
$commands = implode(' ', array_filter($commands));
}

$this->commands[] = trim($command);
$this->commands[] = trim($commands);
return $this;
}

Expand Down Expand Up @@ -156,7 +156,7 @@ public function dir($dir) {
*
* @return $this
*/
public function verbose(bool $verbose) {
public function verbose(string|bool|int $verbose) {
$this->verbose = $this->mixedToBool($verbose);
return $this;
}
Expand All @@ -169,7 +169,7 @@ public function verbose(bool $verbose) {
*
* @return $this
*/
public function veryVerbose(bool $verbose) {
public function veryVerbose(string|bool $verbose) {
$this->veryVerbose = $this->mixedToBool($verbose);
return $this;
}
Expand All @@ -182,7 +182,7 @@ public function veryVerbose(bool $verbose) {
*
* @return $this
*/
public function debug(bool $verbose) {
public function debug(string|bool $verbose) {
$this->debug = $this->mixedToBool($verbose);
return $this;
}
Expand All @@ -203,12 +203,12 @@ public function includePath(string $path) {
/**
* Include or not the --ansi option for drush commands.
*
* @param bool $ansi
* @param string|bool $ansi
* The flag for including --ansi option.
*
* @return $this
*/
public function ansi(bool $ansi) {
public function ansi(string|bool $ansi) {
$this->ansi = $ansi;
return $this;
}
Expand Down Expand Up @@ -237,7 +237,7 @@ protected function init(): void {
$this->interactive(FALSE);
}
if (!isset($this->ansi)) {
$this->ansi($this->getConfig()->get('drush.ansi'));
$this->ansi($this->getConfig()->get('drush.ansi', FALSE));
}

$this->defaultsInitialized = TRUE;
Expand All @@ -249,14 +249,17 @@ protected function init(): void {
* @param mixed $mixedVar
* Mixed.
*
* @return bool
* @return int|bool
* TRUE/FALSE as per PHP's cast to boolean ruleset, with the exception that
* a string value not equal to 'yes' or 'true' will evaluate to FALSE.
*/
protected function mixedToBool(mixed $mixedVar): bool {
protected function mixedToBool(mixed $mixedVar): int|bool {
if (is_string($mixedVar)) {
$boolVar = ($mixedVar === 'yes' || $mixedVar === 'true');
}
elseif (is_int($mixedVar)) {
$boolVar = $mixedVar;
}
else {
$boolVar = (bool) $mixedVar;
}
Expand Down Expand Up @@ -290,8 +293,8 @@ protected function setGlobalOptions(): void {
$this->option('no-interaction');
}

if ($this->verbose !== FALSE) {
$verbosity_threshold = $this->verbosityThreshold();
if ($this->verbose !== FALSE || ($this->verbosityThreshold() >= OutputInterface::VERBOSITY_VERBOSE)) {
$verbosity_threshold = ($this->verbose !== FALSE) ? $this->verbose : $this->verbosityThreshold();
switch ($verbosity_threshold) {
case OutputInterface::VERBOSITY_VERBOSE:
$this->verbose(TRUE);
Expand All @@ -304,12 +307,9 @@ protected function setGlobalOptions(): void {
case OutputInterface::VERBOSITY_DEBUG:
$this->debug(TRUE);
break;

}
}
if ($this->verbosityThreshold() >= OutputInterface::VERBOSITY_VERBOSE
&& $this->verbose !== FALSE) {
$this->verbose(TRUE);
}

if (($this->debug || $this->getConfig()->get('drush.debug'))
&& $this->getConfig()->get('drush.debug') !== FALSE) {
Expand Down Expand Up @@ -359,9 +359,6 @@ public function option(string $option, string $value = NULL, string $separator =
*/
public function run() {
$this->setupExecution();
if (empty($this->exec)) {
throw new TaskException($this, 'You must add at least one command');
}

// If 'stopOnFail' is not set, or if there is only one command to run,
// then execute the single command to run.
Expand Down
7 changes: 6 additions & 1 deletion src/Robo/Tasks/LoadTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
trait LoadTasks {

/**
* An instance of drush task class.
*/
protected string $drushTaskClass = DrushTask::class;

/**
* Task drush.
*
Expand All @@ -17,7 +22,7 @@ trait LoadTasks {
*/
protected function taskDrush(): CollectionBuilder {
/** @var \Acquia\Drupal\RecommendedSettings\Robo\Tasks\DrushTask $task */
$task = $this->task(DrushTask::class);
$task = $this->task($this->drushTaskClass);
/** @var \Symfony\Component\Console\Output\OutputInterface $output */
$output = $this->output();
$task->setVerbosityThreshold($output->getVerbosity());
Expand Down
77 changes: 77 additions & 0 deletions tests/src/CommandsTestBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Acquia\Drupal\RecommendedSettings\Tests;

use Acquia\Drupal\RecommendedSettings\Robo\Config\ConfigAwareTrait;
use Acquia\Drupal\RecommendedSettings\Tests\Helpers\NullLogOutputStylers;
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;
use Symfony\Component\Console\Output\NullOutput;

/**
* Base commands to test drush commands/tasks.
*/
abstract class CommandsTestBase extends TestCase {
use BuilderAwareTrait;
use OutputAwareTrait;
use ConfigAwareTrait;

/**
* Stores an instance of container object.
*/
protected ContainerInterface $container;

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->createContainer();
}

/**
* Initialize the Container.
*
* @param ContainerInterface|null $container
* An instance of container object or NULL.
*/
protected function createContainer(?ContainerInterface $container = NULL): void {
if (!$container) {
$container = new Container();
$output = new NullOutput();
$this->setOutput($output);

$config = new Config();
$this->setConfig($config);
$logger = new Logger($this->output());
$null_log_output = new NullLogOutputStylers;
$logger->setLogOutputStyler($null_log_output);
$container->add("logger", $logger);

$app = Robo::createDefaultApplication("acquia/drupal-recommended-settings", "1.0.0");
Robo::configureContainer($container, $app, $this->getConfig());

$tasks = new Tasks();
$builder = CollectionBuilder::create($container, $tasks);
$this->setBuilder($builder);
$container->add("builder", $builder);
}
$this->container = $container;
}

/**
* Returns an instance of container object.
*/
protected function getContainer(): ContainerInterface {
return $this->container;
}

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

namespace Acquia\Drupal\RecommendedSettings\Tests\Helpers;

use Consolidation\Log\SymfonyLogOutputStyler;

/**
* Class for not logging any error.
*/
class NullLogOutputStylers extends SymfonyLogOutputStyler {

/**
* @inheritdoc
*/
#[Override]
public function error($symfonyStyle, $level, $message, $context): void {
}

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

namespace Acquia\Drupal\RecommendedSettings\Tests\Helpers;

use Acquia\Drupal\RecommendedSettings\Robo\Tasks\DrushTask;
use Robo\ResultData;

class TestDrushTask extends DrushTask {

/**
* {@inheritdoc}
*/
#[Override]
protected function execute($process, $output_callback = NULL): ResultData {
$command = $process->getCommandLine();
$command_arr = explode(" ", $command);
$process->disableOutput();
if (in_array("--error", $command_arr)) {
return new ResultData(ResultData::EXITCODE_ERROR, $command, []);
}
return new ResultData(ResultData::EXITCODE_OK, $command, []);
}

}
35 changes: 35 additions & 0 deletions tests/src/unit/Robo/Config/ConfigAwareTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Acquia\Drupal\RecommendedSettings\Tests\Unit\Robo\Config;

use Acquia\Drupal\RecommendedSettings\Config\DefaultDrushConfig;
use Acquia\Drupal\RecommendedSettings\Robo\Config\ConfigAwareTrait;
use Drush\Config\DrushConfig;
use PHPUnit\Framework\TestCase;

/**
* Unit test for the ConfigAwareTrait trait.
*
* @covers \Acquia\Drupal\RecommendedSettings\Robo\Config\ConfigAwareTrait
*/
class ConfigAwareTraitTest extends TestCase {

use ConfigAwareTrait;

/**
* Tests the getConfigValue() for ConfigAwareTrait trait.
*/
public function testGetConfigValue(): void {
$this->assertEquals(
"/var/www/html/acms.prod/vendor",
$this->getConfigValue("composer.bin", "/var/www/html/acms.prod/vendor"),
);
$config = new DrushConfig();
$config->set("runtime.project", "/var/www/html/acms.prod");
$config->set("options.root", "/var/www/html/acms.prod/docroot");
$drush_config = new DefaultDrushConfig($config);
$this->setConfig($drush_config);
$this->assertEquals("/var/www/html/acms.prod", $this->getConfigValue("repo.root"));
}

}
Loading

0 comments on commit 24da387

Please sign in to comment.