Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mazanax committed Apr 19, 2021
0 parents commit 82e6234
Show file tree
Hide file tree
Showing 8 changed files with 171 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
.phpunit.result.cache
composer.lock
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "mazanax/composer-project-version",
"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",
"ext-json": "*"
},
"autoload": {
"psr-4": {
"MZNX\\ComposerPackageVersion\\": "src/"
},
"files": ["src/helper.php"]
},
"autoload-dev": {
"psr-4": {
"MZNX\\ComposerPackageVersion\\Tests\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^9.5"
}
}
62 changes: 62 additions & 0 deletions src/PackageVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace MZNX\ComposerPackageVersion;

use InvalidArgumentException;
use JsonException;

class PackageVersion
{
private string $pathToComposerJson;
private ?string $version = null;

private static ?PackageVersion $instance = null;

private function __construct(string $pathToComposerJson)
{
$this->pathToComposerJson = $pathToComposerJson;
}

private function __clone()
{
}

public static function init(string $pathToComposerJson): self
{
if (self::$instance === null || self::$instance->pathToComposerJson !== $pathToComposerJson) {
self::$instance = new self($pathToComposerJson);
}

return self::$instance;
}

public function getVersion(): string
{
if (null === $this->version) {
$this->version = $this->parseVersion();
}

return $this->version;
}

private function parseVersion(): string
{
if (!file_exists($this->pathToComposerJson)) {
throw new InvalidArgumentException(
sprintf('Cannot parse %s: file does not exist', $this->pathToComposerJson)
);
}

try {
$json = json_decode(file_get_contents($this->pathToComposerJson), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new InvalidArgumentException(
sprintf('Cannot parse %s: %s', $this->pathToComposerJson, $e->getMessage()), 0, $e
);
}

return trim($json['version'] ?? 'dev-master');
}
}
11 changes: 11 additions & 0 deletions src/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace MZNX\ComposerPackageVersion;

function Version(string $pathToComposerJson): string
{
return PackageVersion::init($pathToComposerJson)
->getVersion();
}
49 changes: 49 additions & 0 deletions tests/PackageVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace MZNX\ComposerPackageVersion\Tests;

use InvalidArgumentException;
use MZNX\ComposerPackageVersion\PackageVersion;
use PHPUnit\Framework\TestCase;

use function MZNX\ComposerPackageVersion\Version;

class PackageVersionTest extends TestCase
{
public function test_get_version(): void
{
$sut = PackageVersion::init(__DIR__ . '/fixtures/with_version.json');

self::assertEquals('1.2.3', $sut->getVersion());
}

public function test_helper_function(): void
{
static::assertEquals('1.2.3', Version(__DIR__ . '/fixtures/with_version.json'));
}

public function test_get_default_version(): void
{
$sut = PackageVersion::init(__DIR__ . '/fixtures/without_version.json');

self::assertEquals('dev-master', $sut->getVersion());
}

public function test_get_version_with_invalid_file_path(): void
{
$sut = PackageVersion::init(__DIR__ . '/non-existing/composer/json/path');

$this->expectException(InvalidArgumentException::class);
$sut->getVersion();
}

public function test_get_version_with_broken_json_file(): void
{
$sut = PackageVersion::init(__DIR__ . '/fixtures/broken_json_file.json');

$this->expectException(InvalidArgumentException::class);
$sut->getVersion();
}
}
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 82e6234

Please sign in to comment.