Skip to content

Commit

Permalink
bug #10 Fix minor ver when major already exists now valid (sstok)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 1.0-dev branch.

Discussion
----------

When a new minor version was given but a newer major version already exists the new minor version was considered non-continues. This is fixed now.

Commits
-------

c957122 Fix minor ver when major already exists now valid
2e9210d Fix GitHub workflow
  • Loading branch information
sstok authored Feb 22, 2024
1 parent 7727b4d commit bea90d8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
jobs:
test:
name: PHP ${{ matrix.php-versions }}
runs-on: ubuntu-18.04
runs-on: 'ubuntu-latest'
strategy:
fail-fast: false
matrix:
Expand All @@ -21,14 +21,14 @@ jobs:
# https://github.com/actions/checkout (official)
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

# https://github.com/shivammathur/setup-php (community)
-
name: Setup PHP and composer with shivammathur/setup-php
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
php-version: '${{ matrix.php-versions }}'
coverage: none

# —— Composer 🧙‍️ —————————————————————————————————————————————————————————
Expand All @@ -42,13 +42,13 @@ jobs:
run: vendor/bin/phpunit --disallow-test-output --verbose
phpstan:
name: PHPStan
runs-on: ubuntu-latest
runs-on: 'ubuntu-latest'
strategy:
fail-fast: false
steps:
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

# https://github.com/shivammathur/setup-php (community)
-
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"php": "^7.1|^8.0"
},
"require-dev": {
"phpunit/phpunit": "^7.3.2|^8.5.12",
"phpunit/phpunit": "^7.3.2|^8.5.12|^9.6",
"phpstan/phpstan": "^0.12.33",
"phpstan/phpstan-phpunit": "^0.12.13",
"symfony/phpunit-bridge": "^5.2"
"symfony/phpunit-bridge": "^5.2 || ^6.4"
},
"autoload": {
"psr-4": {
Expand Down
18 changes: 14 additions & 4 deletions src/ContinuesVersionsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,33 @@ private function getLastArrayIndex(array $array): int
return key($array);
}

private function hasNewerVersionsAfter(int $major, int $minor): bool
private function hasNewerMajorVersionsAfter(int $major): bool
{
return $this->getLastArrayIndex($this->resolveVersions) > $major ||
$this->getLastArrayIndex($this->resolveVersions[$major]) > $minor;
return $this->getLastArrayIndex($this->resolveVersions) > $major;
}

private function hasNewerMinorVersionsAfter(int $major, int $minor): bool
{
return $this->getLastArrayIndex($this->resolveVersions[$major]) > $minor;
}

private function computePossibleVersionsFromMinor(int $major, int $minor): void
{
/** @var Version $version */
$version = $this->resolveVersions[$major][$minor];

if ($this->hasNewerVersionsAfter($major, $minor)) {
if ($this->hasNewerMinorVersionsAfter($major, $minor)) {
$this->possibleVersions = [$version->getNextIncreaseOf('patch')];

return;
}

if ($this->hasNewerMajorVersionsAfter($major)) {
$this->possibleVersions = [$version->getNextIncreaseOf('patch'), $version->getNextIncreaseOf('minor')];

return;
}

$versionCandidates = $version->getNextVersionCandidates();
$this->possibleVersions = $versionCandidates;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/ContinuesVersionsValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function provideContinuesVersions(): iterable
yield 'unstable #6' => ['1.0-BETA1', ['0.2', '0.1'], ['0.2.1', '0.3', '1.0-BETA1', '1.0']];

yield 'stable #1' => ['1.2', ['1.0', '1.1'], ['1.1.1', '1.2-BETA1', '1.2', '2.0-ALPHA1', '2.0-BETA1', '2.0']];
yield 'stable #2' => ['1.1.1', ['1.1', '2.0'], ['1.1.1']];
yield 'stable #2' => ['1.1.1', ['1.1', '2.0'], ['1.1.1', '1.2.0']];
}

/**
Expand All @@ -77,7 +77,7 @@ public function it_accepts_a_continues_version(string $new, array $existing, arr
{
$validator = new ContinuesVersionsValidator(...$this->createVersions($existing));

self::assertTrue($validator->isContinues(Version::fromString($new)));
self::assertTrue($validator->isContinues(Version::fromString($new)), sprintf('Excepts instead %s', implode(',', $validator->getPossibleVersions())));
self::assertEquals($this->createVersions($possible), array_merge([], $validator->getPossibleVersions()));
}

Expand Down Expand Up @@ -129,7 +129,7 @@ public function it_rejects_non_continues_version_with_no_pre_existing(string $ne
}

/**
* @return array<string, array<int, string[]|string>>
* @return iterable<string, array<int, string[]|string>>
*/
public function provideNonContinuesVersions(): iterable
{
Expand All @@ -142,6 +142,7 @@ public function provideNonContinuesVersions(): iterable

yield 'stable #1' => ['1.3', ['1.0', '1.1'], ['1.1.1', '1.2-BETA1', '1.2', '2.0-ALPHA1', '2.0-BETA1', '2.0']];
yield 'stable #2' => ['3.6', ['v3.5-beta1'], ['v3.5-beta2', 'v3.5-RC1', 'v3.5']];
yield 'stable #3 ' => ['3.6', ['v3.4', 'v3.7'], ['3.7.1', '3.8.0-BETA1', '3.8.0', '4.0.0-ALPHA1', '4.0.0-BETA1', '4.0.0']];
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public function provideExpectedIncreasedVersion(): array
// Minor, patch must be reset
'minor with patch 0' => ['0.1.0', '0.2.0', 'minor'],
'minor with patch 1' => ['0.1.1', '0.2.0', 'minor'],
'minor with new minor' => ['1.1.2', '1.2.0', 'minor'],

// Major, minor and patch must be reset
'major.0.0' => ['0.1.0', '1.0.0', 'major'],
Expand Down

0 comments on commit bea90d8

Please sign in to comment.