1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-06 05:29:00 +01:00
psalm/tests/ThrowsInGlobalScopeTest.php

310 lines
8.6 KiB
PHP
Raw Normal View History

<?php
2023-10-19 13:12:06 +02:00
declare(strict_types=1);
namespace Psalm\Tests;
use Psalm\Config;
use Psalm\Context;
2021-12-03 20:29:06 +01:00
use Psalm\Exception\CodeException;
class ThrowsInGlobalScopeTest extends TestCase
{
public function testUncaughtDocumentedThrowCall(): void
{
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
2019-05-17 00:36:36 +02:00
$this->expectExceptionMessage('UncaughtThrowInGlobalScope');
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws RangeException
* @throws InvalidArgumentException
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
}
2022-12-18 17:15:15 +01:00
foo(0, 0);',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testCaughtDocumentedThrowCall(): void
{
Config::getInstance()->check_for_throws_docblock = true;
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws RangeException
* @throws InvalidArgumentException
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
}
try {
foo(0, 0);
2022-12-18 17:15:15 +01:00
} catch (Exception $e) {}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testUncaughtUndocumentedThrowCall(): void
{
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
}
2022-12-18 17:15:15 +01:00
foo(0, 0);',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testUncaughtDocumentedThrowCallInNamespace(): void
{
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
2019-05-17 00:36:36 +02:00
$this->expectExceptionMessage('UncaughtThrowInGlobalScope');
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
namespace ns;
/**
* @throws \RangeException
* @throws \InvalidArgumentException
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
}
2022-12-18 17:15:15 +01:00
foo(0, 0);',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testUncaughtThrow(): void
{
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
2019-05-17 00:36:36 +02:00
$this->expectExceptionMessage('UncaughtThrowInGlobalScope');
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
2022-12-18 17:15:15 +01:00
throw new \Exception();',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testCaughtThrow(): void
{
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
try {
throw new \Exception();
2022-12-18 17:15:15 +01:00
} catch (\Exception $e) {}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
2019-03-29 01:01:26 +01:00
public function testUncaughtThrowWhenSuppressing(): void
2019-03-29 01:01:26 +01:00
{
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
/** @psalm-suppress UncaughtThrowInGlobalScope */
2022-12-18 17:15:15 +01:00
throw new \Exception();',
2019-03-29 01:01:26 +01:00
);
$context = new Context();
2019-08-18 21:34:32 +02:00
$this->analyzeFile('somefile.php', $context, false);
2019-03-29 01:01:26 +01:00
}
public function testUncaughtThrowInNamespaceWhenSuppressing(): void
2019-03-29 01:01:26 +01:00
{
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
namespace ns;
/** @psalm-suppress UncaughtThrowInGlobalScope */
2022-12-18 17:15:15 +01:00
throw new \Exception();',
2019-03-29 01:01:26 +01:00
);
$context = new Context();
2019-08-18 21:34:32 +02:00
$this->analyzeFile('somefile.php', $context, false);
2019-03-29 01:01:26 +01:00
}
public function testUncaughtDocumentedThrowCallWhenSuppressing(): void
2019-03-29 01:01:26 +01:00
{
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws RangeException
* @throws InvalidArgumentException
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
}
/** @psalm-suppress UncaughtThrowInGlobalScope */
2022-12-18 17:15:15 +01:00
foo(0, 0);',
2019-03-29 01:01:26 +01:00
);
$context = new Context();
2019-08-18 21:34:32 +02:00
$this->analyzeFile('somefile.php', $context, false);
2019-03-29 01:01:26 +01:00
}
public function testUncaughtDocumentedThrowCallInNamespaceWhenSuppressing(): void
2019-03-29 01:01:26 +01:00
{
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
namespace ns;
/**
* @throws \RangeException
* @throws \InvalidArgumentException
2019-03-29 01:01:26 +01:00
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
}
/** @psalm-suppress UncaughtThrowInGlobalScope */
2022-12-18 17:15:15 +01:00
foo(0, 0);',
2019-03-29 01:01:26 +01:00
);
$context = new Context();
2019-08-18 21:34:32 +02:00
$this->analyzeFile('somefile.php', $context, false);
2019-03-29 01:01:26 +01:00
}
2019-04-03 01:42:23 +02:00
public function testUncaughtDocumentedThrowCallWhenSuppressingFirst(): void
2019-04-03 01:42:23 +02:00
{
2019-05-17 00:36:36 +02:00
$this->expectExceptionMessage('UncaughtThrowInGlobalScope');
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
2019-04-03 01:42:23 +02:00
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws RangeException
* @throws InvalidArgumentException
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
}
/** @psalm-suppress UncaughtThrowInGlobalScope */
foo(0, 0);
2022-12-18 17:15:15 +01:00
foo(0, 0);',
2019-04-03 01:42:23 +02:00
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
}