1
0
mirror of https://github.com/php/pie.git synced 2026-03-23 23:12:17 +01:00

Updated tests to work for more PHP versions

This commit is contained in:
James Titcumb
2024-10-01 22:30:30 +01:00
parent db8e0e20f0
commit ba38a2b043
9 changed files with 145 additions and 86 deletions

View File

@@ -8,6 +8,7 @@ use RuntimeException;
use Symfony\Component\Process\Process;
use function array_key_exists;
use function assert;
use function file_exists;
use function is_executable;
use function preg_match;

View File

@@ -11,9 +11,6 @@ use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use const PHP_VERSION;
use const PHP_VERSION_ID;
#[CoversClass(BuildCommand::class)]
class BuildCommandTest extends TestCase
{
@@ -28,10 +25,6 @@ class BuildCommandTest extends TestCase
public function testBuildCommandWillBuildTheExtension(): void
{
if (PHP_VERSION_ID < 80300 || PHP_VERSION_ID >= 80400) {
self::markTestSkipped('This test can only run on PHP 8.3 - you are running ' . PHP_VERSION);
}
$this->commandTester->execute(['requested-package-and-version' => self::TEST_PACKAGE]);
$this->commandTester->assertCommandIsSuccessful();

View File

@@ -46,14 +46,18 @@ class DownloadCommandTest extends TestCase
public static function validVersionsList(): array
{
$versionsAndExpected = [
[self::TEST_PACKAGE, self::TEST_PACKAGE . ':1.0.1'],
[self::TEST_PACKAGE . ':^1.0', self::TEST_PACKAGE . ':1.0.1'],
[self::TEST_PACKAGE . ':1.0.1-alpha.3@alpha', self::TEST_PACKAGE . ':1.0.1-alpha.3'],
[self::TEST_PACKAGE . ':*', self::TEST_PACKAGE . ':1.0.1'],
[self::TEST_PACKAGE . ':~1.0.0@alpha', self::TEST_PACKAGE . ':1.0.1'],
[self::TEST_PACKAGE . ':~1.0.0', self::TEST_PACKAGE . ':1.0.1'],
[self::TEST_PACKAGE, self::TEST_PACKAGE . ':2.0.0'],
[self::TEST_PACKAGE . ':*', self::TEST_PACKAGE . ':2.0.0'],
[self::TEST_PACKAGE . ':^2.0', self::TEST_PACKAGE . ':2.0.0'],
];
if (PHP_VERSION_ID >= 80300 && PHP_VERSION_ID <= 80400) {
$versionsAndExpected[] = [self::TEST_PACKAGE . ':^1.0', self::TEST_PACKAGE . ':1.0.1'];
$versionsAndExpected[] = [self::TEST_PACKAGE . ':1.0.1-alpha.3@alpha', self::TEST_PACKAGE . ':1.0.1-alpha.3'];
$versionsAndExpected[] = [self::TEST_PACKAGE . ':~1.0.0@alpha', self::TEST_PACKAGE . ':1.0.1'];
$versionsAndExpected[] = [self::TEST_PACKAGE . ':~1.0.0', self::TEST_PACKAGE . ':1.0.1'];
}
return array_combine(
array_map(static fn ($item) => $item[0], $versionsAndExpected),
$versionsAndExpected,
@@ -65,10 +69,6 @@ class DownloadCommandTest extends TestCase
string $requestedVersion,
string $expectedVersion,
): void {
if (PHP_VERSION_ID < 80300 || PHP_VERSION_ID >= 80400) {
self::markTestSkipped('This test can only run on PHP 8.3 - you are running ' . PHP_VERSION);
}
$this->commandTester->execute(['requested-package-and-version' => $requestedVersion]);
$this->commandTester->assertCommandIsSuccessful();
@@ -84,10 +84,6 @@ class DownloadCommandTest extends TestCase
self::markTestSkipped('This test can only run on non-Windows systems');
}
if (PHP_VERSION_ID < 80300 || PHP_VERSION_ID >= 80400) {
self::markTestSkipped('This test can only run on PHP 8.3 - you are running ' . PHP_VERSION);
}
$this->commandTester->execute(['requested-package-and-version' => 'asgrim/example-pie-extension:dev-main#9b5e6c80a1e05556e4e6824f0c112a4992cee001']);
$this->commandTester->assertCommandIsSuccessful();

View File

@@ -10,9 +10,6 @@ use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use const PHP_VERSION;
use const PHP_VERSION_ID;
#[CoversClass(InfoCommand::class)]
final class InfoCommandTest extends TestCase
{
@@ -25,10 +22,6 @@ final class InfoCommandTest extends TestCase
public function testInfoCommandDisplaysInformation(): void
{
if (PHP_VERSION_ID < 80300 || PHP_VERSION_ID >= 80400) {
self::markTestSkipped('This test can only run on PHP 8.3 - you are running ' . PHP_VERSION);
}
$this->commandTester->execute(['requested-package-and-version' => 'asgrim/example-pie-extension:dev-main#9b5e6c80a1e05556e4e6824f0c112a4992cee001']);
$this->commandTester->assertCommandIsSuccessful();

View File

@@ -8,22 +8,25 @@ use Composer\Util\Platform;
use Php\Pie\Command\InstallCommand;
use Php\Pie\Container;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Process\Process;
use function array_combine;
use function array_filter;
use function array_key_exists;
use function array_map;
use function array_unshift;
use function assert;
use function file_exists;
use function is_executable;
use function is_file;
use function is_string;
use function is_writable;
use function preg_match;
use const PHP_VERSION;
use const PHP_VERSION_ID;
#[CoversClass(InstallCommand::class)]
class InstallCommandTest extends TestCase
{
@@ -36,17 +39,43 @@ class InstallCommandTest extends TestCase
$this->commandTester = new CommandTester(Container::factory()->get(InstallCommand::class));
}
public function testInstallCommandWillInstallCompatibleExtensionNonWindows(): void
/**
* @return array<string, array{0: string}>
*
* @psalm-suppress PossiblyUnusedMethod https://github.com/psalm/psalm-plugin-phpunit/issues/131
*/
public static function phpPathProvider(): array
{
if (PHP_VERSION_ID < 80300 || PHP_VERSION_ID >= 80400) {
self::markTestSkipped('This test can only run on PHP 8.3 - you are running ' . PHP_VERSION);
}
$possiblePhpConfigPaths = array_filter(
[
'/usr/bin/php-config',
'/usr/bin/php-config8.3',
'/usr/bin/php-config8.2',
'/usr/bin/php-config8.1',
'/usr/bin/php-config8.0',
'/usr/bin/php-config7.4',
],
static fn (string $phpConfigPath) => file_exists($phpConfigPath)
&& is_executable($phpConfigPath),
);
return array_combine(
$possiblePhpConfigPaths,
array_map(static fn (string $phpConfigPath) => [$phpConfigPath], $possiblePhpConfigPaths),
);
}
#[DataProvider('phpPathProvider')]
public function testInstallCommandWillInstallCompatibleExtensionNonWindows(string $phpConfigPath): void
{
if (Platform::isWindows()) {
self::markTestSkipped('This test can only run on non-Windows systems');
}
$this->commandTester->execute(['requested-package-and-version' => self::TEST_PACKAGE]);
$this->commandTester->execute(
['requested-package-and-version' => self::TEST_PACKAGE, '--with-php-config' => $phpConfigPath],
['verbosity' => BufferedOutput::VERBOSITY_VERY_VERBOSE],
);
$this->commandTester->assertCommandIsSuccessful();
@@ -76,10 +105,6 @@ class InstallCommandTest extends TestCase
public function testInstallCommandWillInstallCompatibleExtensionWindows(): void
{
if (PHP_VERSION_ID < 80300 || PHP_VERSION_ID >= 80400) {
self::markTestSkipped('This test can only run on PHP 8.3 - you are running ' . PHP_VERSION);
}
if (! Platform::isWindows()) {
self::markTestSkipped('This test can only run on Windows systems');
}

View File

@@ -16,7 +16,6 @@ use PHPUnit\Framework\TestCase;
use function array_combine;
use function array_map;
use const PHP_VERSION;
use const PHP_VERSION_ID;
#[CoversClass(ResolveDependencyWithComposer::class)]
@@ -35,22 +34,25 @@ final class ResolveDependencyWithComposerTest extends TestCase
public static function validVersionsList(): array
{
$versionsAndExpected = [
[null, '1.0.1', self::DOWNLOAD_URL_ANY],
['*', '1.0.1', self::DOWNLOAD_URL_ANY],
['1.0.1-alpha.3@alpha', '1.0.1-alpha.3', self::DOWNLOAD_URL_1_0_1_ALPHA_3],
['^1.0', '1.0.1', self::DOWNLOAD_URL_1_0_1],
['^1.1.0@alpha', '1.1.0-beta.1', self::DOWNLOAD_URL_1_1_0_BETA_1],
['^1.0@beta', '1.0.1', self::DOWNLOAD_URL_1_0_1],
['^1.1@beta', '1.1.0-beta.1', self::DOWNLOAD_URL_1_1_0_BETA_1],
['~1.0.0', '1.0.1', self::DOWNLOAD_URL_1_0_1],
['~1.0.0@alpha', '1.0.1', self::DOWNLOAD_URL_1_0_1],
['~1.0.0@beta', '1.0.1', self::DOWNLOAD_URL_1_0_1],
['~1.0@beta', '1.0.1', self::DOWNLOAD_URL_1_0_1],
[null, '2.0.0', self::DOWNLOAD_URL_ANY],
['*', '2.0.0', self::DOWNLOAD_URL_ANY],
['dev-main', 'dev-main', self::DOWNLOAD_URL_ANY],
['dev-main#769f906413d6d1e12152f6d34134cbcd347ca253', 'dev-main', self::DOWNLOAD_URL_1_0_1],
['v1.x-dev', 'v1.x-dev', self::DOWNLOAD_URL_1_1_0_BETA_1],
];
if (PHP_VERSION_ID >= 80300 && PHP_VERSION_ID <= 80400) {
$versionsAndExpected[] = ['1.0.1-alpha.3@alpha', '1.0.1-alpha.3', self::DOWNLOAD_URL_1_0_1_ALPHA_3];
$versionsAndExpected[] = ['^1.0', '1.0.1', self::DOWNLOAD_URL_1_0_1];
$versionsAndExpected[] = ['^1.1.0@alpha', '1.1.0-beta.1', self::DOWNLOAD_URL_1_1_0_BETA_1];
$versionsAndExpected[] = ['^1.0@beta', '1.0.1', self::DOWNLOAD_URL_1_0_1];
$versionsAndExpected[] = ['^1.1@beta', '1.1.0-beta.1', self::DOWNLOAD_URL_1_1_0_BETA_1];
$versionsAndExpected[] = ['~1.0.0', '1.0.1', self::DOWNLOAD_URL_1_0_1];
$versionsAndExpected[] = ['~1.0.0@alpha', '1.0.1', self::DOWNLOAD_URL_1_0_1];
$versionsAndExpected[] = ['~1.0.0@beta', '1.0.1', self::DOWNLOAD_URL_1_0_1];
$versionsAndExpected[] = ['~1.0@beta', '1.0.1', self::DOWNLOAD_URL_1_0_1];
$versionsAndExpected[] = ['v1.x-dev', 'v1.x-dev', self::DOWNLOAD_URL_1_1_0_BETA_1];
}
return array_combine(
array_map(static fn ($item) => $item[0] ?? 'null', $versionsAndExpected),
$versionsAndExpected,
@@ -63,10 +65,6 @@ final class ResolveDependencyWithComposerTest extends TestCase
string $expectedVersion,
string $expectedDownloadUrl,
): void {
if (PHP_VERSION_ID < 80300 || PHP_VERSION_ID >= 80400) {
self::markTestSkipped('This test can only run on PHP 8.3 - you are running ' . PHP_VERSION);
}
$container = Container::factory();
$resolve = $container->get(DependencyResolver::class);

View File

@@ -15,12 +15,18 @@ use Php\Pie\Installing\UnixInstall;
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
use Php\Pie\Platform\TargetPlatform;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Process\Process;
use function array_combine;
use function array_filter;
use function array_map;
use function array_unshift;
use function assert;
use function file_exists;
use function is_executable;
use function is_writable;
#[CoversClass(UnixInstall::class)]
@@ -28,14 +34,43 @@ final class UnixInstallTest extends TestCase
{
private const TEST_EXTENSION_PATH = __DIR__ . '/../../assets/pie_test_ext';
public function testUnixInstallCanInstallExtension(): void
/**
* @return array<string, array{0: non-empty-string}>
*
* @psalm-suppress PossiblyUnusedMethod https://github.com/psalm/psalm-plugin-phpunit/issues/131
*/
public static function phpPathProvider(): array
{
$possiblePhpConfigPaths = array_filter(
[
'/usr/bin/php-config',
'/usr/bin/php-config8.4',
'/usr/bin/php-config8.3',
'/usr/bin/php-config8.2',
'/usr/bin/php-config8.1',
'/usr/bin/php-config8.0',
'/usr/bin/php-config7.4',
],
static fn (string $phpConfigPath) => file_exists($phpConfigPath)
&& is_executable($phpConfigPath),
);
return array_combine(
$possiblePhpConfigPaths,
array_map(static fn (string $phpConfigPath) => [$phpConfigPath], $possiblePhpConfigPaths),
);
}
#[DataProvider('phpPathProvider')]
public function testUnixInstallCanInstallExtension(string $phpConfig): void
{
assert($phpConfig !== '');
if (Platform::isWindows()) {
self::markTestSkipped('Unix build test cannot be run on Windows');
}
$output = new BufferedOutput();
$targetPlatform = TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess());
$targetPlatform = TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromPhpConfigExecutable($phpConfig));
$extensionPath = $targetPlatform->phpBinaryPath->extensionPath();
$downloadedPackage = DownloadedPackage::fromPackageAndExtractedPath(
@@ -58,7 +93,7 @@ final class UnixInstallTest extends TestCase
$downloadedPackage,
$targetPlatform,
['--enable-pie_test_ext'],
new NullOutput(),
$output,
);
$installedSharedObject = (new UnixInstall())->__invoke(
@@ -66,8 +101,7 @@ final class UnixInstallTest extends TestCase
$targetPlatform,
$output,
);
$outputString = $output->fetch();
$outputString = $output->fetch();
self::assertStringContainsString('Install complete: ' . $extensionPath . '/pie_test_ext.so', $outputString);
self::assertStringContainsString('You must now add "extension=pie_test_ext" to your php.ini', $outputString);

View File

@@ -13,8 +13,8 @@ use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
use function array_column;
use function array_combine;
use function array_filter;
use function array_map;
@@ -30,7 +30,6 @@ use function is_executable;
use function php_uname;
use function phpversion;
use function sprintf;
use function trim;
use const DIRECTORY_SEPARATOR;
use const PHP_INT_SIZE;
@@ -87,28 +86,43 @@ final class PhpBinaryPathTest extends TestCase
self::assertNull($phpBinary->phpConfigPath());
}
public function testFromPhpConfigExecutable(): void
/**
* @return array<string, array{0: non-empty-string, 1: non-empty-string}>
*
* @psalm-suppress PossiblyUnusedMethod https://github.com/psalm/psalm-plugin-phpunit/issues/131
*/
public static function phpConfigPathProvider(): array
{
$process = (new Process(['which', 'php-config']));
$exitCode = $process->run();
$phpConfigExecutable = trim($process->getOutput());
if ($exitCode !== 0 || ! file_exists($phpConfigExecutable) || ! is_executable($phpConfigExecutable) || $phpConfigExecutable === '') {
self::markTestSkipped('Needs php-config in path to run this test');
}
$phpBinary = PhpBinaryPath::fromPhpConfigExecutable($phpConfigExecutable);
// NOTE: this makes an assumption that the `php-config` in path is the same as the version being executed
// In most cases, this will be the cases (e.g. in CI, running locally), but if you're trying to test this and
// the versions are not matching, that's probably why.
// @todo improve this assertion in future, if it becomes problematic
self::assertSame(
sprintf('%s.%s.%s', PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION),
$phpBinary->version(),
$possiblePhpConfigPaths = array_filter(
[
['/usr/bin/php-config8.3', '8.3'],
['/usr/bin/php-config8.2', '8.2'],
['/usr/bin/php-config8.1', '8.1'],
['/usr/bin/php-config8.0', '8.0'],
['/usr/bin/php-config7.4', '7.4'],
],
static fn (array $phpConfigPath) => file_exists($phpConfigPath[0])
&& is_executable($phpConfigPath[0]),
);
self::assertSame($phpConfigExecutable, $phpBinary->phpConfigPath());
return array_combine(
array_column($possiblePhpConfigPaths, 0),
$possiblePhpConfigPaths,
);
}
#[DataProvider('phpConfigPathProvider')]
public function testFromPhpConfigExecutable(string $phpConfigPath, string $expectedMajorMinor): void
{
assert($phpConfigPath !== '');
$phpBinary = PhpBinaryPath::fromPhpConfigExecutable($phpConfigPath);
self::assertSame(
$expectedMajorMinor,
$phpBinary->majorMinorVersion(),
);
self::assertSame($phpConfigPath, $phpBinary->phpConfigPath());
}
public function testExtensions(): void
@@ -195,7 +209,7 @@ final class PhpBinaryPathTest extends TestCase
*
* @psalm-suppress PossiblyUnusedMethod https://github.com/psalm/psalm-plugin-phpunit/issues/131
*/
public function phpPathProvider(): array
public static function phpPathProvider(): array
{
$possiblePhpBinaries = array_filter(
array_unique([

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Php\PieUnitTest\Platform\TargetPhp;
use Composer\Util\Platform;
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
use Php\Pie\Platform\TargetPhp\PhpizePath;
use PHPUnit\Framework\Attributes\CoversClass;
@@ -26,7 +27,7 @@ final class PhpizePathTest extends TestCase
*
* @psalm-suppress PossiblyUnusedMethod https://github.com/psalm/psalm-plugin-phpunit/issues/131
*/
public function phpPathProvider(): array
public static function phpPathProvider(): array
{
$possiblePhpBinaries = array_filter(
array_unique([
@@ -56,6 +57,10 @@ final class PhpizePathTest extends TestCase
#[DataProvider('phpPathProvider')]
public function testGuessingFindsPhpizePath(): void
{
if (Platform::isWindows()) {
self::markTestSkipped('Guessing phpize path is not done for Windows as we are not building for Windows (yet)');
}
$phpize = PhpizePath::guessFrom(PhpBinaryPath::fromCurrentProcess());
self::assertNotEmpty($phpize->phpizeBinaryPath);