1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 16:59:45 +01:00
psalm/tests/Php70Test.php

227 lines
6.0 KiB
PHP
Raw Normal View History

2016-10-19 04:02:38 +02:00
<?php
namespace Psalm\Tests;
use PhpParser;
use PhpParser\ParserFactory;
use PHPUnit_Framework_TestCase;
use Psalm\Context;
use Psalm\Checker\TypeChecker;
use Psalm\Type;
class Php70Test extends PHPUnit_Framework_TestCase
{
protected static $_parser;
public static function setUpBeforeClass()
{
self::$_parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$config = \Psalm\Config::getInstance();
$config->throw_exception = true;
$config->use_docblock_types = true;
}
public function setUp()
{
\Psalm\Checker\FileChecker::clearCache();
}
public function testFunctionTypeHints()
{
$stmts = self::$_parser->parse('<?php
function indexof(string $haystack, string $needle) : int
{
$pos = strpos($haystack, $needle);
if ($pos === false) {
return -1;
}
return $pos;
}
$a = indexof("arr", "a");
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
}
public function testMethodTypeHints()
{
$stmts = self::$_parser->parse('<?php
class Foo {
public static function indexof(string $haystack, string $needle) : int
{
$pos = strpos($haystack, $needle);
if ($pos === false) {
return -1;
}
return $pos;
}
}
$a = Foo::indexof("arr", "a");
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
}
2016-10-20 06:47:10 +02:00
public function testNullCoalesce()
{
$stmts = self::$_parser->parse('<?php
$a = $_GET["bar"] ?? "nobody";
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('mixed', (string) $context->vars_in_scope['$a']);
}
public function testSpaceship()
{
$stmts = self::$_parser->parse('<?php
$a = 1 <=> 1;
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
}
public function testDefineArray()
{
$stmts = self::$_parser->parse('<?php
define("ANIMALS", [
"dog",
"cat",
"bird"
]);
$a = ANIMALS[1];
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('string', (string) $context->vars_in_scope['$a']);
}
public function testAnonymousClass()
{
$stmts = self::$_parser->parse('<?php
interface Logger {
public function log(string $msg);
}
class Application {
private $logger;
public function getLogger(): Logger {
return $this->logger;
}
public function setLogger(Logger $logger) {
$this->logger = $logger;
}
}
$app = new Application;
$app->setLogger(new class implements Logger {
public function log(string $msg) {
echo $msg;
}
});
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
2016-10-20 20:26:03 +02:00
public function testGeneratorWithReturn()
{
$stmts = self::$_parser->parse('<?php
/**
2016-10-21 00:05:28 +02:00
* @return Generator<int,int>
* @psalm-generator-return string
2016-10-20 20:26:03 +02:00
*/
function foo(int $i) : Generator {
if ($i === 1) {
return "bash";
}
yield 1;
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
2016-10-20 06:47:10 +02:00
}
public function testGeneratorDelegation()
{
$stmts = self::$_parser->parse('<?php
2016-10-20 17:31:19 +02:00
/**
2016-10-20 20:26:03 +02:00
* @return Generator<int,int>
2016-10-21 00:05:28 +02:00
* @psalm-generator-return int
2016-10-20 17:31:19 +02:00
*/
2016-10-20 20:26:03 +02:00
function count_to_ten() : Generator {
2016-10-20 06:47:10 +02:00
yield 1;
yield 2;
2016-10-20 17:31:19 +02:00
yield from [3, 4];
yield from new ArrayIterator([5, 6]);
yield from seven_eight();
return yield from nine_ten();
2016-10-20 06:47:10 +02:00
}
2016-10-20 17:31:19 +02:00
/**
2016-10-20 20:26:03 +02:00
* @return Generator<int,int>
2016-10-20 17:31:19 +02:00
*/
2016-10-20 20:26:03 +02:00
function seven_eight() : Generator {
2016-10-20 17:31:19 +02:00
yield 7;
yield from eight();
2016-10-20 06:47:10 +02:00
}
2016-10-20 17:31:19 +02:00
/**
2016-10-20 20:26:03 +02:00
* @return Generator<int,int>
2016-10-20 17:31:19 +02:00
*/
2016-10-20 20:26:03 +02:00
function eight() : Generator {
2016-10-20 17:31:19 +02:00
yield 8;
}
/**
2016-10-20 20:26:03 +02:00
* @return Generator<int,int>
2016-10-21 00:05:28 +02:00
* @psalm-generator-return int
2016-10-20 17:31:19 +02:00
*/
2016-10-20 20:26:03 +02:00
function nine_ten() : Generator {
2016-10-20 17:31:19 +02:00
yield 9;
return 10;
2016-10-20 06:47:10 +02:00
}
2016-10-20 17:31:19 +02:00
$gen = count_to_ten();
foreach ($gen as $num) {
echo "$num ";
}
$gen2 = $gen->getReturn();
2016-10-20 06:47:10 +02:00
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
2016-10-20 20:26:03 +02:00
$this->assertEquals('Generator<int,int>', (string) $context->vars_in_scope['$gen']);
$this->assertEquals('mixed', (string) $context->vars_in_scope['$gen2']);
2016-10-20 06:47:10 +02:00
}
2016-10-19 04:02:38 +02:00
}