Skip to content

Commit 2dd2e84

Browse files
jmikolaalcaeus
andauthored
PHPLIB-1093: Assert $expectedType array is non-empty for InvalidArgumentException (#1049)
* PHPLIB-1093: Assert $expectedType array is non-empty for InvalidArgumentException This also factors out the logic for converting an $expectedType array to a string. Co-authored-by: Andreas Braun <[email protected]>
1 parent 8dcb543 commit 2dd2e84

File tree

4 files changed

+71
-19
lines changed

4 files changed

+71
-19
lines changed

phpunit.evergreen.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
>
1212

1313
<php>
14+
<ini name="assert.exception" value="1"/>
1415
<ini name="error_reporting" value="-1"/>
1516
<ini name="memory_limit" value="-1"/>
17+
<ini name="zend.assertions" value="1"/>
1618
<env name="MONGODB_URI" value="mongodb://127.0.0.1:27017/?serverSelectionTimeoutMS=100"/>
1719
<env name="MONGODB_DATABASE" value="phplib_test"/>
1820
</php>

phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
>
1212

1313
<php>
14+
<ini name="assert.exception" value="1"/>
1415
<ini name="error_reporting" value="-1"/>
16+
<ini name="zend.assertions" value="1"/>
1517
<env name="MONGODB_URI" value="mongodb://127.0.0.1:27017/?serverSelectionTimeoutMS=100"/>
1618
<env name="MONGODB_DATABASE" value="phplib_test"/>
1719
</php>

src/Exception/InvalidArgumentException.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
2121

2222
use function array_pop;
23+
use function assert;
2324
use function count;
2425
use function get_debug_type;
2526
use function implode;
@@ -31,32 +32,31 @@ class InvalidArgumentException extends DriverInvalidArgumentException implements
3132
/**
3233
* Thrown when an argument or option has an invalid type.
3334
*
34-
* @param string $name Name of the argument or option
35-
* @param mixed $value Actual value (used to derive the type)
36-
* @param string|string[] $expectedType Expected type
35+
* @param string $name Name of the argument or option
36+
* @param mixed $value Actual value (used to derive the type)
37+
* @param string|list<string> $expectedType Expected type as a string or an array containing one or more strings
3738
* @return self
3839
*/
3940
public static function invalidType(string $name, $value, $expectedType)
4041
{
4142
if (is_array($expectedType)) {
42-
switch (count($expectedType)) {
43-
case 1:
44-
$typeString = array_pop($expectedType);
45-
break;
46-
47-
case 2:
48-
$typeString = implode('" or "', $expectedType);
49-
break;
50-
51-
default:
52-
$lastType = array_pop($expectedType);
53-
$typeString = sprintf('%s", or "%s', implode('", "', $expectedType), $lastType);
54-
break;
55-
}
56-
57-
$expectedType = $typeString;
43+
$expectedType = self::expectedTypesToString($expectedType);
5844
}
5945

6046
return new static(sprintf('Expected %s to have type "%s" but found "%s"', $name, $expectedType, get_debug_type($value)));
6147
}
48+
49+
/** @param list<string> $types */
50+
private static function expectedTypesToString(array $types): string
51+
{
52+
assert(count($types) > 0);
53+
54+
if (count($types) < 3) {
55+
return implode('" or "', $types);
56+
}
57+
58+
$lastType = array_pop($types);
59+
60+
return sprintf('%s", or "%s', implode('", "', $types), $lastType);
61+
}
6262
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Exception;
4+
5+
use AssertionError;
6+
use MongoDB\Exception\InvalidArgumentException;
7+
use MongoDB\Tests\TestCase;
8+
9+
class InvalidArgumentExceptionTest extends TestCase
10+
{
11+
/**
12+
* @dataProvider provideExpectedTypes
13+
*/
14+
public function testExpectedTypeFormatting($expectedType, $typeString): void
15+
{
16+
$e = InvalidArgumentException::invalidType('$arg', null, $expectedType);
17+
$this->assertStringContainsString($typeString, $e->getMessage());
18+
}
19+
20+
public function provideExpectedTypes()
21+
{
22+
yield 'expectedType is a string' => [
23+
'array',
24+
'type "array"',
25+
];
26+
27+
yield 'expectedType is an array with one string' => [
28+
['array'],
29+
'type "array"',
30+
];
31+
32+
yield 'expectedType is an array with two strings' => [
33+
['array', 'integer'],
34+
'type "array" or "integer"',
35+
];
36+
37+
yield 'expectedType is an array with three strings' => [
38+
['array', 'integer', 'object'],
39+
'type "array", "integer", or "object"',
40+
];
41+
}
42+
43+
public function testExpectedTypeArrayMustNotBeEmpty(): void
44+
{
45+
$this->expectException(AssertionError::class);
46+
InvalidArgumentException::invalidType('$arg', null, []);
47+
}
48+
}

0 commit comments

Comments
 (0)