mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 12:55:26 +01:00
205 lines
4.5 KiB
PHP
205 lines
4.5 KiB
PHP
<?php
|
|
namespace Psalm\Tests;
|
|
|
|
use PhpParser\ParserFactory;
|
|
use PHPUnit_Framework_TestCase;
|
|
use Psalm\Checker\FileChecker;
|
|
use Psalm\Config;
|
|
|
|
class IncludeTest extends PHPUnit_Framework_TestCase
|
|
{
|
|
/** @var \PhpParser\Parser */
|
|
protected static $parser;
|
|
|
|
/** @var TestConfig */
|
|
protected static $config;
|
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
protected $project_checker;
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public static function setUpBeforeClass()
|
|
{
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
self::$config = new TestConfig();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function setUp()
|
|
{
|
|
FileChecker::clearCache();
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
|
$this->project_checker->setConfig(self::$config);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testBasicRequire()
|
|
{
|
|
$this->project_checker->registerFile(
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
|
|
'<?php
|
|
class A{
|
|
}
|
|
'
|
|
);
|
|
|
|
$file2_checker = new FileChecker(
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
|
$this->project_checker,
|
|
self::$parser->parse('<?php
|
|
require("file1.php");
|
|
|
|
class B {
|
|
public function foo() : void {
|
|
(new A);
|
|
}
|
|
}
|
|
')
|
|
);
|
|
|
|
$file2_checker->visitAndAnalyzeMethods();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testNestedRequire()
|
|
{
|
|
$this->project_checker->registerFile(
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
|
|
'<?php
|
|
class A{
|
|
public function fooFoo() : void {
|
|
|
|
}
|
|
}
|
|
'
|
|
);
|
|
|
|
$this->project_checker->registerFile(
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
|
'<?php
|
|
require("file1.php");
|
|
|
|
class B extends A{
|
|
}
|
|
'
|
|
);
|
|
|
|
$file2_checker = new FileChecker(
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file3.php',
|
|
$this->project_checker,
|
|
self::$parser->parse('<?php
|
|
require("file2.php");
|
|
|
|
class C extends B {
|
|
public function doFoo() : void {
|
|
$this->fooFoo();
|
|
}
|
|
}
|
|
')
|
|
);
|
|
|
|
$file2_checker->visitAndAnalyzeMethods();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testRequireNamespace()
|
|
{
|
|
$this->project_checker->registerFile(
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
|
|
'<?php
|
|
namespace Foo;
|
|
|
|
class A{
|
|
}
|
|
'
|
|
);
|
|
|
|
$file2_checker = new FileChecker(
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
|
$this->project_checker,
|
|
self::$parser->parse('<?php
|
|
require("file1.php");
|
|
|
|
class B {
|
|
public function foo() : void {
|
|
(new Foo\A);
|
|
}
|
|
}
|
|
')
|
|
);
|
|
|
|
$file2_checker->visitAndAnalyzeMethods();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testRequireFunction()
|
|
{
|
|
$this->project_checker->registerFile(
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
|
|
'<?php
|
|
function fooFoo() : void {
|
|
|
|
}
|
|
'
|
|
);
|
|
|
|
$file2_checker = new FileChecker(
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
|
$this->project_checker,
|
|
self::$parser->parse('<?php
|
|
require("file1.php");
|
|
|
|
fooFoo();
|
|
')
|
|
);
|
|
|
|
$file2_checker->visitAndAnalyzeMethods();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testRequireNamespaceWithUse()
|
|
{
|
|
$this->project_checker->registerFile(
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
|
|
'<?php
|
|
namespace Foo;
|
|
|
|
class A{
|
|
}
|
|
'
|
|
);
|
|
|
|
$file2_checker = new FileChecker(
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
|
$this->project_checker,
|
|
self::$parser->parse('<?php
|
|
require("file1.php");
|
|
|
|
use Foo\A;
|
|
|
|
class B {
|
|
public function foo() : void {
|
|
(new A);
|
|
}
|
|
}
|
|
')
|
|
);
|
|
|
|
$file2_checker->visitAndAnalyzeMethods();
|
|
}
|
|
}
|