1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00
psalm/tests/InterfaceTest.php

202 lines
4.4 KiB
PHP
Raw Normal View History

2016-10-24 19:20:28 -04:00
<?php
namespace Psalm\Tests;
use PhpParser\ParserFactory;
use PHPUnit_Framework_TestCase;
2016-11-02 02:29:00 -04:00
use Psalm\Checker\FileChecker;
use Psalm\Config;
2016-10-24 19:20:28 -04:00
use Psalm\Context;
class InterfaceTest extends PHPUnit_Framework_TestCase
{
/** @var \PhpParser\Parser */
2016-11-02 02:29:00 -04:00
protected static $parser;
2016-10-24 19:20:28 -04:00
public static function setUpBeforeClass()
{
2016-11-02 02:29:00 -04:00
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
2016-10-24 19:20:28 -04:00
2016-12-14 12:28:38 -05:00
$config = new TestConfig();
2016-10-24 19:20:28 -04:00
}
public function setUp()
{
2016-11-02 02:29:00 -04:00
FileChecker::clearCache();
2016-10-24 19:20:28 -04:00
}
2016-11-20 11:51:19 -05:00
public function testExtendsAndImplements()
2016-10-24 19:20:28 -04:00
{
2016-11-02 02:29:00 -04:00
$stmts = self::$parser->parse('<?php
2016-11-02 12:14:21 -04:00
interface A
2016-10-24 19:20:28 -04:00
{
/**
* @return string
*/
public function foo();
}
2016-11-02 12:14:21 -04:00
interface B
2016-10-24 19:20:28 -04:00
{
2016-12-07 14:13:39 -05:00
/**
* @return string
*/
2016-11-20 11:51:19 -05:00
public function bar();
2016-10-24 19:20:28 -04:00
}
2016-11-02 12:14:21 -04:00
interface C extends A, B
2016-10-24 19:20:28 -04:00
{
/**
* @return string
*/
2016-11-20 11:51:19 -05:00
public function baz();
2016-10-24 19:20:28 -04:00
}
2016-11-02 12:14:21 -04:00
class D implements C
2016-10-24 19:20:28 -04:00
{
public function foo()
{
2016-12-07 14:13:39 -05:00
return "hello";
2016-10-24 19:20:28 -04:00
}
public function bar()
{
2016-12-07 14:13:39 -05:00
return "goodbye";
2016-10-24 19:20:28 -04:00
}
public function baz()
{
2016-12-07 14:13:39 -05:00
return "hello again";
2016-10-24 19:20:28 -04:00
}
}
2016-11-02 12:14:21 -04:00
$cee = (new D())->baz();
$dee = (new D())->foo();
2016-10-24 19:20:28 -04:00
?>
');
2016-11-02 02:29:00 -04:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-10-24 19:20:28 -04:00
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('string', (string) $context->vars_in_scope['$cee']);
$this->assertEquals('string', (string) $context->vars_in_scope['$dee']);
}
2016-11-20 11:51:19 -05:00
public function testIsExtendedInterface()
{
$stmts = self::$parser->parse('<?php
interface A
{
/**
* @return string
*/
public function foo();
}
interface B extends A
{
/**
* @return string
*/
public function baz();
}
class C implements B
{
public function foo()
{
2016-12-07 14:13:39 -05:00
return "hello";
2016-11-20 11:51:19 -05:00
}
public function baz()
{
2016-12-07 14:13:39 -05:00
return "goodbye";
2016-11-20 11:51:19 -05:00
}
}
2016-12-07 14:13:39 -05:00
/**
* @param A $a
* @return void
*/
2016-11-20 11:51:19 -05:00
function qux(A $a) {
}
qux(new C());
?>
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
public function testExtendsWithMethod()
{
$stmts = self::$parser->parse('<?php
interface A
{
/**
* @return string
*/
public function foo();
}
interface B extends A
{
public function bar();
}
/** @return void */
function mux(B $b) {
$b->foo();
}
?>
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage NoInterfaceProperties
*/
public function testNoInterfaceProperties()
{
$stmts = self::$parser->parse('<?php
interface A { }
function foo(A $a) : void {
if ($a->bar) {
}
}
?>
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage UnimplementedInterfaceMethod
*/
public function testUnimplementedInterfaceMethod()
{
$stmts = self::$parser->parse('<?php
interface A {
public function foo();
}
class B implements A { }
?>
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
2016-10-24 19:20:28 -04:00
}