Skip to content

Commit

Permalink
[phpstorm-stubs] fix test that checks constants as default values
Browse files Browse the repository at this point in the history
  • Loading branch information
isfedorov committed Nov 27, 2024
1 parent 7809499 commit bb981ec
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
3 changes: 3 additions & 0 deletions random/random.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ function random_int(int $min, int $max): int {}

namespace Random\Engine
{

use const MT_RAND_MT19937;

/**
* @since 8.2
*/
Expand Down
2 changes: 1 addition & 1 deletion standard/standard_defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@
/**
* @since 7.1
*/
define("MT_RAND_MT19937", 0);
define('MT_RAND_MT19937', 0);
/**
* @since 7.1
* @deprecated 8.3
Expand Down
4 changes: 2 additions & 2 deletions tests/AbstractBaseStubsTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down
54 changes: 27 additions & 27 deletions tests/StubsConstantsAndParametersValuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bb981ec

Please sign in to comment.