Skip to content

Commit

Permalink
Create command: site:keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jigarius committed Oct 24, 2023
1 parent 0879344 commit 823d3c1
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Command/ExecCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ protected function configure() {
$this->setAliases(['ex']);
$this->setDescription('Execute a command on multiple Drupal sites.');
$this->addUsage('drush core:status');
$this->addUsage('--drall-group=bluish drush core:status');
$this->addUsage('--drall-workers=4 drush cache:rebuild');
$this->addUsage('./vendor/bin/drush core:status');
$this->addUsage('--drall-group=GROUP drush core:status');
$this->addUsage('--drall-filter=FILTER drush core:status');
$this->addUsage('--drall-workers=4 drush cache:rebuild');
$this->addUsage('ls web/sites/@@dir/settings.php');
$this->addUsage('echo "Working on @@site" && drush @@site.local core:status');

Expand Down
3 changes: 2 additions & 1 deletion src/Command/SiteAliasesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ protected function configure() {

$this->setName('site:aliases');
$this->setAliases(['sa']);
$this->setDescription('Get a list of site aliases.');
$this->setDescription('List all Drush site aliases.');
$this->addUsage('site:aliases');
$this->addUsage('--drall-group=GROUP site:aliases');
$this->addUsage('--drall-filter=FILTER site:aliases');
}

protected function execute(InputInterface $input, OutputInterface $output) {
Expand Down
4 changes: 2 additions & 2 deletions src/Command/SiteDirectoriesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* A command to get a list of site directories in the Drupal installation.
* A command to get a list of values in the $sites array.
*/
class SiteDirectoriesCommand extends BaseCommand {

Expand All @@ -15,7 +15,7 @@ protected function configure() {

$this->setName('site:directories');
$this->setAliases(['sd']);
$this->setDescription('Get a list of site directories.');
$this->setDescription('List the values of the $sites array.');
$this->addUsage('site:directories');
$this->addUsage('--drall-group=GROUP site:directories');
}
Expand Down
45 changes: 45 additions & 0 deletions src/Command/SiteKeysCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Drall\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* A command to get a list of keys in the $sites array.
*/
class SiteKeysCommand extends BaseCommand {

protected function configure() {
parent::configure();

$this->setName('site:keys');
$this->setAliases(['sk']);
$this->setDescription('List the keys of the $sites array.');
$this->addUsage('site:keys');
$this->addUsage('--drall-group=GROUP site:keys');
$this->addUsage('--drall-filter=FILTER site:keys');
}

protected function execute(InputInterface $input, OutputInterface $output) {
$this->preExecute($input, $output);

$keys = $this->siteDetector()
->getSiteKeys(
$this->getDrallGroup($input),
$this->getDrallFilter($input),
);

if (count($keys) === 0) {
$this->logger->warning('No Drupal sites found.');
return 0;
}

foreach ($keys as $key) {
$output->writeln($key);
}

return 0;
}

}
5 changes: 5 additions & 0 deletions src/Drall.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drall\Command\ExecCommand;
use Drall\Command\SiteDirectoriesCommand;
use Drall\Command\SiteAliasesCommand;
use Drall\Command\SiteKeysCommand;
use Drall\Model\EnvironmentId;
use Drall\Service\SiteDetector;
use Drall\Trait\SiteDetectorAwareTrait;
Expand Down Expand Up @@ -52,6 +53,10 @@ public function __construct(
$cmd->setSiteDetector($siteDetector);
$this->add($cmd);

$cmd = new SiteKeysCommand();
$cmd->setSiteDetector($siteDetector);
$this->add($cmd);

$cmd = new SiteAliasesCommand();
$cmd->setSiteDetector($siteDetector);
$this->add($cmd);
Expand Down
2 changes: 1 addition & 1 deletion test/Integration/Command/SiteAliasesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testExecute(): void {
}

/**
* Run site:aliases with --filter.
* Run site:aliases with --drall-filter.
*/
public function testExecuteWithFilter(): void {
$output = shell_exec('drall site:aliases --drall-filter="leo||ralph"');
Expand Down
2 changes: 1 addition & 1 deletion test/Integration/Command/SiteDirectoriesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testExecute(): void {
}

/**
* Run site:directories with --filter.
* Run site:directories with --drall-filter.
*/
public function testExecuteWithFilter(): void {
$output = shell_exec('drall site:directories --drall-filter="leo||ralph"');
Expand Down
128 changes: 128 additions & 0 deletions test/Integration/Command/SiteKeysCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace Drall\Test\Integration\Commands;

use Drall\IntegrationTestCase;

/**
* @covers \Drall\Command\SiteDirectoriesCommand
*/
class SiteKeysCommandTest extends IntegrationTestCase {

/**
* Run site:keys with no Drupal installation.
*/
public function testWithNoDrupal(): void {
chdir('/tmp');
$output = shell_exec('drall site:keys');
$this->assertOutputEquals("[warning] No Drupal sites found." . PHP_EOL, $output);
}

/**
* Run site:keys with a Drupal installation.
*/
public function testExecute(): void {
$output = shell_exec('drall site:keys');
$this->assertOutputEquals(<<<EOF
tmnt.com
cowabunga.com
tmnt.drall.local
donatello.com
8080.donatello.com
donnie.drall.local
leonardo.com
leo.drall.local
michelangelo.com
mikey.drall.local
raphael.com
ralph.drall.local
EOF, $output);
}

/**
* Run site:keys with --drall-filter.
*/
public function testExecuteWithFilter(): void {
$output = shell_exec('drall site:keys --drall-filter="[email protected]\$@"');
$this->assertOutputEquals(<<<EOF
tmnt.drall.local
donnie.drall.local
leo.drall.local
mikey.drall.local
ralph.drall.local
EOF, $output);
}

/**
* Run site:keys with --drall-group.
*/
public function testWithGroup(): void {
$output = shell_exec('drall site:keys --drall-group=bluish');
$this->assertOutputEquals(<<<EOF
donatello.com
8080.donatello.com
donnie.drall.local
leonardo.com
leo.drall.local
EOF, $output);
}

/**
* Run site:keys with DRALL_GROUP env var.
*/
public function testWithGroupEnvVar(): void {
$output = shell_exec('DRALL_GROUP=bluish drall site:keys');
$this->assertOutputEquals(<<<EOF
donatello.com
8080.donatello.com
donnie.drall.local
leonardo.com
leo.drall.local
EOF, $output);
}

public function testWithComposerRoot() {
chdir('/');
$output = shell_exec('drall --root=' . $this->drupalDir() . ' site:keys');
$this->assertEquals(<<<EOF
tmnt.com
cowabunga.com
tmnt.drall.local
donatello.com
8080.donatello.com
donnie.drall.local
leonardo.com
leo.drall.local
michelangelo.com
mikey.drall.local
raphael.com
ralph.drall.local
EOF, $output);
}

public function testWithDrupalRoot() {
chdir('/');
$output = shell_exec('drall --root=' . $this->drupalDir() . '/web site:keys');
$this->assertEquals(<<<EOF
tmnt.com
cowabunga.com
tmnt.drall.local
donatello.com
8080.donatello.com
donnie.drall.local
leonardo.com
leo.drall.local
michelangelo.com
mikey.drall.local
raphael.com
ralph.drall.local
EOF, $output);
}

}
101 changes: 101 additions & 0 deletions test/Unit/Command/SiteKeysCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Unit\Command;

use Consolidation\SiteAlias\SiteAliasManager;
use Symfony\Component\Console\Tester\CommandTester;
use DrupalFinder\DrupalFinder;
use Drall\Drall;
use Drall\Service\SiteDetector;
use Drall\TestCase;

/**
* @covers \Drall\Command\BaseCommand
* @covers \Drall\Command\SiteDirectoriesCommand
*/
class SiteKeysCommandTest extends TestCase {

public function testExecute() {
$drupalFinder = new DrupalFinder();
$siteAliasManager = new SiteAliasManager();

$siteDetectorMock = $this->getMockBuilder(SiteDetector::class)
->setConstructorArgs([$drupalFinder, $siteAliasManager])
->onlyMethods(['getSiteKeys'])
->getMock();
$siteDetectorMock
->expects($this->once())
->method('getSiteKeys')
->willReturn(['donatello.com', 'leonardo.com']);

$app = new Drall($siteDetectorMock);
$tester = new CommandTester($app->find('site:keys'));
$tester->execute([]);

$tester->assertCommandIsSuccessful();

$this->assertEquals(
<<<EOF
donatello.com
leonardo.com
EOF,
$tester->getDisplay()
);
}

public function testExecuteWithGroup() {
$drupalFinder = new DrupalFinder();
$siteAliasManager = new SiteAliasManager();

$siteDetectorMock = $this->getMockBuilder(SiteDetector::class)
->setConstructorArgs([$drupalFinder, $siteAliasManager])
->onlyMethods(['getSiteKeys'])
->getMock();
$siteDetectorMock
->expects($this->once())
->method('getSiteKeys')
->with('bluish')
->willReturn(['tmnt.com']);

$app = new Drall($siteDetectorMock);
$tester = new CommandTester($app->find('site:keys'));
$tester->execute(['--drall-group' => 'bluish']);

$tester->assertCommandIsSuccessful();

$this->assertEquals(
<<<EOF
tmnt.com
EOF,
$tester->getDisplay()
);
}

public function testExecuteWithNoSiteDirectories() {
$drupalFinder = new DrupalFinder();
$siteAliasManager = new SiteAliasManager();

$siteDetectorMock = $this->getMockBuilder(SiteDetector::class)
->setConstructorArgs([$drupalFinder, $siteAliasManager])
->onlyMethods(['getSiteKeys'])
->getMock();
$siteDetectorMock
->expects($this->once())
->method('getSiteKeys')
->willReturn([]);

$app = new Drall($siteDetectorMock);
$tester = new CommandTester($app->find('site:keys'));
$tester->execute([]);

$tester->assertCommandIsSuccessful();

$this->assertEquals(
'[warning] No Drupal sites found.' . PHP_EOL,
$tester->getDisplay()
);
}

}

0 comments on commit 823d3c1

Please sign in to comment.