1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/tests/TraitTest.php
Matthew Brown 3e78405836 # This is a combination of 4 commits.
# The first commit's message is:
Make cofig schema more relaxed about ordering

# This is the 2nd commit message:

Add tests for awkward case

# This is the 3rd commit message:

Fix static calls to class methods within traits

# This is the 4th commit message:

Repopulate fewer arrays
2016-12-30 01:50:33 -05:00

200 lines
4.2 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();
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();
}
public function testStaticClassMethodFromWithinTrait()
{
$stmts = self::$parser->parse('<?php
trait A {
public function foo() : void {
self::bar();
}
}
class B {
use A;
public static function bar() : void {
}
}
');
$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();
}
}