Skip to content

Commit

Permalink
Improve deprecation message for assertInternalType() and assertNotInt…
Browse files Browse the repository at this point in the history
…ernalType()
  • Loading branch information
sebastianbergmann committed Jan 17, 2021
1 parent c2a7f8b commit d2c1476
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,12 @@ public static function assertAttributeNotInstanceOf(string $expected, string $at
*/
public static function assertInternalType(string $expected, $actual, string $message = ''): void
{
self::createWarning('assertInternalType() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(), assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(), assertIsResource(), assertIsString(), assertIsScalar(), assertIsCallable(), or assertIsIterable() instead.');
self::createWarning(
sprintf(
'assertInternalType() is deprecated and will be removed in PHPUnit 9. Refactor your test to use %s() instead.',
self::assertInternalTypeReplacement($expected, false)
)
);

static::assertThat(
$actual,
Expand Down Expand Up @@ -2249,7 +2254,12 @@ public static function assertIsIterable($actual, string $message = ''): void
*/
public static function assertNotInternalType(string $expected, $actual, string $message = ''): void
{
self::createWarning('assertNotInternalType() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertIsNotArray(), assertIsNotBool(), assertIsNotFloat(), assertIsNotInt(), assertIsNotNumeric(), assertIsNotObject(), assertIsNotResource(), assertIsNotString(), assertIsNotScalar(), assertIsNotCallable(), or assertIsNotIterable() instead.');
self::createWarning(
sprintf(
'assertNotInternalType() is deprecated and will be removed in PHPUnit 9. Refactor your test to use %s() instead.',
self::assertInternalTypeReplacement($expected, true)
)
);

static::assertThat(
$actual,
Expand Down Expand Up @@ -3580,4 +3590,56 @@ private static function createWarning(string $warning): void
}
}
}

private static function assertInternalTypeReplacement(string $type, bool $not): string
{
switch ($type) {
case 'numeric':
return 'assertIs' . ($not ? 'Not' : '') . 'Numeric';

case 'integer':
case 'int':
return 'assertIs' . ($not ? 'Not' : '') . 'Int';

case 'double':
case 'float':
case 'real':
return 'assertIs' . ($not ? 'Not' : '') . 'Float';

case 'string':
return 'assertIs' . ($not ? 'Not' : '') . 'String';

case 'boolean':
case 'bool':
return 'assertIs' . ($not ? 'Not' : '') . 'Bool';

case 'null':
return 'assert' . ($not ? 'Not' : '') . 'Null';

case 'array':
return 'assertIs' . ($not ? 'Not' : '') . 'Array';

case 'object':
return 'assertIs' . ($not ? 'Not' : '') . 'Object';

case 'resource':
return 'assertIs' . ($not ? 'Not' : '') . 'Resource';

case 'scalar':
return 'assertIs' . ($not ? 'Not' : '') . 'Scalar';

case 'callable':
return 'assertIs' . ($not ? 'Not' : '') . 'Callable';

case 'iterable':
return 'assertIs' . ($not ? 'Not' : '') . 'Iterable';
}

throw new Exception(
sprintf(
'"%s" is not a type supported by assertInternalType() / assertNotInternalType()',
$type
)
);
}
}

0 comments on commit d2c1476

Please sign in to comment.