mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Add tests
This commit is contained in:
parent
4550d362eb
commit
07439f8f1c
@ -125,6 +125,29 @@ class ThrowsAnnotationTest extends TestCase
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNoThrowWhenSuppressingInline()
|
||||
{
|
||||
Config::getInstance()->check_for_throws_docblock = true;
|
||||
|
||||
$this->addFile(
|
||||
'somefile.php',
|
||||
'<?php
|
||||
function foo() : void {
|
||||
if (rand(0, 1)) {
|
||||
/** @psalm-suppress MissingThrowsDocblock */
|
||||
throw new \UnexpectedValueException();
|
||||
}
|
||||
}'
|
||||
);
|
||||
|
||||
$context = new Context();
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Psalm\Exception\CodeException
|
||||
* @expectedExceptionMessage MissingThrowsDocblock
|
||||
|
@ -188,4 +188,115 @@ class ThrowsInGlobalScopeTest extends TestCase
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUncaughtThrowWhenSuppressing()
|
||||
{
|
||||
Config::getInstance()->check_for_throws_in_global_scope = true;
|
||||
|
||||
$this->addFile(
|
||||
'somefile.php',
|
||||
'<?php
|
||||
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
||||
throw new \Exception();'
|
||||
);
|
||||
|
||||
$context = new Context();
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUncaughtThrowInNamespaceWhenSuppressing()
|
||||
{
|
||||
Config::getInstance()->check_for_throws_in_global_scope = true;
|
||||
|
||||
$this->addFile(
|
||||
'somefile.php',
|
||||
'<?php
|
||||
namespace ns;
|
||||
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
||||
throw new \Exception();'
|
||||
);
|
||||
|
||||
$context = new Context();
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUncaughtDocumentedThrowCallWhenSuppressing()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
||||
foo(0, 0);'
|
||||
);
|
||||
|
||||
$context = new Context();
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUncaughtDocumentedThrowCallInNamespaceWhenSuppressing()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
||||
foo(0, 0);'
|
||||
);
|
||||
|
||||
$context = new Context();
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user