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

Trim warnings and deprecations from output when invoking PHP in PhpBinaryPath #158

Merged
merged 2 commits into from
Dec 23, 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
5 changes: 5 additions & 0 deletions features/install-extensions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Feature: Extensions can be installed with Behat
When I run a command to build an extension
Then the extension should have been built

Example: An extension can be built with warnings at PHP startup
Given I have an invalid extension installed
When I run a command to build an extension
Then the extension should have been built

Example: An extension can be built with configure options
When I run a command to build an extension with configure options
Then the extension should have been built with options
Expand Down
48 changes: 32 additions & 16 deletions src/Platform/TargetPhp/PhpBinaryPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use function dirname;
use function explode;
use function file_exists;
use function implode;
use function is_dir;
use function is_executable;
use function preg_match;
Expand Down Expand Up @@ -58,7 +59,7 @@ private static function assertValidLookingPhpBinary(string $phpBinaryPath): void

// This is somewhat of a rudimentary check that the target PHP really is a PHP instance; not sure why you
// WOULDN'T want to use a real PHP, but this should stop obvious hiccups at least (rather than for security)
$testOutput = Process::run([$phpBinaryPath, '-r', 'echo "PHP";']);
$testOutput = self::cleanWarningAndDeprecationsFromOutput(Process::run([$phpBinaryPath, '-r', 'echo "PHP";']));

if ($testOutput !== 'PHP') {
throw Exception\InvalidPhpBinaryPath::fromInvalidPhpBinary($phpBinaryPath);
Expand Down Expand Up @@ -116,7 +117,7 @@ public function extensionPath(): string
*/
public function extensions(): array
{
$extVersionsList = Process::run([
$extVersionsList = self::cleanWarningAndDeprecationsFromOutput(Process::run([
$this->phpBinaryPath,
'-r',
<<<'PHP'
Expand All @@ -139,7 +140,7 @@ static function ($k, $v) {
$extVersions
));
PHP,
]);
]));

$pairs = array_map(
static fn (string $row) => explode(':', $row),
Expand All @@ -154,11 +155,11 @@ static function ($k, $v) {

public function operatingSystem(): OperatingSystem
{
$winOrNot = Process::run([
$winOrNot = self::cleanWarningAndDeprecationsFromOutput(Process::run([
$this->phpBinaryPath,
'-r',
'echo \\defined(\'PHP_WINDOWS_VERSION_BUILD\') ? \'win\' : \'not\';',
]);
]));
Assert::stringNotEmpty($winOrNot, 'Could not determine PHP version');

return $winOrNot === 'win' ? OperatingSystem::Windows : OperatingSystem::NonWindows;
Expand All @@ -167,11 +168,11 @@ public function operatingSystem(): OperatingSystem
/** @return non-empty-string */
public function version(): string
{
$phpVersion = Process::run([
$phpVersion = self::cleanWarningAndDeprecationsFromOutput(Process::run([
$this->phpBinaryPath,
'-r',
'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION . "." . PHP_RELEASE_VERSION;',
]);
]));
Assert::stringNotEmpty($phpVersion, 'Could not determine PHP version');

// normalizing the version will throw an exception if it is not a valid version
Expand All @@ -183,11 +184,11 @@ public function version(): string
/** @return non-empty-string */
public function majorMinorVersion(): string
{
$phpVersion = Process::run([
$phpVersion = self::cleanWarningAndDeprecationsFromOutput(Process::run([
$this->phpBinaryPath,
'-r',
'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;',
]);
]));
Assert::stringNotEmpty($phpVersion, 'Could not determine PHP version');

// normalizing the version will throw an exception if it is not a valid version
Expand All @@ -198,23 +199,23 @@ public function majorMinorVersion(): string

public function machineType(): Architecture
{
$phpMachineType = Process::run([
$phpMachineType = self::cleanWarningAndDeprecationsFromOutput(Process::run([
$this->phpBinaryPath,
'-r',
'echo php_uname("m");',
]);
]));
Assert::stringNotEmpty($phpMachineType, 'Could not determine PHP machine type');

return Architecture::parseArchitecture($phpMachineType);
}

public function phpIntSize(): int
{
$phpIntSize = Process::run([
$phpIntSize = self::cleanWarningAndDeprecationsFromOutput(Process::run([
$this->phpBinaryPath,
'-r',
'echo PHP_INT_SIZE;',
]);
]));
Assert::stringNotEmpty($phpIntSize, 'Could not fetch PHP_INT_SIZE');
Assert::same($phpIntSize, (string) (int) $phpIntSize, 'PHP_INT_SIZE was not an integer processed %2$s from %s');

Expand All @@ -224,10 +225,10 @@ public function phpIntSize(): int
/** @return non-empty-string */
public function phpinfo(): string
{
$phpInfo = Process::run([
$phpInfo = self::cleanWarningAndDeprecationsFromOutput(Process::run([
$this->phpBinaryPath,
'-i',
]);
]));

Assert::stringNotEmpty($phpInfo, sprintf('Could not run phpinfo using %s', $this->phpBinaryPath));

Expand All @@ -248,7 +249,7 @@ public function phpConfigPath(): string|null
/** @param non-empty-string $phpConfig */
public static function fromPhpConfigExecutable(string $phpConfig): self
{
$phpExecutable = Process::run([$phpConfig, '--php-binary']);
$phpExecutable = self::cleanWarningAndDeprecationsFromOutput(Process::run([$phpConfig, '--php-binary']));
Assert::stringNotEmpty($phpExecutable, 'Could not find path to PHP executable.');

self::assertValidLookingPhpBinary($phpExecutable);
Expand All @@ -273,4 +274,19 @@ public static function fromCurrentProcess(): self

return new self($phpExecutable, null);
}

private static function cleanWarningAndDeprecationsFromOutput(string $testOutput): string
{
$testOutput = explode("\n", $testOutput);

foreach ($testOutput as $key => $line) {
if (! preg_match('/^(Deprecated|Warning):/', $line)) {
continue;
}

unset($testOutput[$key]);
}

return implode("\n", $testOutput);
}
}
11 changes: 10 additions & 1 deletion test/behaviour/CliContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Php\PieBehaviourTest;

use Behat\Behat\Context\Context;
use Behat\Step\Given;
use Behat\Step\Then;
use Behat\Step\When;
use Composer\Util\Platform;
Expand All @@ -18,6 +19,8 @@ class CliContext implements Context
{
private string|null $output = null;
private int|null $exitCode = null;
/** @var list<string> */
private array $phpArguments = [];

#[When('I run a command to download the latest version of an extension')]
public function iRunACommandToDownloadTheLatestVersionOfAnExtension(): void
Expand All @@ -34,7 +37,7 @@ public function iRunACommandToDownloadSpecificVersionOfAnExtension(string $versi
/** @param list<non-empty-string> $command */
public function runPieCommand(array $command): void
{
$pieCommand = array_merge(['php', 'bin/pie'], $command);
$pieCommand = array_merge(['php', ...$this->phpArguments, 'bin/pie'], $command);

$proc = (new Process($pieCommand))->mustRun();

Expand Down Expand Up @@ -128,4 +131,10 @@ public function theExtensionShouldHaveBeenInstalled(): void

Assert::regex($this->output, '#Install complete: [-_a-zA-Z0-9/]+/example_pie_extension.so#');
}

#[Given('I have an invalid extension installed')]
public function iHaveAnInvalidExtensionInstalled(): void
{
$this->phpArguments = ['-d', 'extension=invalid_extension'];
}
}
Loading