From 045980ffb3f60f02fba31623edfdc363945bd482 Mon Sep 17 00:00:00 2001 From: Philipp Baschke Date: Thu, 19 May 2016 09:34:23 +0200 Subject: [PATCH] Allow exact versions with 3 or 4 digits (#3) * Allow exact versions with 3 or 4 digits ACF PRO are sometimes released as 4 digit version numbers (e.g. 5.3.8.1). The validateVersion regex needs to be updated to account for that scenario as well. Fixes #2 * Update README to reflect new valid versions --- README.md | 4 ++-- src/ACFProInstaller/Plugin.php | 10 +++++----- tests/ACFProInstaller/PluginTest.php | 21 +++++++++++++-------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ce9664d..029c46e 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ It reads your :key: ACF PRO key from the **environment** or a **.env file**. "type": "package", "package": { "name": "advanced-custom-fields/advanced-custom-fields-pro", - "version": "*.*.*", + "version": "*.*.*(.*)", "type": "wordpress-plugin", "dist": { "type": "zip", @@ -35,7 +35,7 @@ It reads your :key: ACF PRO key from the **environment** or a **.env file**. } } ``` -Replace `"version": "*.*.*"` with your desired version. +Replace `"version": "*.*.*(.*)"` with your desired version. **2. Make your ACF PRO key available** diff --git a/src/ACFProInstaller/Plugin.php b/src/ACFProInstaller/Plugin.php index 1ab027a..2ebecd9 100644 --- a/src/ACFProInstaller/Plugin.php +++ b/src/ACFProInstaller/Plugin.php @@ -170,10 +170,10 @@ protected function getPackageFromOperation(OperationInterface $operation) } /** - * Validate that the version is an exact major.minor.patch version + * Validate that the version is an exact major.minor.patch.optional version * * The url to download the code for the package only works with exact - * version numbers with 3 digits: e.g. 1.2.3 + * version numbers with 3 or 4 digits: e.g. 1.2.3 or 1.2.3.4 * * @access protected * @param string $version The version that should be validated @@ -184,12 +184,12 @@ protected function validateVersion($version) { // \A = start of string, \Z = end of string // See: http://stackoverflow.com/a/34994075 - $major_minor_patch = '/\A\d\.\d\.\d\Z/'; + $major_minor_patch_optional = '/\A\d\.\d\.\d(?:\.\d)?\Z/'; - if (!preg_match($major_minor_patch, $version)) { + if (!preg_match($major_minor_patch_optional, $version)) { throw new \UnexpectedValueException( 'The version constraint of ' . self::ACF_PRO_PACKAGE_NAME . - ' should be exact (with 3 digits). ' . + ' should be exact (with 3 or 4 digits). ' . 'Invalid version string "' . $version . '"' ); } diff --git a/tests/ACFProInstaller/PluginTest.php b/tests/ACFProInstaller/PluginTest.php index d52f161..f5e7327 100644 --- a/tests/ACFProInstaller/PluginTest.php +++ b/tests/ACFProInstaller/PluginTest.php @@ -298,7 +298,7 @@ public function testDontAddVersionOnOtherPackages() $plugin->addVersion($packageEvent); } - public function testExactVersionPassesValidation() + protected function versionPassesValidationHelper($version) { // Make key available in the ENVIRONMENT putenv(self::KEY_ENV_VARIABLE . '=KEY'); @@ -322,7 +322,7 @@ public function testExactVersionPassesValidation() $package ->expects($this->once()) ->method('getPrettyVersion') - ->willReturn('1.2.3'); + ->willReturn($version); $package ->expects($this->once()) @@ -369,13 +369,23 @@ public function testExactVersionPassesValidation() $plugin->addVersion($packageEvent); } + public function testExactVersionWith3DigitsPassesValidation() + { + $this->versionPassesValidationHelper('1.2.3'); + } + + public function testExactVersionWith4DigitsPassesValidation() + { + $this->versionPassesValidationHelper('1.2.3.4'); + } + protected function versionFailsValidationHelper($version) { // Expect an Exception $this->setExpectedException( 'UnexpectedValueException', 'The version constraint of ' . self::REPO_NAME . - ' should be exact (with 3 digits). ' . + ' should be exact (with 3 or 4 digits). ' . 'Invalid version string "' . $version . '"' ); @@ -444,11 +454,6 @@ public function testExactVersionWith1DigitsFailsValidation() $this->versionFailsValidationHelper('1'); } - public function testExactVersionWith4DigitsFailsValidation() - { - $this->versionFailsValidationHelper('1.2.3.4'); - } - public function testDontAddVersionTwice() { // The version that should be required