mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 04:45:20 +01:00
181 lines
3.9 KiB
PHP
181 lines
3.9 KiB
PHP
|
<?php
|
||
|
namespace Psalm\Tests;
|
||
|
|
||
|
use PhpParser\ParserFactory;
|
||
|
use PHPUnit_Framework_TestCase;
|
||
|
use Psalm\Checker\FileChecker;
|
||
|
use Psalm\Config;
|
||
|
use Psalm\Context;
|
||
|
|
||
|
class TraitTest extends PHPUnit_Framework_TestCase
|
||
|
{
|
||
|
/** @var \PhpParser\Parser */
|
||
|
protected static $parser;
|
||
|
|
||
|
public static function setUpBeforeClass()
|
||
|
{
|
||
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
||
|
}
|
||
|
|
||
|
public function setUp()
|
||
|
{
|
||
|
$config = new TestConfig();
|
||
|
$config->throw_exception = true;
|
||
|
$config->use_docblock_types = true;
|
||
|
|
||
|
FileChecker::clearCache();
|
||
|
}
|
||
|
|
||
|
public function testAccessiblePrivateMethodFromTrait()
|
||
|
{
|
||
|
$stmts = self::$parser->parse('<?php
|
||
|
trait A {
|
||
|
private function foo() : void {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class B {
|
||
|
use A;
|
||
|
|
||
|
public function doFoo() : void {
|
||
|
$this->foo();
|
||
|
}
|
||
|
}
|
||
|
');
|
||
|
|
||
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
||
|
$file_checker->check();
|
||
|
}
|
||
|
|
||
|
public function testAccessibleProtectedMethodFromTrait()
|
||
|
{
|
||
|
$stmts = self::$parser->parse('<?php
|
||
|
trait A {
|
||
|
protected function foo() : void {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class B {
|
||
|
use A;
|
||
|
|
||
|
public function doFoo() : void {
|
||
|
$this->foo();
|
||
|
}
|
||
|
}
|
||
|
');
|
||
|
|
||
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
||
|
$file_checker->check();
|
||
|
}
|
||
|
|
||
|
public function testAccessiblePublicMethodFromTrait()
|
||
|
{
|
||
|
$stmts = self::$parser->parse('<?php
|
||
|
trait A {
|
||
|
public function foo() : void {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class B {
|
||
|
use A;
|
||
|
|
||
|
public function doFoo() : void {
|
||
|
$this->foo();
|
||
|
}
|
||
|
}
|
||
|
');
|
||
|
|
||
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
||
|
$file_checker->check();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @expectedException \Psalm\Exception\CodeException
|
||
|
* @expectedExceptionMessage UndefinedMethod
|
||
|
*/
|
||
|
public function testInccessiblePrivateMethodFromInheritedTrait()
|
||
|
{
|
||
|
$stmts = self::$parser->parse('<?php
|
||
|
trait A {
|
||
|
private function foo() : void {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class B {
|
||
|
use A;
|
||
|
}
|
||
|
|
||
|
class C extends B {
|
||
|
public function doFoo() : void {
|
||
|
$this->foo();
|
||
|
}
|
||
|
}
|
||
|
');
|
||
|
|
||
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
||
|
$file_checker->check();
|
||
|
}
|
||
|
|
||
|
public function testAccessibleProtectedMethodFromInheritedTrait()
|
||
|
{
|
||
|
$stmts = self::$parser->parse('<?php
|
||
|
trait A {
|
||
|
protected function foo() : void {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class B {
|
||
|
use A;
|
||
|
}
|
||
|
|
||
|
class C extends B {
|
||
|
public function doFoo() : void {
|
||
|
$this->foo();
|
||
|
}
|
||
|
}
|
||
|
');
|
||
|
|
||
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
||
|
$file_checker->check();
|
||
|
}
|
||
|
|
||
|
public function testAccessiblePublicMethodFromInheritedTrait()
|
||
|
{
|
||
|
$stmts = self::$parser->parse('<?php
|
||
|
trait A {
|
||
|
public function foo() : void {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class B {
|
||
|
use A;
|
||
|
}
|
||
|
|
||
|
class C extends B {
|
||
|
public function doFoo() : void {
|
||
|
$this->foo();
|
||
|
}
|
||
|
}
|
||
|
');
|
||
|
|
||
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
||
|
$file_checker->check();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @expectedException \Psalm\Exception\CodeException
|
||
|
* @expectedExceptionMessage UndefinedTrait
|
||
|
*/
|
||
|
public function testUndefinedTrait()
|
||
|
{
|
||
|
$stmts = self::$parser->parse('<?php
|
||
|
class B {
|
||
|
use A;
|
||
|
}
|
||
|
');
|
||
|
|
||
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
||
|
$file_checker->check();
|
||
|
}
|
||
|
}
|