1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/TryCatchTest.php

79 lines
2.2 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
class TryCatchTest extends TestCase
{
use Traits\FileCheckerValidCodeParseTestTrait;
use Traits\FileCheckerInvalidCodeParseTestTrait;
/**
* @return array
*/
public function providerFileCheckerValidCodeParse()
{
return [
'PHP7-addThrowableInterfaceType' => [
'<?php
interface CustomThrowable {}
class CustomException extends Exception implements CustomThrowable {}
/** @psalm-suppress InvalidCatch */
try {
throw new CustomException("Bad");
} catch (CustomThrowable $e) {
echo $e->getMessage();
}',
],
'PHP7-rethrowInterfaceExceptionWithoutInvalidThrow' => [
'<?php
interface CustomThrowable {}
class CustomException extends Exception implements CustomThrowable {}
/** @psalm-suppress InvalidCatch */
try {
throw new CustomException("Bad");
} catch (CustomThrowable $e) {
throw $e;
}',
],
'tryCatchVar' => [
'<?php
try {
$worked = true;
}
catch (\Exception $e) {
$worked = false;
}',
'assertions' => [
'$worked' => 'bool',
],
],
];
}
/**
* @return array
*/
public function providerFileCheckerInvalidCodeParse()
{
return [
'invalidCatchClass' => [
'<?php
class A {}
try {
$worked = true;
}
catch (A $e) {}',
'error_message' => 'InvalidCatch',
],
'invalidThrowClass' => [
'<?php
class A {}
throw new A();',
'error_message' => 'InvalidThrow',
],
];
}
}