Make unit tests working

This commit is contained in:
Eric Espie
2025-10-02 09:48:22 +02:00
parent 0ea0da525e
commit 39fd879ca9
83 changed files with 1924 additions and 262 deletions

View File

@@ -26,7 +26,7 @@ class UnexpectedTypeException extends RuntimeException
*/
public function __construct(mixed $value, PropertyPathInterface $path, int $pathIndex)
{
$message = sprintf(
$message = \sprintf(
'PropertyAccessor requires a graph of objects or arrays to operate on, '.
'but it found type "%s" while trying to traverse path "%s" at property "%s".',
\gettype($value),

View File

@@ -195,12 +195,12 @@ class PropertyAccessor implements PropertyAccessorInterface
if (preg_match('/^\S+::\S+\(\): Argument #\d+ \(\$\S+\) must be of type (\S+), (\S+) given/', $message, $matches)) {
[, $expectedType, $actualType] = $matches;
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
throw new InvalidArgumentException(\sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
}
if (preg_match('/^Cannot assign (\S+) to property \S+::\$\S+ of type (\S+)$/', $message, $matches)) {
[, $actualType, $expectedType] = $matches;
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
throw new InvalidArgumentException(\sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
}
}
@@ -216,7 +216,7 @@ class PropertyAccessor implements PropertyAccessorInterface
];
// handle stdClass with properties with a dot in the name
if ($objectOrArray instanceof \stdClass && str_contains($propertyPath, '.') && property_exists($objectOrArray, $propertyPath)) {
if ($objectOrArray instanceof \stdClass && str_contains($propertyPath, '.') && property_exists($objectOrArray, $propertyPath)) {
$this->readProperty($zval, $propertyPath, $this->ignoreInvalidProperty);
} else {
$this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
@@ -308,13 +308,13 @@ class PropertyAccessor implements PropertyAccessorInterface
if (!$ignoreInvalidIndices && !$isNullSafe) {
if (!\is_array($zval[self::VALUE])) {
if (!$zval[self::VALUE] instanceof \Traversable) {
throw new NoSuchIndexException(sprintf('Cannot read index "%s" while trying to traverse path "%s".', $property, (string) $propertyPath));
throw new NoSuchIndexException(\sprintf('Cannot read index "%s" while trying to traverse path "%s".', $property, (string) $propertyPath));
}
$zval[self::VALUE] = iterator_to_array($zval[self::VALUE]);
}
throw new NoSuchIndexException(sprintf('Cannot read index "%s" while trying to traverse path "%s". Available indices are "%s".', $property, (string) $propertyPath, print_r(array_keys($zval[self::VALUE]), true)));
throw new NoSuchIndexException(\sprintf('Cannot read index "%s" while trying to traverse path "%s". Available indices are "%s".', $property, (string) $propertyPath, print_r(array_keys($zval[self::VALUE]), true)));
}
if ($i + 1 < $propertyPath->getLength()) {
@@ -366,7 +366,7 @@ class PropertyAccessor implements PropertyAccessorInterface
private function readIndex(array $zval, string|int $index): array
{
if (!$zval[self::VALUE] instanceof \ArrayAccess && !\is_array($zval[self::VALUE])) {
throw new NoSuchIndexException(sprintf('Cannot read index "%s" from object of type "%s" because it doesn\'t implement \ArrayAccess.', $index, get_debug_type($zval[self::VALUE])));
throw new NoSuchIndexException(\sprintf('Cannot read index "%s" from object of type "%s" because it doesn\'t implement \ArrayAccess.', $index, get_debug_type($zval[self::VALUE])));
}
$result = self::RESULT_PROTO;
@@ -394,7 +394,7 @@ class PropertyAccessor implements PropertyAccessorInterface
private function readProperty(array $zval, string $property, bool $ignoreInvalidProperty = false, bool $isNullSafe = false): array
{
if (!\is_object($zval[self::VALUE])) {
throw new NoSuchPropertyException(sprintf('Cannot read property "%s" from an array. Maybe you intended to write the property path as "[%1$s]" instead.', $property));
throw new NoSuchPropertyException(\sprintf('Cannot read property "%s" from an array. Maybe you intended to write the property path as "[%1$s]" instead.', $property));
}
$result = self::RESULT_PROTO;
@@ -419,7 +419,7 @@ class PropertyAccessor implements PropertyAccessorInterface
&& $object instanceof $trace['class']
&& preg_match('/Return value (?:of .*::\w+\(\) )?must be of (?:the )?type (\w+), null returned$/', $e->getMessage(), $matches)
) {
throw new UninitializedPropertyException(sprintf('The method "%s::%s()" returned "null", but expected type "%3$s". Did you forget to initialize a property or to make the return type nullable using "?%3$s"?', get_debug_type($object), $name, $matches[1]), 0, $e);
throw new UninitializedPropertyException(\sprintf('The method "%s::%s()" returned "null", but expected type "%3$s". Did you forget to initialize a property or to make the return type nullable using "?%3$s"?', get_debug_type($object), $name, $matches[1]), 0, $e);
}
throw $e;
@@ -430,11 +430,11 @@ class PropertyAccessor implements PropertyAccessorInterface
$r = new \ReflectionProperty($class, $name);
if ($r->isPublic() && !$r->hasType()) {
throw new UninitializedPropertyException(sprintf('The property "%s::$%s" is not initialized.', $class, $name));
throw new UninitializedPropertyException(\sprintf('The property "%s::$%s" is not initialized.', $class, $name));
}
} catch (\ReflectionException $e) {
if (!$ignoreInvalidProperty) {
throw new NoSuchPropertyException(sprintf('Can\'t get a way to read the property "%s" in class "%s".', $property, $class));
throw new NoSuchPropertyException(\sprintf('Can\'t get a way to read the property "%s" in class "%s".', $property, $class));
}
}
}
@@ -451,7 +451,7 @@ class PropertyAccessor implements PropertyAccessorInterface
$r = new \ReflectionProperty(str_contains($matches[1], '@anonymous') ? $class : $matches[1], $matches[2]);
$type = ($type = $r->getType()) instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
throw new UninitializedPropertyException(sprintf('The property "%s::$%s" is not readable because it is typed "%s". You should initialize it or declare a default value instead.', $matches[1], $r->getName(), $type), 0, $e);
throw new UninitializedPropertyException(\sprintf('The property "%s::$%s" is not readable because it is typed "%s". You should initialize it or declare a default value instead.', $matches[1], $r->getName(), $type), 0, $e);
}
throw $e;
@@ -464,7 +464,7 @@ class PropertyAccessor implements PropertyAccessorInterface
} elseif ($isNullSafe) {
$result[self::VALUE] = null;
} elseif (!$ignoreInvalidProperty) {
throw new NoSuchPropertyException(sprintf('Can\'t get a way to read the property "%s" in class "%s".', $property, $class));
throw new NoSuchPropertyException(\sprintf('Can\'t get a way to read the property "%s" in class "%s".', $property, $class));
}
// Objects are always passed around by reference
@@ -514,7 +514,7 @@ class PropertyAccessor implements PropertyAccessorInterface
private function writeIndex(array $zval, string|int $index, mixed $value): void
{
if (!$zval[self::VALUE] instanceof \ArrayAccess && !\is_array($zval[self::VALUE])) {
throw new NoSuchIndexException(sprintf('Cannot modify index "%s" in object of type "%s" because it doesn\'t implement \ArrayAccess.', $index, get_debug_type($zval[self::VALUE])));
throw new NoSuchIndexException(\sprintf('Cannot modify index "%s" in object of type "%s" because it doesn\'t implement \ArrayAccess.', $index, get_debug_type($zval[self::VALUE])));
}
$zval[self::REF][$index] = $value;
@@ -528,7 +528,7 @@ class PropertyAccessor implements PropertyAccessorInterface
private function writeProperty(array $zval, string $property, mixed $value, bool $recursive = false): void
{
if (!\is_object($zval[self::VALUE])) {
throw new NoSuchPropertyException(sprintf('Cannot write property "%s" to an array. Maybe you should write the property path as "[%1$s]" instead?', $property));
throw new NoSuchPropertyException(\sprintf('Cannot write property "%s" to an array. Maybe you should write the property path as "[%1$s]" instead?', $property));
}
$object = $zval[self::VALUE];
@@ -553,7 +553,7 @@ class PropertyAccessor implements PropertyAccessorInterface
throw new NoSuchPropertyException(implode('. ', $mutator->getErrors()).'.');
}
throw new NoSuchPropertyException(sprintf('Could not determine access type for property "%s" in class "%s".', $property, get_debug_type($object)));
throw new NoSuchPropertyException(\sprintf('Could not determine access type for property "%s" in class "%s".', $property, get_debug_type($object)));
}
} catch (\TypeError $e) {
if ($recursive || !$value instanceof \DateTimeInterface || !\in_array($value::class, ['DateTime', 'DateTimeImmutable'], true) || __FILE__ !== ($e->getTrace()[0]['file'] ?? null)) {
@@ -646,7 +646,7 @@ class PropertyAccessor implements PropertyAccessorInterface
$mutatorForArray = $this->getWriteInfo($object::class, $property, []);
if (PropertyWriteInfo::TYPE_PROPERTY === $mutatorForArray->getType()) {
return $mutatorForArray->getVisibility() === 'public';
return 'public' === $mutatorForArray->getVisibility();
}
if (PropertyWriteInfo::TYPE_NONE !== $mutatorForArray->getType()) {
@@ -696,7 +696,7 @@ class PropertyAccessor implements PropertyAccessorInterface
public static function createCache(string $namespace, int $defaultLifetime, string $version, ?LoggerInterface $logger = null): AdapterInterface
{
if (!class_exists(ApcuAdapter::class)) {
throw new \LogicException(sprintf('The Symfony Cache component must be installed to use "%s()".', __METHOD__));
throw new \LogicException(\sprintf('The Symfony Cache component must be installed to use "%s()".', __METHOD__));
}
if (!ApcuAdapter::isSupported()) {

View File

@@ -128,7 +128,7 @@ class PropertyAccessorBuilder
*/
public function isMagicCallEnabled(): bool
{
return (bool) ($this->magicMethods & PropertyAccessor::MAGIC_CALL);
return $this->magicMethods & PropertyAccessor::MAGIC_CALL;
}
/**

View File

@@ -72,7 +72,6 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
{
// Can be used as copy constructor
if ($propertyPath instanceof self) {
/* @var PropertyPath $propertyPath */
$this->elements = $propertyPath->elements;
$this->length = $propertyPath->length;
$this->isIndex = $propertyPath->isIndex;
@@ -122,7 +121,7 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
}
if ('' !== $remaining) {
throw new InvalidPropertyPathException(sprintf('Could not parse property path "%s". Unexpected token "%s" at position %d.', $propertyPath, $remaining[0], $position));
throw new InvalidPropertyPathException(\sprintf('Could not parse property path "%s". Unexpected token "%s" at position %d.', $propertyPath, $remaining[0], $position));
}
$this->length = \count($this->elements);
@@ -171,7 +170,7 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
public function getElement(int $index): string
{
if (!isset($this->elements[$index])) {
throw new OutOfBoundsException(sprintf('The index "%s" is not within the property path.', $index));
throw new OutOfBoundsException(\sprintf('The index "%s" is not within the property path.', $index));
}
return $this->elements[$index];
@@ -180,7 +179,7 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
public function isProperty(int $index): bool
{
if (!isset($this->isIndex[$index])) {
throw new OutOfBoundsException(sprintf('The index "%s" is not within the property path.', $index));
throw new OutOfBoundsException(\sprintf('The index "%s" is not within the property path.', $index));
}
return !$this->isIndex[$index];
@@ -189,7 +188,7 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
public function isIndex(int $index): bool
{
if (!isset($this->isIndex[$index])) {
throw new OutOfBoundsException(sprintf('The index "%s" is not within the property path.', $index));
throw new OutOfBoundsException(\sprintf('The index "%s" is not within the property path.', $index));
}
return $this->isIndex[$index];
@@ -198,7 +197,7 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
public function isNullSafe(int $index): bool
{
if (!isset($this->isNullSafe[$index])) {
throw new OutOfBoundsException(sprintf('The index "%s" is not within the property path.', $index));
throw new OutOfBoundsException(\sprintf('The index "%s" is not within the property path.', $index));
}
return $this->isNullSafe[$index];

View File

@@ -86,7 +86,7 @@ class PropertyPathBuilder
public function remove(int $offset, int $length = 1)
{
if (!isset($this->elements[$offset])) {
throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
throw new OutOfBoundsException(\sprintf('The offset "%s" is not within the property path.', $offset));
}
$this->resize($offset, $length, 0);
@@ -137,7 +137,7 @@ class PropertyPathBuilder
public function replaceByIndex(int $offset, ?string $name = null)
{
if (!isset($this->elements[$offset])) {
throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
throw new OutOfBoundsException(\sprintf('The offset "%s" is not within the property path.', $offset));
}
if (null !== $name) {
@@ -157,7 +157,7 @@ class PropertyPathBuilder
public function replaceByProperty(int $offset, ?string $name = null)
{
if (!isset($this->elements[$offset])) {
throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
throw new OutOfBoundsException(\sprintf('The offset "%s" is not within the property path.', $offset));
}
if (null !== $name) {