1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Add tests

This commit is contained in:
bugreportuser 2019-03-24 14:29:08 -06:00 committed by Matthew Brown
parent b71005e199
commit 47d8da6135
2 changed files with 148 additions and 0 deletions

View File

@ -388,4 +388,108 @@ class ThrowsAnnotationTest extends TestCase
$this->analyzeFile('somefile.php', $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage UncaughtThrowInGlobalScope
*
* @return void
*/
public function testUncaughtDocumentedThrowCallInGlobalScope()
{
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);
}
foo(0, 0);'
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
/**
* @return void
*/
public function testCaughtDocumentedThrowCallInGlobalScope()
{
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);
} catch (Exception $e) {}'
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
/**
* @return void
*/
public function testUncaughtUndocumentedThrowCallInGlobalScope()
{
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);
}
foo(0, 0);'
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
}

View File

@ -1,6 +1,9 @@
<?php
namespace Psalm\Tests;
use Psalm\Config;
use Psalm\Context;
class TryCatchTest extends TestCase
{
use Traits\ValidCodeAnalysisTestTrait;
@ -274,4 +277,45 @@ class TryCatchTest extends TestCase
],
];
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage UncaughtThrowInGlobalScope
*
* @return void
*/
public function testUncaughtThrowInGlobalScope()
{
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
throw new \Exception();'
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
/**
* @return void
*/
public function testCaughtThrowInGlobalScope()
{
Config::getInstance()->check_for_throws_in_global_scope = true;
$this->addFile(
'somefile.php',
'<?php
try {
throw new \Exception();
} catch (\Exception $e) {}'
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
}