1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/MethodSignatureTest.php

118 lines
3.0 KiB
PHP
Raw Normal View History

2016-12-15 01:24:33 +01:00
<?php
namespace Psalm\Tests;
use PhpParser\ParserFactory;
use PHPUnit_Framework_TestCase;
use Psalm\Checker\FileChecker;
use Psalm\Config;
use Psalm\Context;
class MethodSignatureTest extends PHPUnit_Framework_TestCase
{
/** @var \PhpParser\Parser */
protected static $parser;
/** @var TestConfig */
protected static $config;
/** @var \Psalm\Checker\ProjectChecker */
protected $project_checker;
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-12-15 01:24:33 +01:00
public static function setUpBeforeClass()
{
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
self::$config = new TestConfig();
2016-12-15 01:24:33 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-12-15 01:24:33 +01:00
public function setUp()
{
FileChecker::clearCache();
$this->project_checker = new \Psalm\Checker\ProjectChecker();
$this->project_checker->setConfig(self::$config);
2016-12-15 01:24:33 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage Method B::fooFoo has more arguments than parent method A::fooFoo
2017-01-13 20:07:23 +01:00
* @return void
*/
public function testMoreArguments()
{
$stmts = self::$parser->parse('<?php
class A {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a, bool $b) : void {
}
}
class B extends A {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a, bool $b, array $c) : void {
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
2016-12-15 01:24:33 +01:00
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage Method B::fooFoo has fewer arguments than parent method A::fooFoo
2017-01-13 20:07:23 +01:00
* @return void
2016-12-15 01:24:33 +01:00
*/
public function testFewerArguments()
{
$stmts = self::$parser->parse('<?php
class A {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a, bool $b) : void {
2016-12-15 01:24:33 +01:00
}
}
class B extends A {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a) : void {
2016-12-15 01:24:33 +01:00
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-12-15 01:24:33 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-30 19:14:17 +01:00
* @expectedExceptionMessage Argument 1 of B::fooFoo has wrong type 'bool', expecting 'int' as defined by A::foo
2017-01-13 20:07:23 +01:00
* @return void
2016-12-15 01:24:33 +01:00
*/
public function testDifferentArguments()
{
$stmts = self::$parser->parse('<?php
class A {
2016-12-30 19:09:00 +01:00
public function fooFoo(int $a, bool $b) : void {
2016-12-15 01:24:33 +01:00
}
}
class B extends A {
2016-12-30 19:09:00 +01:00
public function fooFoo(bool $b, int $a) : void {
2016-12-15 01:24:33 +01:00
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-12-15 01:24:33 +01:00
}
}