From 4e06aa051a7d27966fe9b0bd548cc2fc0e6fa55f Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Tue, 18 Aug 2020 16:00:44 +0200 Subject: [PATCH 1/4] Check if inet_pton() exists --- src/Composer/Platform/Runtime.php | 9 +++++++++ src/Composer/Repository/PlatformRepository.php | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Composer/Platform/Runtime.php b/src/Composer/Platform/Runtime.php index 82211ec88..3c77a9250 100644 --- a/src/Composer/Platform/Runtime.php +++ b/src/Composer/Platform/Runtime.php @@ -34,6 +34,15 @@ class Runtime return constant(ltrim($class.'::'.$constant, ':')); } + /** + * @param string $fn + * @return bool + */ + public function hasFunction($fn) + { + return function_exists($fn); + } + /** * @param callable $callable * @param array $arguments diff --git a/src/Composer/Repository/PlatformRepository.php b/src/Composer/Repository/PlatformRepository.php index 5a8f70627..c72c88f14 100644 --- a/src/Composer/Repository/PlatformRepository.php +++ b/src/Composer/Repository/PlatformRepository.php @@ -125,7 +125,7 @@ class PlatformRepository extends ArrayRepository // The AF_INET6 constant is only defined if ext-sockets is available but // IPv6 support might still be available. - if ($this->runtime->hasConstant('AF_INET6') || Silencer::call(array($this->runtime, 'invoke'), array('inet_pton', '::')) !== false) { + if ($this->runtime->hasConstant('AF_INET6') || ($this->runtime->hasFunction('inet_pton') && Silencer::call(array($this->runtime, 'invoke'), array('inet_pton', '::')) !== false)) { $phpIpv6 = new CompletePackage('php-ipv6', $version, $prettyVersion); $phpIpv6->setDescription('The PHP interpreter, with IPv6 support'); $this->addPackage($phpIpv6); From 99fd5c7b493471e2437ccfa7992656719b9e9c74 Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Tue, 18 Aug 2020 16:05:40 +0200 Subject: [PATCH 2/4] Add tests --- .../Repository/PlatformRepositoryTest.php | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/Composer/Test/Repository/PlatformRepositoryTest.php b/tests/Composer/Test/Repository/PlatformRepositoryTest.php index d90fcbc73..a608ed7ff 100644 --- a/tests/Composer/Test/Repository/PlatformRepositoryTest.php +++ b/tests/Composer/Test/Repository/PlatformRepositoryTest.php @@ -95,13 +95,28 @@ class PlatformRepositoryTest extends TestCase ), array( array('inet_pton', array('::'), ''), + ), + array( + array('inet_pton', true), + ) + ), + array( + array( + 'PHP_VERSION' => '7.2.31-1+ubuntu16.04.1+deb.sury.org+1', + ), + array( + 'php' => '7.2.31', + ), + array(), + array( + 'inet_pton' => false, ) ) ); } /** @dataProvider getPhpFlavorTestCases */ - public function testPhpVersion(array $constants, array $packages, array $functions = array()) + public function testPhpVersion(array $constants, array $packages, array $functionMap = array(), array $functionExists = array()) { $runtime = $this->getMockBuilder('Composer\Platform\Runtime')->getMock(); $runtime @@ -123,7 +138,11 @@ class PlatformRepositoryTest extends TestCase ); $runtime ->method('invoke') - ->willReturnMap($functions); + ->willReturnMap($functionMap); + + $runtime + ->method('hasFunction') + ->willReturnMap($functionExists); $repository = new PlatformRepository(array(), array(), $runtime); foreach ($packages as $packageName => $version) { From a83588f568ff6b92fcedc0a2788372bbaa4fa6c2 Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Tue, 18 Aug 2020 16:30:47 +0200 Subject: [PATCH 3/4] The proper fix --- .../Repository/PlatformRepository.php | 2 +- .../Repository/PlatformRepositoryTest.php | 51 ++++++++++++++----- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/Composer/Repository/PlatformRepository.php b/src/Composer/Repository/PlatformRepository.php index c72c88f14..ac6d49525 100644 --- a/src/Composer/Repository/PlatformRepository.php +++ b/src/Composer/Repository/PlatformRepository.php @@ -125,7 +125,7 @@ class PlatformRepository extends ArrayRepository // The AF_INET6 constant is only defined if ext-sockets is available but // IPv6 support might still be available. - if ($this->runtime->hasConstant('AF_INET6') || ($this->runtime->hasFunction('inet_pton') && Silencer::call(array($this->runtime, 'invoke'), array('inet_pton', '::')) !== false)) { + if ($this->runtime->hasConstant('AF_INET6') || Silencer::call(array($this->runtime, 'invoke'), 'inet_pton', array('::')) !== false) { $phpIpv6 = new CompletePackage('php-ipv6', $version, $prettyVersion); $phpIpv6->setDescription('The PHP interpreter, with IPv6 support'); $this->addPackage($phpIpv6); diff --git a/tests/Composer/Test/Repository/PlatformRepositoryTest.php b/tests/Composer/Test/Repository/PlatformRepositoryTest.php index a608ed7ff..c96a00f51 100644 --- a/tests/Composer/Test/Repository/PlatformRepositoryTest.php +++ b/tests/Composer/Test/Repository/PlatformRepositoryTest.php @@ -96,9 +96,6 @@ class PlatformRepositoryTest extends TestCase array( array('inet_pton', array('::'), ''), ), - array( - array('inet_pton', true), - ) ), array( array( @@ -107,16 +104,15 @@ class PlatformRepositoryTest extends TestCase array( 'php' => '7.2.31', ), - array(), array( - 'inet_pton' => false, - ) - ) + array('inet_pton', array('::'), false), + ), + ), ); } /** @dataProvider getPhpFlavorTestCases */ - public function testPhpVersion(array $constants, array $packages, array $functionMap = array(), array $functionExists = array()) + public function testPhpVersion(array $constants, array $packages, array $functions = array()) { $runtime = $this->getMockBuilder('Composer\Platform\Runtime')->getMock(); $runtime @@ -138,11 +134,7 @@ class PlatformRepositoryTest extends TestCase ); $runtime ->method('invoke') - ->willReturnMap($functionMap); - - $runtime - ->method('hasFunction') - ->willReturnMap($functionExists); + ->willReturnMap($functions); $repository = new PlatformRepository(array(), array(), $runtime); foreach ($packages as $packageName => $version) { @@ -152,6 +144,39 @@ class PlatformRepositoryTest extends TestCase } } + public function testInetPtonRegressiom() + { + $runtime = $this->getMockBuilder('Composer\Platform\Runtime')->getMock(); + + $runtime + ->expects(self::once()) + ->method('invoke') + ->with('inet_pton', array('::')) + ->willReturn(false); + $runtime + ->method('hasConstant') + ->willReturnMap( + array( + array('PHP_ZTS', false), + array('AF_INET6', false), + ) + ); + $runtime + ->method('getExtensions') + ->willReturn(array()); + $runtime + ->method('getConstant') + ->willReturnMap( + array( + array('PHP_VERSION', null, '7.0.0'), + array('PHP_DEBUG', null, false), + ) + ); + $repository = new PlatformRepository(array(), array(), $runtime); + $package = $repository->findPackage('php-ipv6', '*'); + self::assertNull($package); + } + public static function getLibraryTestCases() { return array( From 3e750b69f42e063abb1fd5560c9589dbd3be2ed8 Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Tue, 18 Aug 2020 16:31:46 +0200 Subject: [PATCH 4/4] Fix name --- tests/Composer/Test/Repository/PlatformRepositoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Composer/Test/Repository/PlatformRepositoryTest.php b/tests/Composer/Test/Repository/PlatformRepositoryTest.php index c96a00f51..2da09c1dd 100644 --- a/tests/Composer/Test/Repository/PlatformRepositoryTest.php +++ b/tests/Composer/Test/Repository/PlatformRepositoryTest.php @@ -144,7 +144,7 @@ class PlatformRepositoryTest extends TestCase } } - public function testInetPtonRegressiom() + public function testInetPtonRegression() { $runtime = $this->getMockBuilder('Composer\Platform\Runtime')->getMock();