Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetupCommand improvements #19

Merged
merged 5 commits into from
Jan 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions src/Command/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Prooph\Micro\Command;

use RomanPitak\Nginx\Config\Directive;
use RomanPitak\Nginx\Config\Scope;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -33,20 +35,22 @@ protected function configure()
->addOption('no-ports', null, InputOption::VALUE_NONE, 'Do not publish ports on docker host.');
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

if (! $this->lock()) {
$io->warning('The command is already running in another process.');
$io->warning('The command is already running in another process. Aborted.');

return 0;
return 1;
}

if (file_exists($this->getRootDir() . '/docker-compose.yml')) {
$io->warning('docker-compose.yml exists already. Aborted.');

return;
$this->release();

return 1;
}

$io->section('This setup will use the following docker-images:');
Expand Down Expand Up @@ -78,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return '';
}

if (! is_int((int) $answer) || $answer === 0) {
if (! is_int((int) $answer) || 0 === (int) $answer) {
throw new \RuntimeException(
'Invalid HTTP port'
);
Expand All @@ -98,7 +102,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return '';
}

if (! is_int((int) $answer) || $answer === 0) {
if (! is_int((int) $answer) || 0 === (int) $answer) {
throw new \RuntimeException(
'Invalid HTTPS port'
);
Expand All @@ -119,7 +123,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (! $io->confirm('Are those settings correct?', false)) {
$io->writeln('<comment>Aborted</comment>');

return;
$this->release();

return 1;
}

file_put_contents(
Expand All @@ -131,35 +137,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
)
);

$gatewayConfig = <<<EOT
server {
listen 80;
listen 443 ssl http2;
server_name localhost;
root /var/www/public;

index index.php;

include conf.d/basic.conf;

location / {
# This is cool because no php is touched for static content.
# include the "?\$args" part so non-default permalinks doesn't break when using query string
try_files \$uri \$uri/ 404;
}
}
EOT;

@mkdir($this->getRootDir() . '/' . $gatewayDirectory);
if (! is_dir($this->getRootDir() . '/' . $gatewayDirectory)) {
mkdir($this->getRootDir() . '/' . $gatewayDirectory, 0777, true);
}

file_put_contents(
$this->getRootDir() . '/' . $gatewayDirectory . '/www.conf',
$gatewayConfig
);
$this->generateNginxConfig()->saveToFile($this->getRootDir() . '/' . $gatewayDirectory . '/www.conf');

$io->success('Successfully created microservice settings');

$this->release();

return 0;
}

private function generateConfigFile(
Expand Down Expand Up @@ -192,4 +180,24 @@ private function generateConfigFile(

return Yaml::dump($config, 4);
}

private function generateNginxConfig(): Scope
{
$config = Scope::create()
->addDirective(Directive::create('server')
->setChildScope(Scope::create()
->addDirective(Directive::create('listen', '80'))
->addDirective(Directive::create('listen', '443 ssl http2'))
->addDirective(Directive::create('server_name', 'localhost'))
->addDirective(Directive::create('root', '/var/www/public'))
->addDirective(Directive::create('index', 'index.php'))
->addDirective(Directive::create('include', ' conf.d/basic.conf'))
->addDirective(Directive::create('location', '/', Scope::create()
->addDirective(Directive::create('try_files', '\$uri \$uri/ 404'))
))
)
);

return $config;
}
}
126 changes: 106 additions & 20 deletions tests/Command/SetupCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,133 @@
final class SetupCommandTest extends TestCase
{
/**
* @var Application
* @var SetupCommand
*/
private $application;
private $command;

public function setUp(): void
{
$this->application = new Application();

$setupCommand = $this->getMockBuilder(SetupCommand::class)
/** @var SetupCommand $command */
$command = $this->getMockBuilder(SetupCommand::class)
->setMethods(['getRootDir'])
->getMock();

$setupCommand->method('getRootDir')->willReturn(sys_get_temp_dir());
$command
->method('getRootDir')
->willReturn(sys_get_temp_dir());

$application = new Application();
$application->add($command);

$this->application->add($setupCommand);
$this->command = $application->get($command->getName());
}

public function tearDown(): void
{
unlink(sys_get_temp_dir() . '/gateway/www.conf');
unlink(sys_get_temp_dir() . '/docker-compose.yml');
$this->removeTempFiles();
}

/**
* @test
*/
public function it_creates_config_files(): void
{
$command = $this->application->find('micro:setup');
$commandTester = new CommandTester($command);

$commandTester->setInputs([
'gateway',
'80',
'443',
'y',
]);

$commandTester->execute(['command' => $command->getName()]);
$commandTester = new CommandTester($this->command);
$commandTester->setInputs(['gateway', '80', '443', 'y']);
$commandTester->execute([]);

$this->assertSame(0, $commandTester->getStatusCode());
$this->assertTrue(file_exists(sys_get_temp_dir() . '/gateway/www.conf'));
$this->assertTrue(file_exists(sys_get_temp_dir() . '/docker-compose.yml'));
}

/**
* @test
* @dataProvider getValidInputParameters
*/
public function it_answers_with_success_exit_code(array $validInputParameters): void
{
$commandTester = new CommandTester($this->command);
$commandTester->setInputs($validInputParameters);
$commandTester->execute([]);

$this->assertSame(0, $commandTester->getStatusCode());
}

public function getValidInputParameters(): array
{
return [
[['', '', '', 'y']],
[['', '80', '443', 'y']],
[['gateway', '80', '443', 'y']],
[['another/dir', '80', '443', 'y']],
[['gateway', '8080', '443', 'y']],
[['gateway', '', '443', 'y']],
[['gateway', 'random', '443', 'y']],
[['gateway', '80', '', 'y']],
[['gateway', '80', 'random', 'y']],
[['gateway', '', '', 'y']],
];
}

/**
* @test
* @dataProvider getInvalidInputParameters
*/
public function it_answers_with_error_exit_code(array $invalidInputParameters): void
{
$commandTester = new CommandTester($this->command);
$commandTester->setInputs($invalidInputParameters);
$commandTester->execute([]);

$this->assertSame(1, $commandTester->getStatusCode());
}

public function getInvalidInputParameters(): array
{
return [
[['', '', '', 'n']],
];
}

/**
* @test
*/
public function it_exits_if_docker_compose_file_already_exists(): void
{
touch(sys_get_temp_dir() . '/docker-compose.yml');

$commandTester = new CommandTester($this->command);
$commandTester->execute([]);

$this->assertSame(1, $commandTester->getStatusCode());
$this->assertRegExp('/docker-compose.yml exists already/', $commandTester->getDisplay());
}

private function removeTempFiles(): void
{
$files = ['gateway', 'docker-compose.yml'];

foreach ($files as $file) {
if (is_file(sys_get_temp_dir() . '/' . $file)) {
unlink(sys_get_temp_dir() . '/' . $file);
}

if (is_dir(sys_get_temp_dir() . '/' . $file)) {
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
sys_get_temp_dir() . '/' . $file,
\RecursiveDirectoryIterator::SKIP_DOTS
),
\RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($files as $fileInfo) {
$fileInfo->isDir() ? rmdir($fileInfo->getRealPath()) : unlink($fileInfo->getRealPath());
}

rmdir(sys_get_temp_dir() . '/' . $file);
}
}
}
}