1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 09:37:59 +01:00
psalm/tests/ListTest.php

142 lines
3.7 KiB
PHP
Raw Normal View History

2016-10-31 00:52:35 +01:00
<?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;
2016-10-31 00:52:35 +01:00
use Psalm\Context;
2016-11-01 19:32:19 +01:00
class ListTest extends PHPUnit_Framework_TestCase
2016-10-31 00:52:35 +01:00
{
/** @var \PhpParser\Parser */
2016-11-02 07:29:00 +01:00
protected static $parser;
2016-10-31 00:52:35 +01:00
/** @var \Psalm\Checker\ProjectChecker */
protected $project_checker;
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-10-31 00:52:35 +01:00
public static function setUpBeforeClass()
{
2016-11-02 07:29:00 +01:00
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
2016-10-31 00:52:35 +01:00
2016-12-14 18:28:38 +01:00
$config = new TestConfig();
2016-10-31 00:52:35 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-10-31 00:52:35 +01:00
public function setUp()
{
2016-11-02 07:29:00 +01:00
FileChecker::clearCache();
$this->project_checker = new \Psalm\Checker\ProjectChecker();
2016-10-31 00:52:35 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-10-31 00:52:35 +01:00
public function testSimpleVars()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-10-31 00:52:35 +01:00
list($a, $b) = ["a", "b"];
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-10-31 00:52:35 +01:00
$this->assertEquals('string', (string) $context->vars_in_scope['$a']);
$this->assertEquals('string', (string) $context->vars_in_scope['$b']);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-10-31 00:52:35 +01:00
public function testSimpleVarsWithSeparateTypes()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-10-31 00:52:35 +01:00
list($a, $b) = ["a", 2];
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-10-31 00:52:35 +01:00
$this->assertEquals('string', (string) $context->vars_in_scope['$a']);
$this->assertEquals('int', (string) $context->vars_in_scope['$b']);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-10-31 00:52:35 +01:00
public function testSimpleVarsWithSeparateTypesInVar()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-10-31 00:52:35 +01:00
$bar = ["a", 2];
list($a, $b) = $bar;
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-10-31 00:52:35 +01:00
$this->assertEquals('int|string', (string) $context->vars_in_scope['$a']);
$this->assertEquals('int|string', (string) $context->vars_in_scope['$b']);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-10-31 00:52:35 +01:00
public function testThisVar()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-10-31 00:52:35 +01:00
class A {
2016-11-01 19:32:19 +01:00
/** @var string */
public $a = "";
2016-11-01 19:32:19 +01:00
/** @var string */
public $b = "";
2016-11-01 19:32:19 +01:00
2016-12-30 19:09:00 +01:00
public function fooFoo() : string
2016-11-01 19:32:19 +01:00
{
list($this->a, $this->b) = ["a", "b"];
return $this->a;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-11-01 19:32:19 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-11-01 19:32:19 +01:00
* @expectedExceptionMessage InvalidPropertyAssignment - somefile.php:11
2017-01-13 20:07:23 +01:00
* @return void
2016-11-01 19:32:19 +01:00
*/
public function testThisVarWithBadType()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-11-01 19:32:19 +01:00
class A {
/** @var int */
public $a = 0;
2016-11-01 19:32:19 +01:00
/** @var string */
public $b = "";
2016-10-31 00:52:35 +01:00
2016-12-30 19:09:00 +01:00
public function fooFoo() : string
2016-10-31 00:52:35 +01:00
{
list($this->a, $this->b) = ["a", "b"];
return $this->a;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-10-31 00:52:35 +01:00
}
}