1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-04 02:27:59 +01:00
psalm/tests/IncludeTest.php

205 lines
4.5 KiB
PHP
Raw Normal View History

2017-01-08 01:07:58 +01:00
<?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;
2017-01-08 01:07:58 +01:00
/** @var \Psalm\Checker\ProjectChecker */
protected $project_checker;
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2017-01-08 01:07:58 +01:00
public static function setUpBeforeClass()
{
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
self::$config = new TestConfig();
2017-01-08 01:07:58 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2017-01-08 01:07:58 +01:00
public function setUp()
{
FileChecker::clearCache();
$this->project_checker = new \Psalm\Checker\ProjectChecker();
$this->project_checker->setConfig(self::$config);
2017-01-08 01:07:58 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2017-01-08 01:07:58 +01:00
public function testBasicRequire()
{
$this->project_checker->registerFile(
2017-01-18 04:10:21 +01:00
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
2017-01-08 01:07:58 +01:00
'<?php
class A{
}
'
);
$file2_checker = new FileChecker(
2017-01-18 04:10:21 +01:00
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
2017-01-08 01:07:58 +01:00
$this->project_checker,
self::$parser->parse('<?php
require("file1.php");
class B {
public function foo() : void {
(new A);
}
}
')
);
$file2_checker->visitAndAnalyzeMethods();
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2017-01-08 01:07:58 +01:00
public function testNestedRequire()
{
$this->project_checker->registerFile(
2017-01-18 04:10:21 +01:00
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
2017-01-08 01:07:58 +01:00
'<?php
class A{
public function fooFoo() : void {
}
}
'
);
$this->project_checker->registerFile(
2017-01-18 04:10:21 +01:00
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
2017-01-08 01:07:58 +01:00
'<?php
require("file1.php");
class B extends A{
}
'
);
$file2_checker = new FileChecker(
2017-01-18 04:10:21 +01:00
getcwd() . DIRECTORY_SEPARATOR . 'file3.php',
2017-01-08 01:07:58 +01:00
$this->project_checker,
self::$parser->parse('<?php
require("file2.php");
class C extends B {
public function doFoo() : void {
$this->fooFoo();
}
}
')
);
$file2_checker->visitAndAnalyzeMethods();
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2017-01-08 01:07:58 +01:00
public function testRequireNamespace()
{
$this->project_checker->registerFile(
2017-01-18 04:10:21 +01:00
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
2017-01-08 01:07:58 +01:00
'<?php
namespace Foo;
class A{
}
'
);
$file2_checker = new FileChecker(
2017-01-18 04:10:21 +01:00
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
2017-01-08 01:07:58 +01:00
$this->project_checker,
self::$parser->parse('<?php
require("file1.php");
class B {
public function foo() : void {
(new Foo\A);
}
}
')
);
$file2_checker->visitAndAnalyzeMethods();
}
2017-01-08 01:33:33 +01:00
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2017-01-08 01:33:33 +01:00
public function testRequireFunction()
{
$this->project_checker->registerFile(
2017-01-18 04:10:21 +01:00
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
2017-01-08 01:33:33 +01:00
'<?php
function fooFoo() : void {
}
'
);
$file2_checker = new FileChecker(
2017-01-18 04:10:21 +01:00
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
2017-01-08 01:33:33 +01:00
$this->project_checker,
self::$parser->parse('<?php
require("file1.php");
fooFoo();
')
);
$file2_checker->visitAndAnalyzeMethods();
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testRequireNamespaceWithUse()
{
$this->project_checker->registerFile(
2017-01-18 04:10:21 +01:00
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
'<?php
namespace Foo;
class A{
}
'
);
$file2_checker = new FileChecker(
2017-01-18 04:10:21 +01:00
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();
}
2017-01-08 01:07:58 +01:00
}