|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Php\PieIntegrationTest\Building; |
| 6 | + |
| 7 | +use Composer\Util\Platform; |
| 8 | +use Php\Pie\Building\PcreCompilerFlags; |
| 9 | +use Php\Pie\Platform\Architecture; |
| 10 | +use Php\Pie\Platform\OperatingSystem; |
| 11 | +use Php\Pie\Platform\OperatingSystemFamily; |
| 12 | +use Php\Pie\Platform\TargetPhp\PhpBinaryPath; |
| 13 | +use Php\Pie\Platform\TargetPlatform; |
| 14 | +use Php\Pie\Platform\ThreadSafetyMode; |
| 15 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +use function getenv; |
| 19 | +use function Safe\realpath; |
| 20 | + |
| 21 | +use const PATH_SEPARATOR; |
| 22 | + |
| 23 | +#[CoversClass(PcreCompilerFlags::class)] |
| 24 | +final class PcreCompilerFlagsTest extends TestCase |
| 25 | +{ |
| 26 | + private const FAKE_PKG_CONFIG_PATH = __DIR__ . '/../../assets/fake-pkg-config-pcre'; |
| 27 | + private const PATH_WITHOUT_PKG_CONFIG = __DIR__; |
| 28 | + |
| 29 | + /** @var array<array-key, mixed> */ |
| 30 | + private array $oldEnv; |
| 31 | + |
| 32 | + public function setUp(): void |
| 33 | + { |
| 34 | + if (Platform::isWindows()) { |
| 35 | + self::markTestSkipped('Bash script does not run on Windows.'); |
| 36 | + } |
| 37 | + |
| 38 | + $this->oldEnv = $_ENV; |
| 39 | + } |
| 40 | + |
| 41 | + protected function tearDown(): void |
| 42 | + { |
| 43 | + $_ENV = $this->oldEnv; |
| 44 | + } |
| 45 | + |
| 46 | + public function testFlagsFromPkgConfigAreReturnedWhenPhpUsesExternalPcre(): void |
| 47 | + { |
| 48 | + $_ENV['PATH'] = realpath(self::FAKE_PKG_CONFIG_PATH) . PATH_SEPARATOR . getenv('PATH'); |
| 49 | + $_ENV['PIE_TEST_PCRE_CFLAGS'] = '-I/opt/homebrew/Cellar/pcre2/10.49/include'; |
| 50 | + |
| 51 | + self::assertSame( |
| 52 | + '-I/opt/homebrew/Cellar/pcre2/10.49/include', |
| 53 | + PcreCompilerFlags::forTargetPlatform($this->targetPlatformWherePhpUsesExternalPcre(true)), |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + public function testNoFlagsWhenPhpUsesBundledPcre(): void |
| 58 | + { |
| 59 | + $_ENV['PATH'] = realpath(self::FAKE_PKG_CONFIG_PATH) . PATH_SEPARATOR . getenv('PATH'); |
| 60 | + $_ENV['PIE_TEST_PCRE_CFLAGS'] = '-I/opt/homebrew/Cellar/pcre2/10.49/include'; |
| 61 | + |
| 62 | + self::assertNull(PcreCompilerFlags::forTargetPlatform($this->targetPlatformWherePhpUsesExternalPcre(false))); |
| 63 | + } |
| 64 | + |
| 65 | + public function testNoFlagsWhenPkgConfigReportsNone(): void |
| 66 | + { |
| 67 | + $_ENV['PATH'] = realpath(self::FAKE_PKG_CONFIG_PATH) . PATH_SEPARATOR . getenv('PATH'); |
| 68 | + $_ENV['PIE_TEST_PCRE_CFLAGS'] = ''; |
| 69 | + |
| 70 | + self::assertNull(PcreCompilerFlags::forTargetPlatform($this->targetPlatformWherePhpUsesExternalPcre(true))); |
| 71 | + } |
| 72 | + |
| 73 | + public function testNoFlagsWhenPkgConfigIsNotAvailable(): void |
| 74 | + { |
| 75 | + $_ENV['PATH'] = realpath(self::PATH_WITHOUT_PKG_CONFIG); |
| 76 | + |
| 77 | + self::assertNull(PcreCompilerFlags::forTargetPlatform($this->targetPlatformWherePhpUsesExternalPcre(true))); |
| 78 | + } |
| 79 | + |
| 80 | + private function targetPlatformWherePhpUsesExternalPcre(bool $usesExternalPcre): TargetPlatform |
| 81 | + { |
| 82 | + $phpBinaryPath = $this->createMock(PhpBinaryPath::class); |
| 83 | + $phpBinaryPath->method('usesExternalPcre')->willReturn($usesExternalPcre); |
| 84 | + |
| 85 | + return new TargetPlatform( |
| 86 | + OperatingSystem::NonWindows, |
| 87 | + OperatingSystemFamily::Linux, |
| 88 | + $phpBinaryPath, |
| 89 | + Architecture::x86_64, |
| 90 | + ThreadSafetyMode::NonThreadSafe, |
| 91 | + 1, |
| 92 | + null, |
| 93 | + null, |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
0 commit comments