1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00
psalm/tests/InterfaceTest.php

585 lines
14 KiB
PHP
Raw Normal View History

2016-10-25 01:20:28 +02: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-25 01:20:28 +02:00
use Psalm\Context;
class InterfaceTest extends PHPUnit_Framework_TestCase
{
/** @var \PhpParser\Parser */
2016-11-02 07:29:00 +01:00
protected static $parser;
2016-10-25 01:20:28 +02:00
/** @var \Psalm\Checker\ProjectChecker */
protected $project_checker;
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-10-25 01:20:28 +02:00
public static function setUpBeforeClass()
{
2016-11-02 07:29:00 +01:00
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
2016-10-25 01:20:28 +02:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-10-25 01:20:28 +02:00
public function setUp()
{
2016-11-02 07:29:00 +01:00
FileChecker::clearCache();
$this->project_checker = new \Psalm\Checker\ProjectChecker();
$this->project_checker->setConfig(new TestConfig());
2016-10-25 01:20:28 +02:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-11-20 17:51:19 +01:00
public function testExtendsAndImplements()
2016-10-25 01:20:28 +02:00
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-11-02 17:14:21 +01:00
interface A
2016-10-25 01:20:28 +02:00
{
/**
* @return string
*/
2016-12-30 19:09:00 +01:00
public function fooFoo();
2016-10-25 01:20:28 +02:00
}
2016-11-02 17:14:21 +01:00
interface B
2016-10-25 01:20:28 +02:00
{
2016-12-07 20:13:39 +01:00
/**
* @return string
*/
2016-12-30 19:09:00 +01:00
public function barBar();
2016-10-25 01:20:28 +02:00
}
2016-11-02 17:14:21 +01:00
interface C extends A, B
2016-10-25 01:20:28 +02:00
{
/**
* @return string
*/
2016-11-20 17:51:19 +01:00
public function baz();
2016-10-25 01:20:28 +02:00
}
2016-11-02 17:14:21 +01:00
class D implements C
2016-10-25 01:20:28 +02:00
{
2016-12-30 19:09:00 +01:00
public function fooFoo()
2016-10-25 01:20:28 +02:00
{
2016-12-07 20:13:39 +01:00
return "hello";
2016-10-25 01:20:28 +02:00
}
2016-12-30 19:09:00 +01:00
public function barBar()
2016-10-25 01:20:28 +02:00
{
2016-12-07 20:13:39 +01:00
return "goodbye";
2016-10-25 01:20:28 +02:00
}
public function baz()
{
2016-12-07 20:13:39 +01:00
return "hello again";
2016-10-25 01:20:28 +02:00
}
}
2016-11-02 17:14:21 +01:00
$cee = (new D())->baz();
2016-12-30 19:09:00 +01:00
$dee = (new D())->fooFoo();
2016-10-25 01:20:28 +02:00
?>
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-10-25 01:20:28 +02:00
$this->assertEquals('string', (string) $context->vars_in_scope['$cee']);
$this->assertEquals('string', (string) $context->vars_in_scope['$dee']);
}
2016-11-20 17:51:19 +01:00
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-11-20 17:51:19 +01:00
public function testIsExtendedInterface()
{
$stmts = self::$parser->parse('<?php
interface A
{
/**
* @return string
*/
2016-12-30 19:09:00 +01:00
public function fooFoo();
2016-11-20 17:51:19 +01:00
}
interface B extends A
{
/**
* @return string
*/
public function baz();
}
class C implements B
{
2016-12-30 19:09:00 +01:00
public function fooFoo()
2016-11-20 17:51:19 +01:00
{
2016-12-07 20:13:39 +01:00
return "hello";
2016-11-20 17:51:19 +01:00
}
public function baz()
{
2016-12-07 20:13:39 +01:00
return "goodbye";
2016-11-20 17:51:19 +01:00
}
}
2016-12-07 20:13:39 +01:00
/**
* @param A $a
* @return void
*/
2016-11-20 17:51:19 +01:00
function qux(A $a) {
}
qux(new C());
?>
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-11-20 17:51:19 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-11-20 17:51:19 +01:00
public function testExtendsWithMethod()
{
$stmts = self::$parser->parse('<?php
interface A
{
/**
* @return string
*/
2016-12-30 19:09:00 +01:00
public function fooFoo();
2016-11-20 17:51:19 +01:00
}
interface B extends A
{
2016-12-30 19:09:00 +01:00
public function barBar();
2016-11-20 17:51:19 +01:00
}
/** @return void */
function mux(B $b) {
2016-12-30 19:09:00 +01:00
$b->fooFoo();
2016-11-20 17:51:19 +01:00
}
?>
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-11-20 17:51:19 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage NoInterfaceProperties
2017-01-13 20:07:23 +01:00
* @return void
*/
public function testNoInterfaceProperties()
{
$stmts = self::$parser->parse('<?php
interface A { }
2016-12-30 19:09:00 +01:00
function fooFoo(A $a) : void {
if ($a->bar) {
}
}
?>
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage UnimplementedInterfaceMethod
2017-01-13 20:07:23 +01:00
* @return void
*/
public function testUnimplementedInterfaceMethod()
{
$stmts = self::$parser->parse('<?php
interface A {
2016-12-30 19:09:00 +01:00
public function fooFoo();
}
class B implements A { }
?>
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
2016-12-29 02:33:26 +01:00
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-29 02:33:26 +01:00
* @expectedExceptionMessage MethodSignatureMismatch
2017-01-13 20:07:23 +01:00
* @return void
2016-12-29 02:33:26 +01:00
*/
public function testMismatchingInterfaceMethodSignature()
{
$stmts = self::$parser->parse('<?php
interface A {
public function fooFoo(int $a) : void;
2016-12-29 02:33:26 +01:00
}
class B implements A {
public function fooFoo(string $a) : void {
2016-12-29 02:33:26 +01:00
}
}
?>
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testCorrectInterfaceMethodSignature()
{
$stmts = self::$parser->parse('<?php
interface A {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a) : void;
}
class B implements A {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a) : void {
}
}
?>
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testInterfaceMethodImplementedInParent()
{
$stmts = self::$parser->parse('<?php
interface MyInterface {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a) : void;
}
class B {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a) : void {
}
}
class C extends B implements MyInterface { }
?>
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-12-29 02:33:26 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-29 02:33:26 +01:00
* @expectedExceptionMessage MethodSignatureMismatch
2017-01-13 20:07:23 +01:00
* @return void
2016-12-29 02:33:26 +01:00
*/
public function testMismatchingInterfaceMethodSignatureInTrait()
{
$stmts = self::$parser->parse('<?php
interface A {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a, int $b) : void;
2016-12-29 02:33:26 +01:00
}
trait T {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a) : void {
2016-12-29 02:33:26 +01:00
}
}
class B implements A {
use T;
}
?>
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testInterfaceMethodSignatureInTrait()
{
$stmts = self::$parser->parse('<?php
interface A {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a, int $b) : void;
}
trait T {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a, int $b) : void {
}
}
2016-12-29 02:33:26 +01:00
class B implements A {
use T;
}
?>
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-12-29 02:33:26 +01:00
}
2016-12-29 03:37:24 +01:00
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-29 03:37:24 +01:00
* @expectedExceptionMessage MethodSignatureMismatch
2017-01-13 20:07:23 +01:00
* @return void
2016-12-29 03:37:24 +01:00
*/
public function testMismatchingInterfaceMethodSignatureInImplementer()
{
$stmts = self::$parser->parse('<?php
interface A {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a, int $b) : void;
2016-12-29 03:37:24 +01:00
}
trait T {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a, int $b) : void {
2016-12-29 03:37:24 +01:00
}
}
class B implements A {
use T;
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a) : void {
2016-12-29 03:37:24 +01:00
}
}
?>
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testDelayedInterface()
{
$stmts = self::$parser->parse('<?php
// fails in PHP, whatcha gonna do
$c = new C;
class A { }
interface B { }
class C extends A implements B { }
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-12-29 03:37:24 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage UndefinedClass
2017-01-13 20:07:23 +01:00
* @return void
*/
public function testInvalidImplements()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
2017-01-17 01:06:29 +01:00
class C2 implements A { }
'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testTypeDoesNotContainType()
{
$stmts = self::$parser->parse('<?php
interface A { }
interface B {
function foo();
}
function bar(A $a) : void {
if ($a instanceof B) {
$a->foo();
}
}');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/
public function testAbstractInterfaceImplements()
{
$stmts = self::$parser->parse('<?php
interface I {
public function fnc();
}
abstract class A implements I {}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/
public function testImplementsPartialInterfaceMethods()
{
Config::getInstance()->setCustomErrorLevel('MissingReturnType', Config::REPORT_SUPPRESS);
$stmts = self::$parser->parse('<?php
namespace Bat;
interface I {
public function foo();
public function bar();
}
abstract class A implements I {
public function foo() {
return "hello";
}
}
class B extends A {
public function bar() {
return "goodbye";
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage UnimplementedInterfaceMethod
* @return void
*/
public function testAbstractInterfaceImplementsWithSubclass()
{
$stmts = self::$parser->parse('<?php
interface I {
public function fnc();
}
abstract class A implements I {}
class B extends A {}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/
public function testInterfaceConstants()
{
$stmts = self::$parser->parse('<?php
interface I1 {
const A = 5;
const B = "two";
const C = 3.0;
}
interface I2 extends I1 {
const D = 5;
const E = "two";
}
class A implements I2 {
/** @var int */
public $foo = I1::A;
/** @var string */
public $bar = self::B;
/** @var float */
public $bar2 = I2::C;
/** @var int */
public $foo2 = I2::D;
/** @var string */
public $bar3 = self::E;
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/
public function testInterfaceExtendsReturnType()
{
$stmts = self::$parser->parse('<?php
interface A {}
interface B extends A {}
function foo(B $a) : A {
return $a;
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage MoreSpecificReturnType
* @return void
*/
public function testMoreSpecificReturnType()
{
$stmts = self::$parser->parse('<?php
interface A {}
interface B extends A {}
function foo(A $a) : B {
return $a;
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
2016-10-25 01:20:28 +02:00
}