From e82e02ec2c81ea481bfae7bccbc3dd61b82bae7c Mon Sep 17 00:00:00 2001 From: LeJeanbono Date: Wed, 13 Oct 2021 17:04:48 +0200 Subject: [PATCH 1/2] #10 Make the cli parameter optional --- README.md | 2 ++ src/Configuration.php | 4 +++- src/PushCommand.php | 2 +- tests/ConfigurationTest.php | 22 ++++++++++++++++------ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 738a812..b8060c1 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,8 @@ Many of the options are optional since they can be added directly to the `compos [--src-ref=]\ [--ssl-verify=true/]\ + +If is not set, `composer.json` version will be used. # Example $ composer push --username=admin --password=admin123 --url=http://localhost:8081/repository/composer --ignore=test.php --ignore=foo/ --src-type=git --src-url="$(git remote get-url origin)" --src-ref="$(git rev-parse HEAD)" 0.0.1 diff --git a/src/Configuration.php b/src/Configuration.php index 5b1d652..273d902 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -94,11 +94,13 @@ public function getUrl() /** * Return the package version + * If version argument is not set, will return composer.json version property * @return string */ public function getVersion() { - return $this->input->getArgument('version'); + $versionArgument = $this->input->getArgument('version'); + return (!isset($versionArgument) || empty($versionArgument)) ? $this->composer->getPackage()->getVersion() : $versionArgument; } /** diff --git a/src/PushCommand.php b/src/PushCommand.php index b2f9ab4..c476f87 100644 --- a/src/PushCommand.php +++ b/src/PushCommand.php @@ -45,7 +45,7 @@ protected function configure() ->setAliases(['nexus-push']) // Deprecated, use push instead ->setDescription('Initiate a push to a distant repository') ->setDefinition([ - new InputArgument('version', InputArgument::REQUIRED, 'The package version'), + new InputArgument('version', InputArgument::OPTIONAL, 'The package version, if not set take composer.json version'), new InputOption('name', null, InputArgument::OPTIONAL, 'Name of the package (if different from the composer.json file)'), new InputOption('url', null, InputArgument::OPTIONAL, 'URL to the distant repository'), new InputOption('type', null, InputArgument::OPTIONAL, 'Type of the distant repository (default: nexus, available: [' . implode(', ', array_keys(self::PROVIDER_TYPES)) . '])'), diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index 5f5b05c..00ab1d0 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -68,9 +68,24 @@ public function setUp(): void public function testGetVersion() { + $this->inputMock->method('getArgument')->willReturnCallback(function ($argument) { + if ($argument === 'version') { + return '1.0.1'; + } + }); $this->assertEquals('1.0.1', $this->configuration->getVersion()); } + public function testGetVersionComposeJson() + { + $this->inputMock->method('getArgument')->willReturnCallback(function ($argument) { + if ($argument === 'version') { + return null; + } + }); + $this->assertEquals('1.2.3', $this->configuration->getVersion()); + } + public function testGetSourceUrl() { $this->assertEquals('my-src-url', $this->configuration->getSourceUrl()); @@ -270,12 +285,6 @@ private function createInputMock() } }); - $input->method('getArgument')->willReturnCallback(function ($argument) { - if ($argument === 'version') { - return '1.0.1'; - } - }); - return $input; } @@ -293,6 +302,7 @@ private function createComposerMock() $composer->method('getPackage')->willReturn($packageInterface); + $packageInterface->method('getVersion')->willReturn('1.2.3'); $packageInterface->method('getExtra')->willReturnCallback(function() { if ($this->singleConfig) { return [ From 25ae936a6911a03c2c277a599a22546dfc6870bd Mon Sep 17 00:00:00 2001 From: Jean-Michel Leclercq Date: Wed, 13 Oct 2021 21:03:31 +0200 Subject: [PATCH 2/2] Update src/Configuration.php Co-authored-by: Jonas Renaudot --- src/Configuration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Configuration.php b/src/Configuration.php index d09bae4..2788af5 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -100,7 +100,7 @@ public function getUrl() public function getVersion() { $versionArgument = $this->input->getArgument('version'); - return (!isset($versionArgument) || empty($versionArgument)) ? $this->composer->getPackage()->getVersion() : $versionArgument; + return empty($versionArgument) ? $this->composer->getPackage()->getVersion() : $versionArgument; } /**