-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8319e1a
Showing
9 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
language: php | ||
php: | ||
- '7.4' | ||
- nightly | ||
|
||
before_script: | ||
- travis_retry composer self-update | ||
- travis_retry composer install --no-interaction --prefer-source --dev | ||
|
||
script: | ||
- vendor/bin/phpunit --coverage-clover=coverage.xml | ||
|
||
after_success: | ||
- bash <(curl -s https://codecov.io/bash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "mazanax/composer-version-semver", | ||
"description": "Small helper to get current composer project version", | ||
"type": "library", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Alexander Gorshkov", | ||
"email": "agorshkov@mznx.ru" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php": "^7.4|^8.0", | ||
"ext-json": "*", | ||
"nikolaposa/version": "^4.1", | ||
"mazanax/composer-project-version": "^1.0.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"MZNX\\ComposerPackageSemver\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"MZNX\\ComposerPackageSemver\\Tests\\": "tests/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> | ||
<coverage> | ||
<include> | ||
<directory suffix=".php">./src/</directory> | ||
</include> | ||
<report> | ||
<clover outputFile="clover.xml"/> | ||
</report> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Unit"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<logging/> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MZNX\ComposerPackageSemver; | ||
|
||
use MZNX\ComposerPackageVersion\PackageVersion; | ||
use Version\Exception\InvalidVersionString; | ||
use Version\Version; | ||
|
||
class PackageVersionSemver | ||
{ | ||
private string $pathToComposerJson; | ||
|
||
private ?Version $version = null; | ||
|
||
private static ?PackageVersionSemver $instance = null; | ||
|
||
private function __construct(string $pathToComposerJson) | ||
{ | ||
$this->pathToComposerJson = $pathToComposerJson; | ||
} | ||
|
||
private function __clone() | ||
{ | ||
} | ||
|
||
public static function init(string $pathToComposerJson): PackageVersionSemver | ||
{ | ||
if (self::$instance === null || self::$instance->pathToComposerJson !== $pathToComposerJson) { | ||
self::$instance = new self($pathToComposerJson); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
|
||
/** | ||
* @throws InvalidVersionString | ||
*/ | ||
public function getVersion(): ?Version | ||
{ | ||
if (null === $this->version) { | ||
$version = PackageVersion::init($this->pathToComposerJson)->getVersion(); | ||
$this->version = Version::fromString($version); | ||
} | ||
|
||
return $this->version; | ||
} | ||
|
||
public function getVersionAsString(): string | ||
{ | ||
return PackageVersion::init($this->pathToComposerJson)->getVersion(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MZNX\ComposerPackageSemver\Tests; | ||
|
||
use InvalidArgumentException; | ||
use MZNX\ComposerPackageSemver\PackageVersionSemver; | ||
use PHPUnit\Framework\TestCase; | ||
use Version\Exception\InvalidVersionString; | ||
|
||
class PackageVersionSemverTest extends TestCase | ||
{ | ||
public function test_broken_json(): void | ||
{ | ||
$sut = PackageVersionSemver::init(__DIR__ . '/fixtures/broken_json_file.json'); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$sut->getVersion(); | ||
} | ||
|
||
public function test_get_version_with_invalid_file_path(): void | ||
{ | ||
$sut = PackageVersionSemver::init(__DIR__ . '/non-existing/composer/json/path'); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$sut->getVersion(); | ||
} | ||
|
||
public function test_get_version_as_string(): void | ||
{ | ||
$sut = PackageVersionSemver::init(__DIR__ . '/fixtures/without_version.json'); | ||
|
||
self::assertEquals('dev-master', $sut->getVersionAsString()); | ||
} | ||
|
||
public function test_get_version_with_default_value(): void | ||
{ | ||
$sut = PackageVersionSemver::init(__DIR__ . '/fixtures/without_version.json'); | ||
|
||
$this->expectException(InvalidVersionString::class); | ||
$sut->getVersion(); | ||
} | ||
|
||
public function test_get_version(): void | ||
{ | ||
$sut = PackageVersionSemver::init(__DIR__ . '/fixtures/with_version.json'); | ||
|
||
$version = $sut->getVersion(); | ||
self::assertEquals(1, $version->getMajor()); | ||
self::assertEquals(2, $version->getMinor()); | ||
self::assertEquals(3, $version->getPatch()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "test/package", | ||
"description": "Broken json file with specified version", | ||
"version": "1.2.3", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "test/package", | ||
"description": "Package with specified version", | ||
"version": "1.2.3" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "test/package", | ||
"description": "Package without specified version" | ||
} |