Skip to content

Commit

Permalink
add semver dto
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Mar 29, 2024
1 parent aac53eb commit ee1f424
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 15 deletions.
97 changes: 95 additions & 2 deletions src/Inspector/SemVer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,100 @@
*/
class SemVer
{
public const PATCH = 'patch';
public const MINOR = 'minor';
public const MAJOR = 'major';
public const MINOR = 'minor';
public const PATCH = 'patch';

private int $major;
private int $minor;
private int $patch;

public function __construct(int $major, int $minor, int $patch)
{
$this->major = $major;
$this->minor = $minor;
$this->patch = $patch;
}

public function getMajor(): int
{
return $this->major;
}

public function getMinor(): int
{
return $this->minor;
}

public function getPatch(): int
{
return $this->patch;
}

public function equals(SemVer $version): bool
{
return version_compare($this->toString(), $version->toString()) === 0;
}

public function greater(SemVer $version): bool
{
return version_compare($this->toString(), $version->toString()) === 1;
}

public function lower(SemVer $version): bool
{
return version_compare($this->toString(), $version->toString()) === -1;
}

public function increase(string $type): void
{
if ($type === self::MAJOR) {
$this->increaseMajor();
} elseif ($type === self::MINOR) {
$this->increaseMinor();
} elseif ($type === self::PATCH) {
$this->increasePatch();
} else {
throw new \InvalidArgumentException('Provided an invalid semantic versioning type, must be either major, minor or patch');
}
}

public function increaseMajor(): void
{
$this->major++;
$this->minor = 0;
$this->patch = 0;
}

public function increaseMinor(): void
{
$this->minor++;
$this->patch = 0;
}

public function increasePatch(): void
{
$this->patch++;
}

public function toString(): string
{
return implode('.', [$this->major, $this->minor, $this->patch]);
}

public function __toString(): string
{
return $this->toString();
}

public static function fromString(string $version): self
{
$parts = explode('.', $version, 3);

return new self(
(int) ($parts[0] ?? 0),
(int) ($parts[1] ?? 0),
(int) ($parts[2] ?? 0)
);
}
}
26 changes: 13 additions & 13 deletions src/Inspector/SemVerLifter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,37 @@
*/
class SemVerLifter
{
private ChangelogGenerator $generator;

public function __construct()
{
$this->generator = new ChangelogGenerator();
}

public function elevate(string $baseVersion, DefinitionsInterface $left, ?DefinitionsInterface $right = null): string
{
if ($right === null) {
return '0.1.0';
}

$parts = explode('.', $baseVersion, 3);
$major = (int) ($parts[0] ?? 0);
$minor = (int) ($parts[1] ?? 0);
$patch = (int) ($parts[2] ?? 0);
$version = SemVer::fromString($baseVersion);

$level = $this->getMaxSemVerLevel($left, $right);
if ($level === SemVer::MAJOR) {
$major++;
$minor = 0;
$patch = 0;
$version->increaseMajor();
} elseif ($level === SemVer::MINOR) {
$minor++;
$patch = 0;
$version->increaseMinor();
} else {
$patch++;
$version->increasePatch();
}

return implode('.', [$major, $minor, $patch]);
return $version->toString();
}

private function getMaxSemVerLevel(DefinitionsInterface $left, DefinitionsInterface $right): string
{
$generator = new ChangelogGenerator();
$levels = [];
foreach ($generator->generate($left, $right) as $level => $message) {
foreach ($this->generator->generate($left, $right) as $level => $message) {
$levels[$level] = $level;
}

Expand Down
96 changes: 96 additions & 0 deletions tests/Inspector/SemVerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/*
* PSX is an open source PHP framework to develop RESTful APIs.
* For the current version and information visit <https://phpsx.org>
*
* Copyright 2010-2023 Christoph Kappestein <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace PSX\Schema\Tests;

use PHPUnit\Framework\TestCase;
use PSX\Schema\Inspector\SemVer;

/**
* SemVerTest
*
* @author Christoph Kappestein <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://phpsx.org
*/
class SemVerTest extends TestCase
{
public function testCompare()
{
$version1 = SemVer::fromString('0.1.0');
$version2 = SemVer::fromString('0.1.1');

$this->assertTrue($version1->equals($version1));
$this->assertFalse($version1->greater($version1));
$this->assertFalse($version1->lower($version1));

$this->assertFalse($version1->equals($version2));
$this->assertFalse($version1->greater($version2));
$this->assertTrue($version1->lower($version2));

$this->assertFalse($version2->equals($version1));
$this->assertTrue($version2->greater($version1));
$this->assertFalse($version2->lower($version1));
}

public function testIncrease()
{
$version1 = SemVer::fromString('0.1.0');
$version1->increaseMajor();

$this->assertEquals('1.0.0', $version1->toString());

$version1 = SemVer::fromString('0.1.0');
$version1->increaseMinor();

$this->assertEquals('0.2.0', $version1->toString());

$version1 = SemVer::fromString('0.1.0');
$version1->increasePatch();

$this->assertEquals('0.1.1', $version1->toString());
}

public function testIncreaseType()
{
$version1 = SemVer::fromString('0.1.0');
$version1->increase(SemVer::MAJOR);

$this->assertEquals('1.0.0', $version1->toString());

$version1 = SemVer::fromString('0.1.0');
$version1->increase(SemVer::MINOR);

$this->assertEquals('0.2.0', $version1->toString());

$version1 = SemVer::fromString('0.1.0');
$version1->increase(SemVer::PATCH);

$this->assertEquals('0.1.1', $version1->toString());
}

public function testIncreaseTypeInvalid()
{
$this->expectException(\InvalidArgumentException::class);

$version1 = SemVer::fromString('0.1.0');
$version1->increase('foo');
}
}

0 comments on commit ee1f424

Please sign in to comment.