Skip to content

Commit

Permalink
Merge pull request #142 from ajardin/feature/environment-name
Browse files Browse the repository at this point in the history
Add the ability to use custom names when installing or registering
  • Loading branch information
ajardin authored May 13, 2020
2 parents 5b59da6 + b53ab47 commit 28c8b77
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 66 deletions.
35 changes: 23 additions & 12 deletions src/Command/Additional/RegisterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,38 @@

use App\Command\AbstractBaseCommand;
use App\Environment\EnvironmentEntity;
use App\Event\EnvironmentInstalledEvent;
use App\Exception\OrigamiExceptionInterface;
use App\Helper\CommandExitCode;
use App\Helper\ProcessProxy;
use App\Middleware\Configuration\ConfigurationInstaller;
use App\Middleware\Database;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class RegisterCommand extends AbstractBaseCommand
{
/** @var Database */
private $database;
/** @var ProcessProxy */
private $processProxy;

/** @var ConfigurationInstaller */
private $installer;

/** @var ProcessProxy */
private $processProxy;
/** @var EventDispatcherInterface */
private $eventDispatcher;

public function __construct(
Database $database,
ConfigurationInstaller $installer,
ProcessProxy $processProxy,
ConfigurationInstaller $installer,
EventDispatcherInterface $eventDispatcher,
?string $name = null
) {
parent::__construct($name);

$this->database = $database;
$this->installer = $installer;
$this->processProxy = $processProxy;
$this->installer = $installer;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand All @@ -57,10 +58,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
try {
if ($io->confirm('Do you want to register the current directory as a custom environment?', false)) {
$location = $this->processProxy->getWorkingDirectory();
$name = $this->askEnvironmentName($io, basename($location));

$environment = $this->installer->install($name, $location, EnvironmentEntity::TYPE_CUSTOM);

$environment = $this->installer->install($location, EnvironmentEntity::TYPE_CUSTOM);
$this->database->add($environment);
$this->database->save();
$event = new EnvironmentInstalledEvent($environment, $io);
$this->eventDispatcher->dispatch($event);

$io->success('Environment successfully registered.');
}
Expand All @@ -71,4 +74,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int

return $exitCode ?? CommandExitCode::SUCCESS;
}

/**
* Asks the question about the environment name.
*/
private function askEnvironmentName(SymfonyStyle $io, string $defaultName): string
{
return $io->ask('What is the name of the environment you want to install?', $defaultName);
}
}
20 changes: 17 additions & 3 deletions src/Command/Main/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Exception\InvalidConfigurationException;
use App\Exception\OrigamiExceptionInterface;
use App\Helper\CommandExitCode;
use App\Helper\ProcessProxy;
use App\Middleware\Configuration\ConfigurationInstaller;
use App\Middleware\DockerHub;
use App\Validator\Constraints\LocalDomains;
Expand All @@ -28,6 +29,9 @@ class InstallCommand extends AbstractBaseCommand
EnvironmentEntity::TYPE_SYMFONY,
];

/** @var ProcessProxy */
private $processProxy;

/** @var DockerHub */
private $dockerHub;

Expand All @@ -41,6 +45,7 @@ class InstallCommand extends AbstractBaseCommand
private $eventDispatcher;

public function __construct(
ProcessProxy $processProxy,
DockerHub $dockerHub,
ValidatorInterface $validator,
ConfigurationInstaller $installer,
Expand All @@ -49,6 +54,7 @@ public function __construct(
) {
parent::__construct($name);

$this->processProxy = $processProxy;
$this->dockerHub = $dockerHub;
$this->validator = $validator;
$this->installer = $installer;
Expand All @@ -72,13 +78,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io->note('The environment will be installed in the current directory.');

try {
$location = $this->processProxy->getWorkingDirectory();
$name = $this->askEnvironmentName($io, basename($location));
$type = $this->askEnvironmentType($io);
$phpVersion = $this->askPhpVersion($type, $io);
$domains = $this->askDomains($type, $io);

/** @var string $location */
$location = realpath('.');
$environment = $this->installer->install($location, $type, $phpVersion, $domains);
$environment = $this->installer->install($name, $location, $type, $phpVersion, $domains);

$event = new EnvironmentInstalledEvent($environment, $io);
$this->eventDispatcher->dispatch($event);
Expand All @@ -92,6 +98,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return $exitCode ?? CommandExitCode::SUCCESS;
}

/**
* Asks the question about the environment name.
*/
private function askEnvironmentName(SymfonyStyle $io, string $defaultName): string
{
return $io->ask('What is the name of the environment you want to install?', $defaultName);
}

/**
* Asks the choice question about the environment type.
*/
Expand Down
11 changes: 8 additions & 3 deletions src/Middleware/Configuration/ConfigurationInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ class ConfigurationInstaller extends AbstractConfiguration
*
* @throws FilesystemException
*/
public function install(string $location, string $type, ?string $phpVersion = null, ?string $domains = null): EnvironmentEntity
{
public function install(
string $name,
string $location,
string $type,
?string $phpVersion = null,
?string $domains = null
): EnvironmentEntity {
if ($type !== EnvironmentEntity::TYPE_CUSTOM) {
$source = __DIR__.sprintf('/../../Resources/%s', $type);
$destination = sprintf('%s/var/docker', $location);
Expand All @@ -34,6 +39,6 @@ public function install(string $location, string $type, ?string $phpVersion = nu
}
}

return new EnvironmentEntity(basename($location), $location, $type, $domains);
return new EnvironmentEntity($name, $location, $type, $domains);
}
}
68 changes: 54 additions & 14 deletions tests/Command/Additional/RegisterCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,36 @@

use App\Command\Additional\RegisterCommand;
use App\Environment\EnvironmentEntity;
use App\Event\EnvironmentInstalledEvent;
use App\Exception\InvalidEnvironmentException;
use App\Helper\CommandExitCode;
use App\Helper\ProcessProxy;
use App\Middleware\Configuration\ConfigurationInstaller;
use App\Middleware\Database;
use App\Tests\Command\AbstractCommandWebTestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\EventDispatcher\EventDispatcher;

/**
* @internal
*
* @covers \App\Command\AbstractBaseCommand
* @covers \App\Command\Additional\RegisterCommand
*
* @uses \App\Event\AbstractEnvironmentEvent
*/
final class RegisterCommandTest extends AbstractCommandWebTestCase
{
/** @var ObjectProphecy */
private $database;
private $processProxy;

/** @var ObjectProphecy */
private $installer;

/** @var ObjectProphecy */
private $processProxy;
private $eventDispatcher;

/**
* {@inheritdoc}
Expand All @@ -40,24 +44,52 @@ protected function setUp(): void
{
parent::setUp();

$this->database = $this->prophet->prophesize(Database::class);
$this->installer = $this->prophet->prophesize(ConfigurationInstaller::class);
$this->processProxy = $this->prophet->prophesize(ProcessProxy::class);
$this->installer = $this->prophet->prophesize(ConfigurationInstaller::class);
$this->eventDispatcher = $this->prophet->prophesize(EventDispatcher::class);
}

public function testItRegistersAnExternalEnvironment(): void
public function testItRegistersAnExternalEnvironmentWithDefaultName(): void
{
(new MethodProphecy($this->processProxy, 'getWorkingDirectory', []))
->shouldBeCalledOnce()
->willReturn('')
->willReturn('/fake/directory')
;

(new MethodProphecy($this->installer, 'install', ['', EnvironmentEntity::TYPE_CUSTOM, null]))
(new MethodProphecy($this->installer, 'install', ['directory', '/fake/directory', EnvironmentEntity::TYPE_CUSTOM, null]))
->shouldBeCalledOnce()
;

(new MethodProphecy($this->eventDispatcher, 'dispatch', [Argument::type(EnvironmentInstalledEvent::class)]))
->shouldBeCalledOnce()
;

$commandTester = new CommandTester($this->getCommand());
$commandTester->setInputs(['yes']);
$commandTester->setInputs(['yes', '']);
$commandTester->execute([]);

$display = $commandTester->getDisplay();
static::assertStringContainsString('[OK] Environment successfully registered.', $display);
static::assertSame(CommandExitCode::SUCCESS, $commandTester->getStatusCode());
}

public function testItRegistersAnExternalEnvironmentWithCustomName(): void
{
(new MethodProphecy($this->processProxy, 'getWorkingDirectory', []))
->shouldBeCalledOnce()
->willReturn('/fake/directory')
;

(new MethodProphecy($this->installer, 'install', ['custom-name', '/fake/directory', EnvironmentEntity::TYPE_CUSTOM, null]))
->shouldBeCalledOnce()
;

(new MethodProphecy($this->eventDispatcher, 'dispatch', [Argument::type(EnvironmentInstalledEvent::class)]))
->shouldBeCalledOnce()
;

$commandTester = new CommandTester($this->getCommand());
$commandTester->setInputs(['yes', 'custom-name']);
$commandTester->execute([]);

$display = $commandTester->getDisplay();
Expand All @@ -71,7 +103,11 @@ public function testItAbortsTheRegistrationAfterDisapproval(): void
->shouldNotBeCalled()
;

(new MethodProphecy($this->installer, 'install', ['', EnvironmentEntity::TYPE_CUSTOM, null]))
(new MethodProphecy($this->installer, 'install', []))
->shouldNotBeCalled()
;

(new MethodProphecy($this->eventDispatcher, 'dispatch', [Argument::type(EnvironmentInstalledEvent::class)]))
->shouldNotBeCalled()
;

Expand All @@ -91,12 +127,16 @@ public function testItGracefullyExitsWhenAnExceptionOccurred(): void
->willThrow(new InvalidEnvironmentException('Unable to determine the current working directory.'))
;

(new MethodProphecy($this->installer, 'install', ['', EnvironmentEntity::TYPE_CUSTOM, null]))
(new MethodProphecy($this->installer, 'install', []))
->shouldNotBeCalled()
;

(new MethodProphecy($this->eventDispatcher, 'dispatch', [Argument::type(EnvironmentInstalledEvent::class)]))
->shouldNotBeCalled()
;

$commandTester = new CommandTester($this->getCommand());
$commandTester->setInputs(['yes']);
$commandTester->setInputs(['yes', '']);
$commandTester->execute([]);

$display = $commandTester->getDisplay();
Expand All @@ -110,9 +150,9 @@ public function testItGracefullyExitsWhenAnExceptionOccurred(): void
private function getCommand(): RegisterCommand
{
return new RegisterCommand(
$this->database->reveal(),
$this->installer->reveal(),
$this->processProxy->reveal(),
$this->installer->reveal(),
$this->eventDispatcher->reveal(),
);
}
}
Loading

0 comments on commit 28c8b77

Please sign in to comment.