1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00
psalm/tests/FunctionCallTest.php

129 lines
3.4 KiB
PHP
Raw Normal View History

2016-12-11 23:41:11 -05:00
<?php
namespace Psalm\Tests;
use PhpParser\ParserFactory;
use PHPUnit_Framework_TestCase;
use Psalm\Checker\FileChecker;
use Psalm\Config;
use Psalm\Context;
class FunctionCallTest extends PHPUnit_Framework_TestCase
{
/** @var \PhpParser\Parser */
2016-12-11 23:41:11 -05:00
protected static $parser;
public static function setUpBeforeClass()
{
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
}
2016-12-11 23:41:11 -05:00
public function setUp()
{
2016-12-14 12:28:38 -05:00
$config = new TestConfig();
2016-12-11 23:41:11 -05:00
FileChecker::clearCache();
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidScalarArgument
*/
public function testInvalidScalarArgument()
{
$stmts = self::$parser->parse('<?php
2016-12-30 13:09:00 -05:00
function fooFoo(int $a) : void {}
fooFoo("string");
2016-12-11 23:41:11 -05:00
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
2016-12-14 19:24:33 -05:00
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage MixedArgument
*/
public function testMixedArgument()
{
2016-12-29 20:07:42 -05:00
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
2016-12-14 19:24:33 -05:00
$stmts = self::$parser->parse('<?php
2016-12-30 13:09:00 -05:00
function fooFoo(int $a) : void {}
2016-12-14 19:24:33 -05:00
/** @var mixed */
$a = "hello";
2016-12-30 13:09:00 -05:00
fooFoo($a);
2016-12-14 19:24:33 -05:00
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage NullArgument
*/
public function testNullArgument()
{
$stmts = self::$parser->parse('<?php
2016-12-30 13:09:00 -05:00
function fooFoo(int $a) : void {}
fooFoo(null);
2016-12-14 19:24:33 -05:00
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
2016-12-16 22:16:29 -05:00
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage TooFewArguments
*/
public function testTooFewArguments()
{
$stmts = self::$parser->parse('<?php
2016-12-30 13:09:00 -05:00
function fooFoo(int $a) : void {}
fooFoo();
2016-12-16 22:16:29 -05:00
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage TooManyArguments
*/
public function testTooManyArguments()
{
$stmts = self::$parser->parse('<?php
2016-12-30 13:09:00 -05:00
function fooFoo(int $a) : void {}
fooFoo(5, "dfd");
2016-12-16 22:16:29 -05:00
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage TypeCoercion
*/
public function testTypeCoercion()
{
$stmts = self::$parser->parse('<?php
class A {}
class B extends A{}
2016-12-30 13:09:00 -05:00
function fooFoo(B $b) : void {}
fooFoo(new A());
2016-12-16 22:16:29 -05:00
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
2016-12-11 23:41:11 -05:00
}