mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Add sanity check tests
This commit is contained in:
parent
2bfe45ce24
commit
d2243c3317
@ -761,4 +761,117 @@ class TypeTest extends PHPUnit_Framework_TestCase
|
||||
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
||||
$file_checker->check();
|
||||
}
|
||||
|
||||
public function testVariableReassignment()
|
||||
{
|
||||
$stmts = self::$_parser->parse('<?php
|
||||
class One {
|
||||
public function foo() {}
|
||||
}
|
||||
|
||||
class Two {
|
||||
public function bar() {}
|
||||
}
|
||||
|
||||
$one = new One();
|
||||
|
||||
$one = new Two();
|
||||
|
||||
$one->bar();
|
||||
|
||||
');
|
||||
|
||||
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
||||
$file_checker->check();
|
||||
}
|
||||
|
||||
public function testVariableReassignmentInIf()
|
||||
{
|
||||
$stmts = self::$_parser->parse('<?php
|
||||
class One {
|
||||
public function foo() {}
|
||||
}
|
||||
|
||||
class Two {
|
||||
public function bar() {}
|
||||
}
|
||||
|
||||
$one = new One();
|
||||
|
||||
if (1 + 1 === 2) {
|
||||
$one = new Two();
|
||||
|
||||
$one->bar();
|
||||
}
|
||||
|
||||
');
|
||||
|
||||
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
||||
$file_checker->check();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException CodeInspector\CodeException
|
||||
*/
|
||||
public function testVariableReassignmentInIfWithOutsideCall()
|
||||
{
|
||||
$stmts = self::$_parser->parse('<?php
|
||||
class One {
|
||||
public function foo() {}
|
||||
}
|
||||
|
||||
class Two {
|
||||
public function bar() {}
|
||||
}
|
||||
|
||||
$one = new One();
|
||||
|
||||
if (1 + 1 === 2) {
|
||||
$one = new Two();
|
||||
|
||||
$one->bar();
|
||||
}
|
||||
|
||||
$one->bar();
|
||||
|
||||
');
|
||||
|
||||
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
||||
$file_checker->check();
|
||||
}
|
||||
|
||||
public function testUnionTypeFlow()
|
||||
{
|
||||
$stmts = self::$_parser->parse('<?php
|
||||
class One {
|
||||
public function foo() {}
|
||||
}
|
||||
|
||||
class Two {
|
||||
public function bar() {}
|
||||
}
|
||||
|
||||
class Three {
|
||||
public function baz() {}
|
||||
}
|
||||
|
||||
/** @var One|Two|Three|null */
|
||||
$var = null;
|
||||
|
||||
if ($var instanceof One) {
|
||||
$var->foo();
|
||||
}
|
||||
else {
|
||||
if ($var instanceof Two) {
|
||||
$var->bar();
|
||||
}
|
||||
else if ($var) {
|
||||
$var->baz();
|
||||
}
|
||||
}
|
||||
');
|
||||
|
||||
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
||||
$file_checker->check();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user