1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Add additional tests

This commit is contained in:
Matt Brown 2017-03-13 18:07:36 -04:00
parent 3d2be3410e
commit b3bb8b72cc
3 changed files with 187 additions and 116 deletions

View File

@ -327,6 +327,9 @@ class LoopScopeTest extends PHPUnit_Framework_TestCase
$this->assertEquals('B', (string) $context->vars_in_scope['$a']);
}
/**
* @return void
*/
public function testSecondLoopWithNotNullCheck()
{
$stmts = self::$parser->parse('<?php
@ -345,6 +348,9 @@ class LoopScopeTest extends PHPUnit_Framework_TestCase
$file_checker->visitAndAnalyzeMethods();
}
/**
* @return void
*/
public function testSecondLoopWithIntCheck()
{
$stmts = self::$parser->parse('<?php
@ -363,6 +369,9 @@ class LoopScopeTest extends PHPUnit_Framework_TestCase
$file_checker->visitAndAnalyzeMethods();
}
/**
* @return void
*/
public function testSecondLoopWithIntCheckAndConditionalSet()
{
$stmts = self::$parser->parse('<?php
@ -384,6 +393,33 @@ class LoopScopeTest extends PHPUnit_Framework_TestCase
$file_checker->visitAndAnalyzeMethods();
}
/**
* @return void
*/
public function testSecondLoopWithIntCheckAndAssignmentsInIfAndElse()
{
$stmts = self::$parser->parse('<?php
/** @return void **/
function takesInt(int $i) {}
$a = null;
foreach ([1, 2, 3] as $i) {
if (is_int($a)) {
$a = 6;
} else {
$a = $i;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
/**
* @return void
*/
public function testSecondLoopWithIntCheckAndLoopSet()
{
$stmts = self::$parser->parse('<?php
@ -405,6 +441,9 @@ class LoopScopeTest extends PHPUnit_Framework_TestCase
$file_checker->visitAndAnalyzeMethods();
}
/**
* @return void
*/
public function testThirdLoopWithIntCheckAndLoopSet()
{
$stmts = self::$parser->parse('<?php
@ -431,4 +470,115 @@ class LoopScopeTest extends PHPUnit_Framework_TestCase
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
/**
* @return void
*/
public function testAssignInsideForeach()
{
$stmts = self::$parser->parse('<?php
$b = false;
foreach ([1, 2, 3, 4] as $a) {
if ($a === rand(0, 10)) {
$b = true;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
$this->assertSame('bool', (string) $context->vars_in_scope['$b']);
}
/**
* @return void
*/
public function testAssignInsideForeachWithBreak()
{
$stmts = self::$parser->parse('<?php
$b = false;
foreach ([1, 2, 3, 4] as $a) {
if ($a === rand(0, 10)) {
$b = true;
break;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
$this->assertSame('bool', (string) $context->vars_in_scope['$b']);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage PossiblyNullReference
* @return void
*/
public function testPossiblyNullCheckInsideForeachWithNoLeaveStatement()
{
$stmts = self::$parser->parse('<?php
class A {
/** @return array<A|null> */
public static function loadMultiple()
{
return [new A, null];
}
/** @return void */
public function barBar() {
}
}
foreach (A::loadMultiple() as $a) {
if ($a === null) {
// do nothing
}
$a->barBar();
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
/**
* @return void
*/
public function testNullCheckInsideForeachWithContinue()
{
$stmts = self::$parser->parse('<?php
class A {
/** @return array<A|null> */
public static function loadMultiple()
{
return [new A, null];
}
/** @return void */
public function barBar() {
}
}
foreach (A::loadMultiple() as $a) {
if ($a === null) {
continue;
}
$a->barBar();
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
}

View File

@ -16,9 +16,6 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase
/** @var \PhpParser\Parser */
protected static $parser;
/** @var TestConfig */
protected static $config;
/** @var \Psalm\Checker\ProjectChecker */
protected $project_checker;
@ -31,7 +28,6 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase
public static function setUpBeforeClass()
{
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
self::$config = new TestConfig();
}
/**
@ -42,7 +38,7 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase
FileChecker::clearCache();
$this->project_checker = new ProjectChecker();
$this->project_checker->setConfig(self::$config);
$this->project_checker->setConfig(new TestConfig());
$this->file_checker = new FileChecker('somefile.php', $this->project_checker);
$this->file_checker->context = new Context();
@ -886,4 +882,40 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
/**
* @return void
*/
public function testIgnoreNullCheckAndMaintainNullValue()
{
Config::getInstance()->setCustomErrorLevel('FailedTypeResolution', Config::REPORT_SUPPRESS);
$stmts = self::$parser->parse('<?php
$a = null;
if ($a !== null) { }
$b = $a;
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
$this->assertEquals('null', (string) $context->vars_in_scope['$b']);
}
/**
* @return void
*/
public function testIgnoreNullCheckAndMaintainNullableValue()
{
$stmts = self::$parser->parse('<?php
$a = rand(0, 1) ? 5 : null;
if ($a !== null) { }
$b = $a;
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
$this->assertEquals('int|null', (string) $context->vars_in_scope['$b']);
}
}

View File

@ -1753,85 +1753,6 @@ class TypeTest extends PHPUnit_Framework_TestCase
$file_checker->visitAndAnalyzeMethods();
}
/**
* @return void
*/
public function testAssignInsideForeach()
{
$stmts = self::$parser->parse('<?php
$b = false;
foreach ([1, 2, 3, 4] as $a) {
if ($a === rand(0, 10)) {
$b = true;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
$this->assertSame('bool', (string) $context->vars_in_scope['$b']);
}
/**
* @return void
*/
public function testAssignInsideForeachWithBreak()
{
$stmts = self::$parser->parse('<?php
$b = false;
foreach ([1, 2, 3, 4] as $a) {
if ($a === rand(0, 10)) {
$b = true;
break;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
$this->assertSame('bool', (string) $context->vars_in_scope['$b']);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage PossiblyNullReference
* @return void
*/
public function testPossiblyNullCheckInsideForeachWithNoLeaveStatement()
{
$stmts = self::$parser->parse('<?php
class A {
/** @return array<A|null> */
public static function loadMultiple()
{
return [new A, null];
}
/** @return void */
public function barBar() {
}
}
foreach (A::loadMultiple() as $a) {
if ($a === null) {
// do nothing
}
$a->barBar();
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage NullReference
@ -1849,38 +1770,6 @@ class TypeTest extends PHPUnit_Framework_TestCase
$file_checker->visitAndAnalyzeMethods();
}
/**
* @return void
*/
public function testNullCheckInsideForeachWithContinue()
{
$stmts = self::$parser->parse('<?php
class A {
/** @return array<A|null> */
public static function loadMultiple()
{
return [new A, null];
}
/** @return void */
public function barBar() {
}
}
foreach (A::loadMultiple() as $a) {
if ($a === null) {
continue;
}
$a->barBar();
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
/**
* @return void
*/