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

Add mo tests

This commit is contained in:
Matthew Brown 2016-07-24 18:02:15 -04:00
parent 0e77403625
commit 70462033c1
3 changed files with 204 additions and 99 deletions

View File

@ -0,0 +1,150 @@
<?php
namespace CodeInspector\Tests;
use PhpParser;
use PhpParser\ParserFactory;
use PHPUnit_Framework_TestCase;
class ArrayReturnTypeTest extends PHPUnit_Framework_TestCase
{
protected static $_parser;
public static function setUpBeforeClass()
{
self::$_parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$config = \CodeInspector\Config::getInstance();
$config->throw_exception = true;
$config->use_docblock_types = true;
}
public function setUp()
{
\CodeInspector\ClassMethodChecker::clearCache();
}
public function testGenericArrayCreation()
{
$stmts = self::$_parser->parse('<?php
class B {
/**
* @return array<int>
*/
public function bar(array $in) {
$out = [];
foreach ($in as $key => $value) {
$out[] = 4;
}
return $out;
}
}');
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
public function testGeneric2DArrayCreation()
{
$stmts = self::$_parser->parse('<?php
class B {
/**
* @return array<array<int>>
*/
public function bar(array $in) {
$out = [];
foreach ($in as $key => $value) {
$out[] = [4];
}
return $out;
}
}');
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
public function testGeneric2DArrayCreationAddedInIf()
{
$stmts = self::$_parser->parse('<?php
class B {
/**
* @return array<array<int>>
*/
public function bar(array $in) {
$out = [];
$bits = [];
foreach ($in as $key => $value) {
if (rand(0,100) > 50) {
$out[] = $bits;
$bits = [];
}
$bits[] = 4;
}
if ($bits) {
$out[] = $bits;
}
return $out;
}
}');
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
public function testGenericArrayCreationWithObjectAddedInIf()
{
$stmts = self::$_parser->parse('<?php
class B {
/**
* @return array<B>
*/
public function bar(array $in) {
$out = [];
if (rand(0,10) === 10) {
$out[] = new B();
}
return $out;
}
}');
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
public function testGenericArrayCreationWithObjectAddedInSwitch()
{
$stmts = self::$_parser->parse('<?php
class B {
/**
* @return array<B>
*/
public function bar(array $in) {
$out = [];
if (rand(0,10) === 10) {
switch (rand(0,10)) {
case 5:
$out[4] = new B();
}
}
return $out;
}
}');
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace CodeInspector\Tests;
use CodeInspector\Type;
use PhpParser;
use PhpParser\ParserFactory;
use PHPUnit_Framework_TestCase;
class IssueSuppressionTest extends PHPUnit_Framework_TestCase
{
protected static $_parser;
protected static $_file_filter;
public static function setUpBeforeClass()
{
self::$_parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$config = \CodeInspector\Config::getInstance();
$config->throw_exception = true;
$filter = new \CodeInspector\Config\FileFilter();
$filter->addExcludeFile('somefile.php');
$filter->makeExclusive();
self::$_file_filter = $filter;
}
public function setUp()
{
\CodeInspector\ClassChecker::clearCache();
\CodeInspector\ClassMethodChecker::clearCache();
\CodeInspector\Config::getInstance()->setIssueHandler('PossiblyUndefinedVariable', null);
}
public function testUndefinedClass()
{
$stmts = self::$_parser->parse('<?php
class A{
/**
* @suppress UndefinedClass
*/
public function a() {
B::foo()->bar()->bat()->baz()->bam()->bas()->bee()->bet()->bes()->bis();
}
}
');
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
}

View File

@ -24,105 +24,6 @@ class ReturnTypeTest extends PHPUnit_Framework_TestCase
\CodeInspector\ClassMethodChecker::clearCache();
}
public function testGenericArrayCreation()
{
$stmts = self::$_parser->parse('<?php
class B {
/**
* @return array<int>
*/
public function bar(array $in) {
$out = [];
foreach ($in as $key => $value) {
}
return $out;
}
}');
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
public function testGenericArrayCreationWithElements()
{
$stmts = self::$_parser->parse('<?php
class B {
/**
* @return array<array<int>>
*/
public function bar(array $in) {
$out = [];
foreach ($in as $key => $value) {
$out[] = 4;
}
return $out;
}
}');
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
public function test2DGenericArrayCreationWithElements()
{
$stmts = self::$_parser->parse('<?php
class B {
/**
* @return array<array<int>>
*/
public function bar(array $in) {
$out = [];
foreach ($in as $key => $value) {
$out[] = [4];
}
return $out;
}
}');
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
public function test2DGenericArrayCreationWithElementsAddedInIf()
{
$stmts = self::$_parser->parse('<?php
class B {
/**
* @return array<array<int>>
*/
public function bar(array $in) {
$out = [];
$bits = [];
foreach ($in as $key => $value) {
if (rand(0,100) > 50) {
$out[] = $bits;
$bits = [];
}
$bits[] = 4;
}
if ($bits) {
$out[] = $bits;
}
return $out;
}
}');
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
public function testReturnTypeAfterUselessNullcheck()
{
$stmts = self::$_parser->parse('<?php