1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00
psalm/tests/Php71Test.php

257 lines
6.4 KiB
PHP
Raw Normal View History

2016-12-03 23:03:51 -05:00
<?php
namespace Psalm\Tests;
use PhpParser\ParserFactory;
use PHPUnit_Framework_TestCase;
use Psalm\Checker\FileChecker;
use Psalm\Config;
use Psalm\Context;
class Php71Test extends PHPUnit_Framework_TestCase
{
protected static $parser;
public static function setUpBeforeClass()
{
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$config = Config::getInstance();
$config->throw_exception = true;
$config->use_docblock_types = true;
}
public function setUp()
{
FileChecker::clearCache();
}
public function testNullableReturnType()
{
$stmts = self::$parser->parse('<?php
function a(): ?string
{
return rand(0, 10) ? "elePHPant" : null;
}
$a = a();
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('string|null', (string) $context->vars_in_scope['$a']);
}
public function testNullableArgument()
{
$stmts = self::$parser->parse('<?php
function test(?string $name) : ?string
{
return $name;
}
test("elePHPant");
test(null);
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
public function testPrivateClassConst()
{
$stmts = self::$parser->parse('<?php
class A
{
private const IS_PRIVATE = 1;
function foo() : int {
return A::IS_PRIVATE;
}
}
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InaccessibleClassConstant
*/
public function testInvalidPrivateClassConstFetch()
{
$stmts = self::$parser->parse('<?php
class A
{
private const IS_PRIVATE = 1;
}
echo A::IS_PRIVATE;
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InaccessibleClassConstant
*/
public function testInvalidPrivateClassConstFetchFromSubclass()
{
$stmts = self::$parser->parse('<?php
class A
{
private const IS_PRIVATE = 1;
}
class B extends A
{
function foo() : int {
return A::IS_PRIVATE;
}
}
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
public function testProtectedClassConst()
{
$stmts = self::$parser->parse('<?php
class A
{
protected const IS_PROTECTED = 1;
}
class B extends A
{
function foo() : int {
return A::IS_PROTECTED;
}
}
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InaccessibleClassConstant
*/
public function testInvalidProtectedClassConstFetch()
{
$stmts = self::$parser->parse('<?php
class A
{
protected const IS_PROTECTED = 1;
}
echo A::IS_PROTECTED;
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
public function testPublicClassConstFetch()
{
$stmts = self::$parser->parse('<?php
class A
{
public const IS_PUBLIC = 1;
const IS_ALSO_PUBLIC = 2;
}
class B extends A
{
function foo() : int {
echo A::IS_PUBLIC;
return A::IS_ALSO_PUBLIC;
}
}
echo A::IS_PUBLIC;
echo A::IS_ALSO_PUBLIC;
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
2016-12-04 13:35:38 -05:00
public function testArrayDestructuring()
{
$stmts = self::$parser->parse('<?php
$data = [
[1, "Tom"],
[2, "Fred"],
];
// list() style
list($id1, $name1) = $data[0];
// [] style
[$id2, $name2] = $data[1];
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('string|int', (string) $context->vars_in_scope['$id1']);
$this->assertEquals('string|int', (string) $context->vars_in_scope['$name1']);
$this->assertEquals('string|int', (string) $context->vars_in_scope['$id2']);
$this->assertEquals('string|int', (string) $context->vars_in_scope['$name2']);
}
public function testArrayListDestructuringInForeach()
{
$stmts = self::$parser->parse('<?php
$data = [
[1, "Tom"],
[2, "Fred"],
];
// list() style
foreach ($data as list($id, $name)) {
echo $id;
echo $name;
}
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
public function testArrayDestructuringInForeach()
{
$stmts = self::$parser->parse('<?php
$data = [
[1, "Tom"],
[2, "Fred"],
];
// [] style
foreach ($data as [$id, $name]) {
echo $id;
echo $name;
}
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
2016-12-03 23:03:51 -05:00
}