Skip to content

PHPLIB-1093: Assert $expectedType array is non-empty for InvalidArgumentException #1049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions phpunit.evergreen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
>

<php>
<ini name="assert.exception" value="1"/>
<ini name="error_reporting" value="-1"/>
<ini name="memory_limit" value="-1"/>
<ini name="zend.assertions" value="1"/>
<env name="MONGODB_URI" value="mongodb://127.0.0.1:27017/?serverSelectionTimeoutMS=100"/>
<env name="MONGODB_DATABASE" value="phplib_test"/>
</php>
Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
>

<php>
<ini name="assert.exception" value="1"/>
<ini name="error_reporting" value="-1"/>
<ini name="zend.assertions" value="1"/>
<env name="MONGODB_URI" value="mongodb://127.0.0.1:27017/?serverSelectionTimeoutMS=100"/>
<env name="MONGODB_DATABASE" value="phplib_test"/>
</php>
Expand Down
38 changes: 19 additions & 19 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;

use function array_pop;
use function assert;
use function count;
use function get_debug_type;
use function implode;
Expand All @@ -31,32 +32,31 @@ class InvalidArgumentException extends DriverInvalidArgumentException implements
/**
* Thrown when an argument or option has an invalid type.
*
* @param string $name Name of the argument or option
* @param mixed $value Actual value (used to derive the type)
* @param string|string[] $expectedType Expected type
* @param string $name Name of the argument or option
* @param mixed $value Actual value (used to derive the type)
* @param string|list<string> $expectedType Expected type as a string or an array containing one or more strings
* @return self
*/
public static function invalidType(string $name, $value, $expectedType)
{
if (is_array($expectedType)) {
switch (count($expectedType)) {
case 1:
$typeString = array_pop($expectedType);
break;

case 2:
$typeString = implode('" or "', $expectedType);
break;

default:
$lastType = array_pop($expectedType);
$typeString = sprintf('%s", or "%s', implode('", "', $expectedType), $lastType);
break;
}

$expectedType = $typeString;
$expectedType = self::expectedTypesToString($expectedType);
}

return new static(sprintf('Expected %s to have type "%s" but found "%s"', $name, $expectedType, get_debug_type($value)));
}

/** @param list<string> $types */
private static function expectedTypesToString(array $types): string
{
assert(count($types) > 0);

if (count($types) < 3) {
return implode('" or "', $types);
}

$lastType = array_pop($types);

return sprintf('%s", or "%s', implode('", "', $types), $lastType);
}
}
48 changes: 48 additions & 0 deletions tests/Exception/InvalidArgumentExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace MongoDB\Tests\Exception;

use AssertionError;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Tests\TestCase;

class InvalidArgumentExceptionTest extends TestCase
{
/**
* @dataProvider provideExpectedTypes
*/
public function testExpectedTypeFormatting($expectedType, $typeString): void
{
$e = InvalidArgumentException::invalidType('$arg', null, $expectedType);
$this->assertStringContainsString($typeString, $e->getMessage());
}

public function provideExpectedTypes()
{
yield 'expectedType is a string' => [
'array',
'type "array"',
];

yield 'expectedType is an array with one string' => [
['array'],
'type "array"',
];

yield 'expectedType is an array with two strings' => [
['array', 'integer'],
'type "array" or "integer"',
];

yield 'expectedType is an array with three strings' => [
['array', 'integer', 'object'],
'type "array", "integer", or "object"',
];
}

public function testExpectedTypeArrayMustNotBeEmpty(): void
{
$this->expectException(AssertionError::class);
InvalidArgumentException::invalidType('$arg', null, []);
}
}