1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/ClosureTest.php

194 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
use PhpParser\ParserFactory;
use PHPUnit_Framework_TestCase;
2016-11-02 07:29:00 +01:00
use Psalm\Checker\FileChecker;
use Psalm\Config;
use Psalm\Context;
class ClosureTest extends PHPUnit_Framework_TestCase
{
/** @var \PhpParser\Parser */
2016-11-02 07:29:00 +01:00
protected static $parser;
/** @var \Psalm\Checker\ProjectChecker */
protected $project_checker;
public static function setUpBeforeClass()
{
2016-11-02 07:29:00 +01:00
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
2016-12-14 18:28:38 +01:00
$config = new TestConfig();
}
public function setUp()
{
2016-11-02 07:29:00 +01:00
FileChecker::clearCache();
$this->project_checker = new \Psalm\Checker\ProjectChecker();
}
public function testByRefUseVar()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-11-11 23:13:24 +01:00
/** @return void */
function run_function(\Closure $fnc) {
$fnc();
}
// here we have to make sure $data exists as a side-effect of calling `run_function`
// because it could exist depending on how run_function is implemented
/**
* @return void
* @psalm-suppress MixedArgument
*/
function fn() {
run_function(
2016-12-07 20:13:39 +01:00
/**
* @return void
*/
function() use(&$data) {
$data = 1;
}
);
echo $data;
}
fn();
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
}
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidScalarArgument
*/
public function testWrongArg()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
$bar = ["foo", "bar"];
$bam = array_map(
2016-12-07 20:13:39 +01:00
function(int $a) : int {
return $a + 1;
},
$bar
);
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
}
2016-12-07 20:13:39 +01:00
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidReturnType
*/
public function testNoReturn()
{
$stmts = self::$parser->parse('<?php
$bar = ["foo", "bar"];
$bam = array_map(
function(string $a) : string {
},
$bar
);
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
2016-12-07 20:13:39 +01:00
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
2016-12-07 20:13:39 +01:00
}
public function testInferredArg()
{
$stmts = self::$parser->parse('<?php
$bar = ["foo", "bar"];
$bam = array_map(
/**
* @psalm-suppress MissingReturnType
*/
function(string $a) {
return $a . "blah";
},
$bar
);
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
2016-12-07 20:13:39 +01:00
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
2016-12-07 20:13:39 +01:00
}
public function testVarReturnType()
{
$stmts = self::$parser->parse('<?php
$add_one = function(int $a) : int {
return $a + 1;
};
$a = $add_one(1);
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
}
2017-01-09 07:27:35 +01:00
public function testCallableToClosure()
{
$stmts = self::$parser->parse('<?php
/**
* @return callable
*/
function foo() {
return function(string $a) : string {
return $a . "blah";
};
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
}
public function testCallable()
{
$stmts = self::$parser->parse('<?php
function foo(Callable $c) : void {
echo (string)$c();
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidFunctionCall
*/
public function testStringFunctionCall()
{
$stmts = self::$parser->parse('<?php
$bad_one = "hello";
$a = $bad_one(1);
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
}
}