1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Add more tests to identify regressions

This commit is contained in:
Matthew Brown 2016-08-24 00:30:55 -04:00
parent 0e68179bb4
commit 7e6d3b90ce
2 changed files with 105 additions and 17 deletions

View File

@ -146,24 +146,50 @@ class ScopeTest extends PHPUnit_Framework_TestCase
public function testSwitchVariableWithContinue()
{
$stmts = self::$_parser->parse('<?php
class B {
public function bar() {
foreach ([\'a\', \'b\', \'c\'] as $letter) {
switch ($letter) {
case \'a\':
$foo = 1;
break;
case \'b\':
$foo = 2;
break;
default:
continue;
}
$moo = $foo;
}
foreach ([\'a\', \'b\', \'c\'] as $letter) {
switch ($letter) {
case \'a\':
$foo = 1;
break;
case \'b\':
$foo = 2;
break;
default:
continue;
}
}');
$moo = $foo;
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
public function testSwitchVariableWithContinueAndIfs()
{
$stmts = self::$_parser->parse('<?php
foreach ([\'a\', \'b\', \'c\'] as $letter) {
switch ($letter) {
case \'a\':
if (rand(0, 10) === 1) {
continue;
}
$foo = 1;
break;
case \'b\':
if (rand(0, 10) === 1) {
continue;
}
$foo = 2;
break;
default:
continue;
}
$moo = $foo;
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$file_checker->check();

View File

@ -1481,4 +1481,66 @@ class TypeTest extends PHPUnit_Framework_TestCase
$this->assertSame('bool', (string) $context->vars_in_scope['b']);
}
/**
* @expectedException Psalm\Exception\CodeException
* @expectedExceptionMessage NullReference
*/
public function testNullCheckInsideForeachWithNoLeaveStatement()
{
$stmts = self::$_parser->parse('<?php
class A {
/** @return array<A|null> */
public static function loadMultiple()
{
return [new A, null];
}
public function bar() {
}
}
foreach (A::loadMultiple() as $a) {
if ($a === null) {
// do nothing
}
$a->bar();
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check();
}
public function testNullCheckInsideForeachWithContinue()
{
$stmts = self::$_parser->parse('<?php
class A {
/** @return array<A|null> */
public static function loadMultiple()
{
return [new A, null];
}
public function bar() {
}
}
foreach (A::loadMultiple() as $a) {
if ($a === null) {
continue;
}
$a->bar();
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check();
}
}