1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-06 04:59:14 +01:00
psalm/tests/TraitTest.php

416 lines
9.2 KiB
PHP
Raw Normal View History

2016-12-17 04:16:29 +01:00
<?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;
/** @var TestConfig */
protected static $config;
/** @var \Psalm\Checker\ProjectChecker */
protected $project_checker;
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-12-17 04:16:29 +01:00
public static function setUpBeforeClass()
{
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
self::$config = new TestConfig();
2016-12-17 04:16:29 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-12-17 04:16:29 +01:00
public function setUp()
{
FileChecker::clearCache();
$this->project_checker = new \Psalm\Checker\ProjectChecker();
$this->project_checker->setConfig(self::$config);
2016-12-17 04:16:29 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-12-17 04:16:29 +01:00
public function testAccessiblePrivateMethodFromTrait()
{
$stmts = self::$parser->parse('<?php
trait T {
2016-12-30 19:09:00 +01:00
private function fooFoo() : void {
2016-12-17 04:16:29 +01:00
}
}
class B {
use T;
2016-12-17 04:16:29 +01:00
public function doFoo() : void {
2016-12-30 19:09:00 +01:00
$this->fooFoo();
2016-12-17 04:16:29 +01:00
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-17 04:16:29 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-12-17 04:16:29 +01:00
public function testAccessibleProtectedMethodFromTrait()
{
$stmts = self::$parser->parse('<?php
trait T {
2016-12-30 19:09:00 +01:00
protected function fooFoo() : void {
2016-12-17 04:16:29 +01:00
}
}
class B {
use T;
2016-12-17 04:16:29 +01:00
public function doFoo() : void {
2016-12-30 19:09:00 +01:00
$this->fooFoo();
2016-12-17 04:16:29 +01:00
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-17 04:16:29 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-12-17 04:16:29 +01:00
public function testAccessiblePublicMethodFromTrait()
{
$stmts = self::$parser->parse('<?php
trait T {
2016-12-30 19:09:00 +01:00
public function fooFoo() : void {
2016-12-17 04:16:29 +01:00
}
}
class B {
use T;
2016-12-17 04:16:29 +01:00
public function doFoo() : void {
2016-12-30 19:09:00 +01:00
$this->fooFoo();
2016-12-17 04:16:29 +01:00
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-17 04:16:29 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2017-01-07 20:35:07 +01:00
public function testAccessiblePrivatePropertyFromTrait()
{
$stmts = self::$parser->parse('<?php
trait T {
/** @var string */
private $fooFoo = "";
2017-01-07 20:35:07 +01:00
}
class B {
use T;
public function doFoo() : void {
echo $this->fooFoo;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2017-01-07 20:35:07 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2017-01-07 20:35:07 +01:00
public function testAccessibleProtectedPropertyFromTrait()
{
$stmts = self::$parser->parse('<?php
trait T {
/** @var string */
protected $fooFoo = "";
2017-01-07 20:35:07 +01:00
}
class B {
use T;
public function doFoo() : void {
echo $this->fooFoo;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2017-01-07 20:35:07 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2017-01-07 20:35:07 +01:00
public function testAccessiblePublicPropertyFromTrait()
{
$stmts = self::$parser->parse('<?php
trait T {
/** @var string */
public $fooFoo = "";
2017-01-07 20:35:07 +01:00
}
class B {
use T;
public function doFoo() : void {
echo $this->fooFoo;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2017-01-07 20:35:07 +01:00
}
2016-12-17 04:16:29 +01:00
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InaccessibleMethod
2017-01-13 20:07:23 +01:00
* @return void
2016-12-17 04:16:29 +01:00
*/
public function testInccessiblePrivateMethodFromInheritedTrait()
{
$stmts = self::$parser->parse('<?php
trait T {
2016-12-30 19:09:00 +01:00
private function fooFoo() : void {
2016-12-17 04:16:29 +01:00
}
}
class B {
use T;
2016-12-17 04:16:29 +01:00
}
class C extends B {
public function doFoo() : void {
2016-12-30 19:09:00 +01:00
$this->fooFoo();
2016-12-17 04:16:29 +01:00
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-17 04:16:29 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-12-17 04:16:29 +01:00
public function testAccessibleProtectedMethodFromInheritedTrait()
{
$stmts = self::$parser->parse('<?php
trait T {
2016-12-30 19:09:00 +01:00
protected function fooFoo() : void {
2016-12-17 04:16:29 +01:00
}
}
class B {
use T;
2016-12-17 04:16:29 +01:00
}
class C extends B {
public function doFoo() : void {
2016-12-30 19:09:00 +01:00
$this->fooFoo();
2016-12-17 04:16:29 +01:00
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-17 04:16:29 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-12-17 04:16:29 +01:00
public function testAccessiblePublicMethodFromInheritedTrait()
{
$stmts = self::$parser->parse('<?php
trait T {
2016-12-30 19:09:00 +01:00
public function fooFoo() : void {
2016-12-17 04:16:29 +01:00
}
}
class B {
use T;
2016-12-17 04:16:29 +01:00
}
class C extends B {
public function doFoo() : void {
2016-12-30 19:09:00 +01:00
$this->fooFoo();
2016-12-17 04:16:29 +01:00
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-17 04:16:29 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testStaticClassMethodFromWithinTrait()
{
$stmts = self::$parser->parse('<?php
trait T {
2016-12-30 19:09:00 +01:00
public function fooFoo() : void {
self::barBar();
}
}
class B {
use T;
2016-12-30 19:09:00 +01:00
public static function barBar() : void {
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
2016-12-17 04:16:29 +01:00
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-17 04:16:29 +01:00
* @expectedExceptionMessage UndefinedTrait
2017-01-13 20:07:23 +01:00
* @return void
2016-12-17 04:16:29 +01:00
*/
public function testUndefinedTrait()
{
$stmts = self::$parser->parse('<?php
class B {
use A;
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-17 04:16:29 +01:00
}
2016-12-31 17:49:04 +01:00
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2017-01-12 03:37:53 +01:00
public function testRedefinedTraitMethodWithoutAlias()
2017-01-07 20:35:07 +01:00
{
$stmts = self::$parser->parse('<?php
trait T {
public function fooFoo() : void {
}
}
class B {
use T;
public function fooFoo(string $a) : void {
}
}
(new B)->fooFoo("hello");
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2017-01-07 20:35:07 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2017-01-07 20:35:07 +01:00
public function testRedefinedTraitMethodWithAlias()
{
$stmts = self::$parser->parse('<?php
trait T {
public function fooFoo() : void {
}
}
class B {
use T {
fooFoo as barBar;
}
public function fooFoo() : void {
$this->barBar();
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2017-01-07 20:35:07 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-12-31 17:49:04 +01:00
public function testTraitSelf()
{
$stmts = self::$parser->parse('<?php
trait T {
public function g(): self
{
return $this;
}
}
class A {
use T;
}
$a = (new A)->g();
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-12-31 17:49:04 +01:00
$this->assertEquals('A', (string) $context->vars_in_scope['$a']);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-12-31 17:49:04 +01:00
public function testParentTraitSelf()
{
$stmts = self::$parser->parse('<?php
trait T {
public function g(): self
{
return $this;
}
}
class A {
use T;
}
class B extends A {
}
class C {
use T;
}
2016-12-31 17:49:04 +01:00
$a = (new B)->g();
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-12-31 17:49:04 +01:00
$this->assertEquals('A', (string) $context->vars_in_scope['$a']);
}
2016-12-17 04:16:29 +01:00
}