Skip to content

Commit

Permalink
Fixed check version stability
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Jan 26, 2021
1 parent 8a05dae commit 6664a74
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 30 deletions.
10 changes: 8 additions & 2 deletions src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,17 @@ services:
tags:
- { name: "support_tools.system_info.collector", identifier: "ibexa" }

EzSystems\EzSupportTools\VersionStability\ComposerVersionStabilityChecker: ~

EzSystems\EzSupportTools\VersionStability\VersionStabilityChecker:
'@EzSystems\EzSupportTools\VersionStability\ComposerVersionStabilityChecker'

support_tools.system_info.collector.composer.lock_file:
class: "%support_tools.system_info.collector.composer.lock_file.class%"
arguments:
- "%kernel.project_dir%/composer.lock"
- "%kernel.project_dir%/composer.json"
$versionStabilityChecker: '@EzSystems\EzSupportTools\VersionStability\VersionStabilityChecker'
$lockFile: "%kernel.project_dir%/composer.lock"
$jsonFile: "%kernel.project_dir%/composer.json"
tags:
- { name: "support_tools.system_info.collector", identifier: "composer" }

Expand Down
5 changes: 3 additions & 2 deletions src/bundle/SystemInfo/Collector/IbexaSystemInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace EzSystems\EzSupportToolsBundle\SystemInfo\Collector;

use EzSystems\EzPlatformCoreBundle\EzPlatformCoreBundle;
use EzSystems\EzSupportTools\Value\Stability;
use EzSystems\EzSupportToolsBundle\DependencyInjection\EzSystemsEzSupportToolsExtension;
use EzSystems\EzSupportToolsBundle\SystemInfo\Exception\ComposerLockFileNotFoundException;
use EzSystems\EzSupportToolsBundle\SystemInfo\Value\ComposerSystemInfo;
Expand Down Expand Up @@ -226,7 +227,7 @@ private function getEOLDate(string $ibexaRelease): ?DateTime

private static function getStability(ComposerSystemInfo $composerInfo): string
{
$stabilityFlags = array_flip(JsonComposerLockSystemInfoCollector::STABILITIES);
$stabilityFlags = array_flip(Stability::STABILITIES);

// Root package stability
$stabilityFlag = $composerInfo->minimumStability !== null ?
Expand All @@ -248,7 +249,7 @@ private static function getStability(ComposerSystemInfo $composerInfo): string
}
}

return JsonComposerLockSystemInfoCollector::STABILITIES[$stabilityFlag];
return Stability::STABILITIES[$stabilityFlag];
}

private static function hasAnyPackage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
namespace EzSystems\EzSupportToolsBundle\SystemInfo\Collector;

use Composer\InstalledVersions;
use EzSystems\EzSupportTools\Value\Stability;
use EzSystems\EzSupportTools\VersionStability\VersionStabilityChecker;
use EzSystems\EzSupportToolsBundle\SystemInfo\Exception;
use EzSystems\EzSupportToolsBundle\SystemInfo\Value\ComposerSystemInfo;
use EzSystems\EzSupportToolsBundle\SystemInfo\Value\ComposerPackage;
Expand All @@ -15,18 +18,10 @@
*/
class JsonComposerLockSystemInfoCollector implements SystemInfoCollector
{
/**
* @var array Hash of stability constant values to human readable stabilities, see Composer\Package\BasePackage.
*
* Needed as long as we don't want to depend on Composer.
*/
public const STABILITIES = [
0 => 'stable',
5 => 'RC',
10 => 'beta',
15 => 'alpha',
20 => 'dev',
];
public const IBEXA_OSS_PACKAGE = 'ibexa/oss';

/** @var \EzSystems\EzSupportTools\VersionStability\VersionStabilityChecker */
private $versionStabilityChecker;

/**
* @var string Composer lock file with full path.
Expand All @@ -43,18 +38,22 @@ class JsonComposerLockSystemInfoCollector implements SystemInfoCollector
*/
private $value;

public function __construct($lockFile, $jsonFile)
{
public function __construct(
VersionStabilityChecker $versionStabilityChecker,
$lockFile,
$jsonFile
) {
$this->versionStabilityChecker = $versionStabilityChecker;
$this->lockFile = $lockFile;
$this->jsonFile = $jsonFile;
}

/**
* Collects information about installed composer packages.
*
* @throws Exception\ComposerLockFileNotFoundException if the composer.lock file was not found.
*
* @return \EzSystems\EzSupportToolsBundle\SystemInfo\Value\ComposerSystemInfo
*
* @throws Exception\ComposerLockFileNotFoundException if the composer.lock file was not found.
*/
public function collect(): ComposerSystemInfo
{
Expand All @@ -72,11 +71,16 @@ public function collect(): ComposerSystemInfo

$lockData = json_decode(file_get_contents($this->lockFile), true);
$jsonData = json_decode(file_get_contents($this->jsonFile), true);
$stability = InstalledVersions::isInstalled(self::IBEXA_OSS_PACKAGE)
? $this->versionStabilityChecker->getStability(
InstalledVersions::getVersion(self::IBEXA_OSS_PACKAGE)
)
: $this->getMinimumStability($lockData);

return $this->value = new ComposerSystemInfo([
'packages' => $this->extractPackages($lockData),
'repositoryUrls' => $this->extractRepositoryUrls($jsonData),
'minimumStability' => isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : null,
'minimumStability' => $stability,
]);
}

Expand Down Expand Up @@ -106,8 +110,8 @@ private function extractPackages(array $lockData): array
if (isset($lockData['stability-flags'][$package->name])) {
$stabilityFlag = (int)$lockData['stability-flags'][$package->name];

if (isset(self::STABILITIES[$stabilityFlag])) {
$package->stability = self::STABILITIES[$stabilityFlag];
if (isset(Stability::STABILITIES[$stabilityFlag])) {
$package->stability = Stability::STABILITIES[$stabilityFlag];
}
}

Expand Down Expand Up @@ -171,4 +175,9 @@ private static function setNormalizedVersion(ComposerPackage $package): void

$package->version = $version;
}

private function getMinimumStability(array $lockData): ?string
{
return $lockData['minimum-stability'] ?? null;
}
}
4 changes: 2 additions & 2 deletions src/bundle/SystemInfo/Value/IbexaSystemInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ class IbexaSystemInfo extends ValueObject implements SystemInfo
/**
* Lowest stability found in the installation (packages / minimumStability).
*
* @var string One of {@see \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\JsonComposerLockSystemInfoCollector::STABILITIES}.
* @var string One of {@see \EzSystems\EzSupportTools\Value\Stability::STABILITIES}.
*/
public $lowestStability;

/**
* @deprecated Instead use $lowestStability.
*
* @var string One of {@see \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\JsonComposerLockSystemInfoCollector::STABILITIES}.
* @var string One of {@see \EzSystems\EzSupportTools\Value\Stability::STABILITIES}.
*/
public $stability;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@
namespace EzSystems\EzSupportToolsBundle\Tests\SystemInfo\Collector;

use EzSystems\EzPlatformCoreBundle\EzPlatformCoreBundle;
use EzSystems\EzSupportTools\VersionStability\VersionStabilityChecker;
use EzSystems\EzSupportToolsBundle\SystemInfo\Collector\IbexaSystemInfoCollector;
use EzSystems\EzSupportToolsBundle\SystemInfo\Collector\JsonComposerLockSystemInfoCollector;
use EzSystems\EzSupportToolsBundle\SystemInfo\Value\IbexaSystemInfo;
use PHPUnit\Framework\TestCase;

class IbexaSystemInfoCollectorTest extends TestCase
{
/** @var \EzSystems\EzSupportTools\VersionStability\VersionStabilityChecker|\PHPUnit\Framework\MockObject\MockObject */
private $composerInstalledVersions;

public function setUp(): void
{
$this->composerInstalledVersions = $this->createMock(VersionStabilityChecker::class);
}

public function testCollect(): void
{
$composerCollector = new JsonComposerLockSystemInfoCollector(
__DIR__ . '/_fixtures/composer.lock', __DIR__ . '/_fixtures/composer.json'
$this->composerInstalledVersions,
__DIR__ . '/_fixtures/composer.lock',
__DIR__ . '/_fixtures/composer.json'
);

$systemInfoCollector = new IbexaSystemInfoCollector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
namespace EzSystems\EzSupportToolsBundle\Tests\SystemInfo\Collector;

use EzSystems\EzSupportTools\VersionStability\VersionStabilityChecker;
use EzSystems\EzSupportToolsBundle\SystemInfo\Collector\JsonComposerLockSystemInfoCollector;
use EzSystems\EzSupportToolsBundle\SystemInfo\Exception\ComposerJsonFileNotFoundException;
use EzSystems\EzSupportToolsBundle\SystemInfo\Exception\ComposerLockFileNotFoundException;
Expand All @@ -15,10 +16,18 @@

class JsonComposerLockSystemInfoCollectorTest extends TestCase
{
/** @var \EzSystems\EzSupportTools\VersionStability\VersionStabilityChecker|\PHPUnit\Framework\MockObject\MockObject */
private $composerInstalledVersions;

public function setUp(): void
{
$this->composerInstalledVersions = $this->createMock(VersionStabilityChecker::class);
}

/**
* @covers \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\JsonComposerLockSystemInfoCollector::collect()
*/
public function testCollect()
public function testCollectWithMinimumStability()
{
$expected = new ComposerSystemInfo([
'packages' => [
Expand Down Expand Up @@ -68,7 +77,11 @@ public function testCollect()
'repositoryUrls' => ['https://updates.ez.no/bul'],
]);

$composerCollector = new JsonComposerLockSystemInfoCollector(__DIR__ . '/_fixtures/composer.lock', __DIR__ . '/_fixtures/composer.json');
$composerCollector = new JsonComposerLockSystemInfoCollector(
$this->composerInstalledVersions,
__DIR__ . '/_fixtures/composer.lock',
__DIR__ . '/_fixtures/composer.json'
);
$value = $composerCollector->collect();

self::assertInstanceOf('EzSystems\EzSupportToolsBundle\SystemInfo\Value\ComposerSystemInfo', $value);
Expand All @@ -82,7 +95,11 @@ public function testCollectLockFileNotFound()
{
$this->expectException(ComposerLockFileNotFoundException::class);

$composerCollectorNotFound = new JsonComposerLockSystemInfoCollector(__DIR__ . '/_fixtures/snafu.lock', __DIR__ . '/_fixtures/composer.json');
$composerCollectorNotFound = new JsonComposerLockSystemInfoCollector(
$this->composerInstalledVersions,
__DIR__ . '/_fixtures/snafu.lock',
__DIR__ . '/_fixtures/composer.json'
);
$composerCollectorNotFound->collect();
}

Expand All @@ -93,7 +110,11 @@ public function testCollectJsonFileNotFound()
{
$this->expectException(ComposerJsonFileNotFoundException::class);

$composerCollectorNotFound = new JsonComposerLockSystemInfoCollector(__DIR__ . '/_fixtures/composer.lock', __DIR__ . '/_fixtures/snafu.json');
$composerCollectorNotFound = new JsonComposerLockSystemInfoCollector(
$this->composerInstalledVersions,
__DIR__ . '/_fixtures/composer.lock',
__DIR__ . '/_fixtures/snafu.json'
);
$composerCollectorNotFound->collect();
}
}
125 changes: 125 additions & 0 deletions src/lib/Tests/VersionStability/VersionStabilityCheckerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzSupportTools\VersionStability;

use EzSystems\EzSupportTools\Value\Stability;
use PHPUnit\Framework\TestCase;

final class VersionStabilityCheckerTest extends TestCase
{
/** @var \EzSystems\EzSupportTools\VersionStability\VersionStabilityChecker */
private $versionStabilityChecker;

public function setUp(): void
{
$this->versionStabilityChecker = new ComposerVersionStabilityChecker();
}

/**
* @dataProvider provideStableVersions
*/
public function testIsStableVersion(string $stableVersion): void
{
self::assertTrue(
$this->versionStabilityChecker->isStableVersion($stableVersion)
);
}

public function provideStableVersions(): iterable
{
yield ['1.0.0.0'];
yield ['1.1.0.0'];
yield ['2.10.5.0'];
yield ['6.1.10.10'];
yield ['15.20.100.1500'];
}

/**
* @dataProvider provideNotStableVersions
*/
public function testIsNotStableVersion(string $notStableVersion): void
{
self::assertFalse(
$this->versionStabilityChecker->isStableVersion($notStableVersion)
);
}

public function provideNotStableVersions(): iterable
{
yield ['1.0.20'];
yield ['1.0.2-beta'];
yield ['1.0.2-beta1'];
yield ['1.0.2-rc'];
yield ['1.0.2-dev'];
yield ['dev-1.0.2'];
yield ['dev-master'];
yield ['dev-main'];
yield ['1.0.2.1-alpha'];
yield ['1.0'];
yield ['v1.0-rc2'];
}

/**
* @dataProvider provideVersionsString
*/
public function testGetStabilityFromVersionString(
string $versionString,
?string $expectedStability = null
): void {
self::assertEquals(
$expectedStability,
$this->versionStabilityChecker->getStabilityFromVersionString($versionString)
);
}

public function provideVersionsString(): iterable
{
yield ['1.0.1.5-RC1', Stability::STABILITIES[5]];
yield ['1.0.1.5-RC2', Stability::STABILITIES[5]];
yield ['1.0.1.5-RC10', Stability::STABILITIES[5]];
yield ['1.0.1.5-beta1', Stability::STABILITIES[10]];
yield ['1.0.1.5-beta12', Stability::STABILITIES[10]];
yield ['1.0.1.5-beta100', Stability::STABILITIES[10]];
yield ['1.1000.1.5-beta1000', Stability::STABILITIES[10]];
yield ['dev-master', Stability::STABILITIES[20]];
yield ['dev-test-v1', Stability::STABILITIES[20]];
yield ['4.0.1-dev', null];
yield ['dev-test_custom', Stability::STABILITIES[20]];
yield ['1.100.10.0-alpha1', Stability::STABILITIES[15]];
yield ['1.0.1.5', null];
yield ['1.0.1', null];
}

/**
* @dataProvider provideVersions
*/
public function testGetStability(
string $version,
string $expectedStability
): void {
self::assertEquals(
$expectedStability,
$this->versionStabilityChecker->getStability($version)
);
}

public function provideVersions(): iterable
{
yield ['0.1.10.50', Stability::STABILITIES[0]];
yield ['10.10.10.50', Stability::STABILITIES[0]];
yield ['1.0.0.1-RC2', Stability::STABILITIES[5]];
yield ['1.0.0.1-RC20', Stability::STABILITIES[5]];
yield ['10.10.10.50-dev', Stability::STABILITIES[20]];
yield ['dev-master', Stability::STABILITIES[20]];
yield ['1.0.0.1-custom', Stability::STABILITIES[20]];
yield ['1.0.0.1-beta5', Stability::STABILITIES[10]];
yield ['1.0.0-alpha5', Stability::STABILITIES[15]];
yield ['1.0.0-alpha51', Stability::STABILITIES[15]];
}
}
Loading

0 comments on commit 6664a74

Please sign in to comment.