Skip to content

Commit

Permalink
test: Add check to ensure the right version of Composer is used (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 5, 2024
1 parent c6d8e3b commit a633e8b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
35 changes: 35 additions & 0 deletions tests/ComposerVersionProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Bamarni\Composer\Bin\Tests;

use Symfony\Component\Process\Process;
use function preg_match;

final class ComposerVersionProvider
{
public static function getComposerVersion(): string
{
$composerVersionProcess = Process::fromShellCommandline(
'composer --version --no-ansi --no-interaction',
null,
null,
null,
1
);

$composerVersionProcess->mustRun();

$output = $composerVersionProcess->getOutput();

return self::extractComposerVersion($output);
}

private static function extractComposerVersion(string $versionOutput): string
{
preg_match('/Composer version (?<version>\d+\.\d+\.\d+) /', $versionOutput, $matches);

return $matches['version'];
}
}
33 changes: 33 additions & 0 deletions tests/ComposerVersionProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Bamarni\Composer\Bin\Tests;

use PHPUnit\Framework\TestCase;
use function preg_match;
use function sprintf;

/**
* @group e2e
* @covers \Bamarni\Composer\Bin\Tests\ComposerVersionProvider
*/
final class ComposerVersionProviderTest extends TestCase
{
public function test_it_can_get_the_composer_version(): void
{
$expectedPattern = '/^\d+\.\d+\.\d+$/';

$actual = ComposerVersionProvider::getComposerVersion();

self::assertSame(
1,
preg_match($expectedPattern, $actual),
sprintf(
'The version "%s" does not match the pattern "%s".',
$actual,
$expectedPattern
)
);
}
}
14 changes: 12 additions & 2 deletions tests/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Bamarni\Composer\Bin\Tests;

use Composer\Semver\Semver;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;

use function array_map;
use function basename;
use function dirname;
Expand All @@ -21,7 +21,6 @@
use function sprintf;
use function str_replace;
use function trim;

use const PHP_EOL;

/**
Expand All @@ -32,6 +31,17 @@ final class EndToEndTest extends TestCase
{
private const E2E_DIR = __DIR__.'/../e2e';

private const COMPOSER_VERSION_CONSTRAINT = '^2.8.2';

public function test_it_has_the_required_composer_version(): void
{
$composerVersion = ComposerVersionProvider::getComposerVersion();

self::assertTrue(
Semver::satisfies($composerVersion, self::COMPOSER_VERSION_CONSTRAINT)
);
}

/**
* @dataProvider scenarioProvider
*/
Expand Down

0 comments on commit a633e8b

Please sign in to comment.