Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mazanax committed Apr 20, 2021
0 parents commit 8319e1a
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
.phpunit.result.cache
14 changes: 14 additions & 0 deletions .travis.yml
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)
33 changes: 33 additions & 0 deletions composer.json
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"
}
}
17 changes: 17 additions & 0 deletions phpunit.xml.dist
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>
54 changes: 54 additions & 0 deletions src/PackageVersionSemver.php
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();
}
}
54 changes: 54 additions & 0 deletions tests/PackageVersionSemverTest.php
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());
}
}
5 changes: 5 additions & 0 deletions tests/fixtures/broken_json_file.json
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",
}
5 changes: 5 additions & 0 deletions tests/fixtures/with_version.json
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"
}
4 changes: 4 additions & 0 deletions tests/fixtures/without_version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test/package",
"description": "Package without specified version"
}

0 comments on commit 8319e1a

Please sign in to comment.