diff --git a/bin/mage b/bin/mage index 3d1b6ca..3175025 100755 --- a/bin/mage +++ b/bin/mage @@ -12,7 +12,6 @@ use Mage\MageApplication; try { $mage = new MageApplication('.mage.yml'); - $mage->configure(); $mage->run(); } catch (Exception $exception) { printf('Error: %s' . PHP_EOL, $exception->getMessage()); diff --git a/src/Command/AbstractCommand.php b/src/Command/AbstractCommand.php index d26584e..9fa2f90 100644 --- a/src/Command/AbstractCommand.php +++ b/src/Command/AbstractCommand.php @@ -66,4 +66,12 @@ abstract class AbstractCommand extends Command $utils = new Utils(); return $utils->getStageName($this->runtime->getStage()); } + + /** + * Requires the configuration to be loaded + */ + protected function requireConfig() + { + $this->getApplication()->configure(); + } } diff --git a/src/Command/BuiltIn/Config/DumpCommand.php b/src/Command/BuiltIn/Config/DumpCommand.php index d5dfc56..8b7a185 100644 --- a/src/Command/BuiltIn/Config/DumpCommand.php +++ b/src/Command/BuiltIn/Config/DumpCommand.php @@ -41,6 +41,8 @@ class DumpCommand extends AbstractCommand */ protected function execute(InputInterface $input, OutputInterface $output) { + $this->requireConfig(); + $output->writeln('Starting Magallanes'); $output->writeln(''); diff --git a/src/Command/BuiltIn/Config/EnvironmentsCommand.php b/src/Command/BuiltIn/Config/EnvironmentsCommand.php index 566f83e..b66616b 100644 --- a/src/Command/BuiltIn/Config/EnvironmentsCommand.php +++ b/src/Command/BuiltIn/Config/EnvironmentsCommand.php @@ -42,6 +42,8 @@ class EnvironmentsCommand extends AbstractCommand */ protected function execute(InputInterface $input, OutputInterface $output) { + $this->requireConfig(); + $output->writeln('Starting Magallanes'); $output->writeln(''); diff --git a/src/Command/BuiltIn/DeployCommand.php b/src/Command/BuiltIn/DeployCommand.php index c932463..dd86e4d 100644 --- a/src/Command/BuiltIn/DeployCommand.php +++ b/src/Command/BuiltIn/DeployCommand.php @@ -58,6 +58,8 @@ class DeployCommand extends AbstractCommand */ protected function execute(InputInterface $input, OutputInterface $output) { + $this->requireConfig(); + $output->writeln('Starting Magallanes'); $output->writeln(''); diff --git a/src/Command/BuiltIn/Releases/ListCommand.php b/src/Command/BuiltIn/Releases/ListCommand.php index 5c034c9..4bee690 100644 --- a/src/Command/BuiltIn/Releases/ListCommand.php +++ b/src/Command/BuiltIn/Releases/ListCommand.php @@ -51,6 +51,8 @@ class ListCommand extends AbstractCommand */ protected function execute(InputInterface $input, OutputInterface $output) { + $this->requireConfig(); + $utils = new Utils(); $output->writeln('Starting Magallanes'); $output->writeln(''); diff --git a/src/Command/BuiltIn/Releases/RollbackCommand.php b/src/Command/BuiltIn/Releases/RollbackCommand.php index 794ad5d..c5d1a9b 100644 --- a/src/Command/BuiltIn/Releases/RollbackCommand.php +++ b/src/Command/BuiltIn/Releases/RollbackCommand.php @@ -52,6 +52,8 @@ class RollbackCommand extends DeployCommand */ protected function execute(InputInterface $input, OutputInterface $output) { + $this->requireConfig(); + $output->writeln('Starting Magallanes'); $output->writeln(''); diff --git a/src/MageApplication.php b/src/MageApplication.php index acbdc47..70b144c 100644 --- a/src/MageApplication.php +++ b/src/MageApplication.php @@ -40,9 +40,11 @@ class MageApplication extends Application */ public function __construct($file) { - $this->file = $file; + parent::__construct('Magallanes', Mage::VERSION); + $this->file = $file; $dispatcher = new EventDispatcher(); + $this->setDispatcher($dispatcher); $dispatcher->addListener(ConsoleEvents::EXCEPTION, function (ConsoleExceptionEvent $event) { $output = $event->getOutput(); @@ -52,8 +54,8 @@ class MageApplication extends Application $event->setException(new \LogicException('Caught exception', $exitCode, $event->getException())); }); - $this->setDispatcher($dispatcher); - parent::__construct('Magallanes', Mage::VERSION); + $this->runtime = $this->instantiateRuntime(); + $this->loadBuiltInCommands(); } /** @@ -85,12 +87,9 @@ class MageApplication extends Application $logger->pushHandler(new StreamHandler($logfile)); } - $this->runtime = $this->instantiateRuntime(); $this->runtime->setConfiguration($config['magephp']); $this->runtime->setLogger($logger); - - $this->loadBuiltInCommands(); - return true; + return; } throw new RuntimeException(sprintf('The file "%s" does not have a valid Magallanes configuration.', $this->file)); diff --git a/tests/Command/BuiltIn/Config/DumpCommandTest.php b/tests/Command/BuiltIn/Config/DumpCommandTest.php index 7723ec4..4eb25a9 100644 --- a/tests/Command/BuiltIn/Config/DumpCommandTest.php +++ b/tests/Command/BuiltIn/Config/DumpCommandTest.php @@ -21,7 +21,6 @@ class DumpCommandTest extends TestCase public function testConfigDumpTermination() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/basic.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('config:dump'); diff --git a/tests/Command/BuiltIn/Config/EnvironmentsCommandTest.php b/tests/Command/BuiltIn/Config/EnvironmentsCommandTest.php index f8e6995..58a6600 100644 --- a/tests/Command/BuiltIn/Config/EnvironmentsCommandTest.php +++ b/tests/Command/BuiltIn/Config/EnvironmentsCommandTest.php @@ -21,7 +21,6 @@ class EnvironmentsCommandTest extends TestCase public function testConfigDumpTermination() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/basic.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('config:environments'); diff --git a/tests/Command/BuiltIn/DeployCommandMiscTasksTest.php b/tests/Command/BuiltIn/DeployCommandMiscTasksTest.php index 1aaf87e..4d5efd6 100644 --- a/tests/Command/BuiltIn/DeployCommandMiscTasksTest.php +++ b/tests/Command/BuiltIn/DeployCommandMiscTasksTest.php @@ -21,7 +21,6 @@ class DeployCommandMiscTasksTest extends TestCase public function testSymfonyEnvironmentConfiguration() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/symfony-envconf.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -54,7 +53,6 @@ class DeployCommandMiscTasksTest extends TestCase public function testComposerFlags() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/composer.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -85,7 +83,6 @@ class DeployCommandMiscTasksTest extends TestCase public function testInvalidTaskName() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/invalid-task.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -101,7 +98,6 @@ class DeployCommandMiscTasksTest extends TestCase public function testBrokenGitBranch() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/broken-git-branch.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -120,7 +116,6 @@ class DeployCommandMiscTasksTest extends TestCase public function testBrokenGitCheckout() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/broken-git-branch.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -139,7 +134,6 @@ class DeployCommandMiscTasksTest extends TestCase public function testBrokenGitUpdate() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/broken-git-branch.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); diff --git a/tests/Command/BuiltIn/DeployCommandMiscTest.php b/tests/Command/BuiltIn/DeployCommandMiscTest.php index c459886..24dfdb8 100644 --- a/tests/Command/BuiltIn/DeployCommandMiscTest.php +++ b/tests/Command/BuiltIn/DeployCommandMiscTest.php @@ -21,7 +21,6 @@ class DeployCommandMiscTest extends TestCase public function testDeploymentWithNoHosts() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/no-hosts.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -40,7 +39,6 @@ class DeployCommandMiscTest extends TestCase public function testDeploymentWithSudo() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-sudo.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -79,7 +77,6 @@ class DeployCommandMiscTest extends TestCase public function testDeploymentWithBranchOverwrite() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-without-releases.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -117,8 +114,6 @@ class DeployCommandMiscTest extends TestCase public function testDeploymentWithCustomTask() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-custom-task.yml'); - $application->configure(); - /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -151,7 +146,6 @@ class DeployCommandMiscTest extends TestCase public function testDeploymentWithErrorTaskCommands() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-with-error.yml'); - $application->configure(); $application->getRuntime()->setReleaseId('20170101015120'); @@ -193,7 +187,6 @@ class DeployCommandMiscTest extends TestCase public function testDeploymentWithFailingPostDeployTaskCommands() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-with-postdeploy-error.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -229,7 +222,6 @@ class DeployCommandMiscTest extends TestCase public function testDeploymentWithSkippingTask() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-skipping.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); diff --git a/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php b/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php index 57747f3..fdcdece 100644 --- a/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php +++ b/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php @@ -21,7 +21,6 @@ class DeployCommandWithReleasesTest extends TestCase public function testDeploymentWithReleasesCommands() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost.yml'); - $application->configure(); $application->getRuntime()->setReleaseId('20170101015120'); @@ -72,7 +71,6 @@ class DeployCommandWithReleasesTest extends TestCase public function testDeploymentWithoutReleasesTarPrepare() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-force-tar1.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -88,7 +86,6 @@ class DeployCommandWithReleasesTest extends TestCase public function testDeploymentWithoutReleasesTarCopy() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-force-tar2.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -104,7 +101,6 @@ class DeployCommandWithReleasesTest extends TestCase public function testDeploymentWithoutReleasesTarCleanup() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-force-tar3.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -120,7 +116,6 @@ class DeployCommandWithReleasesTest extends TestCase public function testDeploymentFailCopyCommands() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-fail-copy-tar.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -136,7 +131,6 @@ class DeployCommandWithReleasesTest extends TestCase public function testDeploymentWithoutReleasesForceRelease() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-force-release.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -152,7 +146,6 @@ class DeployCommandWithReleasesTest extends TestCase public function testDeploymentFailToExtract() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost.yml'); - $application->configure(); $application->getRuntime()->setReleaseId('20170101015120'); @@ -195,7 +188,6 @@ class DeployCommandWithReleasesTest extends TestCase public function testDeploymentFailToCopy() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost.yml'); - $application->configure(); $application->getRuntime()->setReleaseId('20170101015120'); @@ -237,7 +229,6 @@ class DeployCommandWithReleasesTest extends TestCase public function testDeploymentFailCleanup() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost.yml'); - $application->configure(); $application->getRuntime()->setReleaseId('20170101015120'); @@ -291,7 +282,6 @@ class DeployCommandWithReleasesTest extends TestCase public function testDeploymentFailMidway() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost.yml'); - $application->configure(); $application->getRuntime()->setReleaseId('20170101015120'); diff --git a/tests/Command/BuiltIn/DeployCommandWithoutReleasesTest.php b/tests/Command/BuiltIn/DeployCommandWithoutReleasesTest.php index aa8d8f2..e4b7fd3 100644 --- a/tests/Command/BuiltIn/DeployCommandWithoutReleasesTest.php +++ b/tests/Command/BuiltIn/DeployCommandWithoutReleasesTest.php @@ -21,7 +21,6 @@ class DeployCommandWithoutReleasesTest extends TestCase public function testDeploymentWithoutReleasesCommands() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-without-releases.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); @@ -59,7 +58,6 @@ class DeployCommandWithoutReleasesTest extends TestCase public function testDeploymentFailMidway() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-without-releases.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('deploy'); diff --git a/tests/Command/BuiltIn/Releases/ListCommandTest.php b/tests/Command/BuiltIn/Releases/ListCommandTest.php index 99eb5d9..a37d802 100644 --- a/tests/Command/BuiltIn/Releases/ListCommandTest.php +++ b/tests/Command/BuiltIn/Releases/ListCommandTest.php @@ -21,7 +21,6 @@ class ListCommandTest extends TestCase public function testListReleasesCommands() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('releases:list'); @@ -49,7 +48,6 @@ class ListCommandTest extends TestCase public function testListReleasesWithInvalidEnvironment() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('releases:list'); @@ -65,7 +63,6 @@ class ListCommandTest extends TestCase public function testListReleasesWithoutReleases() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-without-releases.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('releases:list'); @@ -81,7 +78,6 @@ class ListCommandTest extends TestCase public function testFailToGetCurrentRelease() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-fail-get-current.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('releases:list'); @@ -97,7 +93,6 @@ class ListCommandTest extends TestCase public function testNoReleasesAvailable() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-no-releases.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('releases:list'); @@ -112,7 +107,6 @@ class ListCommandTest extends TestCase public function testFailGetReleases() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-fail-get-releases.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('releases:list'); @@ -128,7 +122,6 @@ class ListCommandTest extends TestCase public function testNoHosts() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-no-hosts.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('releases:list'); diff --git a/tests/Command/BuiltIn/Releases/RollbackCommandTest.php b/tests/Command/BuiltIn/Releases/RollbackCommandTest.php index 1e34aaf..30bbebb 100644 --- a/tests/Command/BuiltIn/Releases/RollbackCommandTest.php +++ b/tests/Command/BuiltIn/Releases/RollbackCommandTest.php @@ -21,7 +21,6 @@ class RollbackCommandTest extends TestCase public function testRollbackReleaseCommands() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('releases:rollback'); @@ -49,7 +48,6 @@ class RollbackCommandTest extends TestCase public function testRollbackReleaseWithInvalidEnvironment() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('releases:rollback'); @@ -65,7 +63,6 @@ class RollbackCommandTest extends TestCase public function testRollbackReleaseWithoutReleases() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-without-releases.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('releases:rollback'); @@ -81,7 +78,6 @@ class RollbackCommandTest extends TestCase public function testRollbackReleaseNotAvailable() { $application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-not-have-release.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('releases:rollback'); diff --git a/tests/Command/BuiltIn/VersionCommandTest.php b/tests/Command/BuiltIn/VersionCommandTest.php index feb2af8..2f83891 100644 --- a/tests/Command/BuiltIn/VersionCommandTest.php +++ b/tests/Command/BuiltIn/VersionCommandTest.php @@ -22,7 +22,6 @@ class VersionCommandTest extends TestCase public function testVersionOutput() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/basic.yml'); - $application->configure(); /** @var AbstractCommand $command */ $command = $application->find('version'); diff --git a/tests/MageApplicationTest.php b/tests/MageApplicationTest.php index 02af709..c3f2705 100644 --- a/tests/MageApplicationTest.php +++ b/tests/MageApplicationTest.php @@ -21,7 +21,6 @@ class MageApplicationTest extends TestCase public function testValidConfiguration() { $application = new MageApplication(__DIR__ . '/Resources/basic.yml'); - $application->configure(); $this->assertTrue($application instanceof MageApplication); } @@ -29,7 +28,6 @@ class MageApplicationTest extends TestCase { try { $application = new MageApplication(__DIR__ . '/Resources/invalid.yml'); - $application->configure(); } catch (Exception $exception) { $this->assertTrue($exception instanceof RuntimeException); $this->assertEquals(sprintf('The file "%s" does not have a valid Magallanes configuration.', __DIR__ . '/Resources/invalid.yml'), $exception->getMessage()); @@ -40,7 +38,6 @@ class MageApplicationTest extends TestCase { try { $application = new MageApplication(__DIR__ . '/Resources/invalid-yaml.yml'); - $application->configure(); } catch (Exception $exception) { $this->assertTrue($exception instanceof RuntimeException); $this->assertEquals(sprintf('Error parsing the file "%s".', __DIR__ . '/Resources/invalid-yaml.yml'), $exception->getMessage()); @@ -51,7 +48,6 @@ class MageApplicationTest extends TestCase { try { $application = new MageApplication(__DIR__ . '/Resources/this-does-not-exists.yml'); - $application->configure(); } catch (Exception $exception) { $this->assertTrue($exception instanceof RuntimeException); $this->assertEquals(sprintf('The file "%s" does not exists or is not readable.', __DIR__ . '/Resources/this-does-not-exists.yml'), $exception->getMessage()); @@ -62,7 +58,6 @@ class MageApplicationTest extends TestCase { $application = new MageApplication(__DIR__ . '/Resources/basic.yml'); $application->setAutoExit(false); - $application->configure(); $this->assertTrue($application instanceof MageApplication); $application->register('foo')->setCode(function () {