Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add check to ensure the right version of Composer is used #164

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading