From 67b9394d62c60d094733246d3eaa5b0e1e9dcbee Mon Sep 17 00:00:00 2001 From: Ivan Fedorov Date: Wed, 27 Nov 2024 17:11:44 +0100 Subject: [PATCH] [phpstorm-stubs] fix test that checks constants as default values --- random/random.php | 3 ++ standard/standard_defines.php | 2 +- tests/AbstractBaseStubsTestCase.php | 4 +- .../StubsConstantsAndParametersValuesTest.php | 54 +++++++++---------- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/random/random.php b/random/random.php index 11bb39e1d..53cdfb22d 100644 --- a/random/random.php +++ b/random/random.php @@ -113,6 +113,9 @@ function random_int(int $min, int $max): int {} namespace Random\Engine { + + use const MT_RAND_MT19937; + /** * @since 8.2 */ diff --git a/standard/standard_defines.php b/standard/standard_defines.php index 51a3b1117..abdde0821 100644 --- a/standard/standard_defines.php +++ b/standard/standard_defines.php @@ -1264,7 +1264,7 @@ /** * @since 7.1 */ -define("MT_RAND_MT19937", 0); +define('MT_RAND_MT19937', 0); /** * @since 7.1 * @deprecated 8.3 diff --git a/tests/AbstractBaseStubsTestCase.php b/tests/AbstractBaseStubsTestCase.php index 4a1528073..ff5bf0097 100644 --- a/tests/AbstractBaseStubsTestCase.php +++ b/tests/AbstractBaseStubsTestCase.php @@ -85,7 +85,7 @@ public static function getStringRepresentationOfDefaultParameterValue(mixed $def throw new Exception("Class $class not found in stubs"); } if ($parentClass instanceof PHPEnum) { - $value = $parentClass->name . "::" . $defaultValue->name; + $value = $parentClass->id . "::" . $defaultValue->name; } elseif ((string)$defaultValue->name === 'class') { $value = (string)$defaultValue->class; } else { @@ -97,7 +97,7 @@ public static function getStringRepresentationOfDefaultParameterValue(mixed $def } elseif (is_array($defaultValue) || $defaultValue instanceof Array_) { $value = '[]'; } elseif ($defaultValue instanceof UnitEnum){ - $value = get_class($defaultValue) . "::" . $defaultValue->name; + $value = "\\" . get_class($defaultValue) . "::" . $defaultValue->name; } else { $value = strval($defaultValue); } diff --git a/tests/StubsConstantsAndParametersValuesTest.php b/tests/StubsConstantsAndParametersValuesTest.php index cf906212b..d70c202b8 100644 --- a/tests/StubsConstantsAndParametersValuesTest.php +++ b/tests/StubsConstantsAndParametersValuesTest.php @@ -92,33 +92,33 @@ public function testFunctionsWithoutOptionalDefaultParametersValue(?string $func ); } -// #[DataProviderExternal(ReflectionParametersProvider::class, 'classMethodOptionalParametersWithDefaultValueProvider')] -// public function testClassMethodsDefaultParametersValue(?string $classId, ?string $methodName, ?string $parameterName) -// { -// if (!$classId && !$methodName && !$parameterName) { -// self::markTestSkipped($this->emptyDataSetMessage); -// } -// $reflectionParameter = ReflectionStubsSingleton::getReflectionStubs()->getClass($classId, fromReflection: true)->getMethod($methodName, fromReflection: true)->getParameter($parameterName); -// $stubClass = PhpStormStubsSingleton::getPhpStormStubs()->getClass($classId); -// $phpstormFunction = $stubClass->getMethod($methodName); -// $stubParameters = array_filter($phpstormFunction->parameters, fn (PHPParameter $stubParameter) => $stubParameter->indexInSignature === $reflectionParameter->indexInSignature); -// /** @var PHPParameter $stubOptionalParameter */ -// $stubOptionalParameter = array_pop($stubParameters); -// $reflectionValue = AbstractBaseStubsTestCase::getStringRepresentationOfDefaultParameterValue($reflectionParameter->defaultValue); -// $stubValue = AbstractBaseStubsTestCase::getStringRepresentationOfDefaultParameterValue($stubOptionalParameter->defaultValue, $stubClass); -// self::assertEquals( -// $reflectionValue, -// $stubValue, -// sprintf( -// 'Reflection method %s::%s has optional parameter %s with default value %s but stub parameter has value %s', -// $classId, -// $methodName, -// $parameterName, -// $reflectionValue, -// $stubValue -// ) -// ); -// } + #[DataProviderExternal(ReflectionParametersProvider::class, 'classMethodOptionalParametersWithDefaultValueProvider')] + public function testClassMethodsDefaultParametersValue(?string $classId, ?string $methodName, ?string $parameterName) + { + if (!$classId && !$methodName && !$parameterName) { + self::markTestSkipped($this->emptyDataSetMessage); + } + $reflectionParameter = ReflectionStubsSingleton::getReflectionStubs()->getClass($classId, fromReflection: true)->getMethod($methodName, fromReflection: true)->getParameter($parameterName); + $stubClass = PhpStormStubsSingleton::getPhpStormStubs()->getClass($classId); + $phpstormFunction = $stubClass->getMethod($methodName); + $stubParameters = array_filter($phpstormFunction->parameters, fn (PHPParameter $stubParameter) => $stubParameter->indexInSignature === $reflectionParameter->indexInSignature); + /** @var PHPParameter $stubOptionalParameter */ + $stubOptionalParameter = array_pop($stubParameters); + $reflectionValue = AbstractBaseStubsTestCase::getStringRepresentationOfDefaultParameterValue($reflectionParameter->defaultValue); + $stubValue = AbstractBaseStubsTestCase::getStringRepresentationOfDefaultParameterValue($stubOptionalParameter->defaultValue, $stubClass); + self::assertEquals( + $reflectionValue, + $stubValue, + sprintf( + 'Reflection method %s::%s has optional parameter %s with default value %s but stub parameter has value %s', + $classId, + $methodName, + $parameterName, + $reflectionValue, + $stubValue + ) + ); + } #[DataProviderExternal(ReflectionParametersProvider::class, 'interfaceMethodOptionalParametersWithDefaultValueProvider')] public function testInterfaceMethodsDefaultParametersValue(?string $classId, ?string $methodName, ?string $parameterName)