2017-04-25 05:45:02 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-12-15 12:18:33 +01:00
|
|
|
use PHPUnit\Framework\TestCase as BaseTestCase;
|
2017-04-25 05:45:02 +02:00
|
|
|
use Psalm\Checker\FileChecker;
|
2018-01-02 03:17:23 +01:00
|
|
|
use Psalm\Checker\ProjectChecker;
|
2017-04-25 05:45:02 +02:00
|
|
|
|
2017-12-15 12:18:33 +01:00
|
|
|
class TestCase extends BaseTestCase
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
2017-07-25 23:04:58 +02:00
|
|
|
/** @var string */
|
|
|
|
protected static $src_dir_path;
|
|
|
|
|
2018-01-02 03:17:23 +01:00
|
|
|
/** @var ProjectChecker */
|
2017-04-25 05:45:02 +02:00
|
|
|
protected $project_checker;
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
/** @var Provider\FakeFileProvider */
|
|
|
|
protected $file_provider;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2018-01-09 15:21:54 +01:00
|
|
|
ini_set('memory_limit', '-1');
|
2017-04-25 05:45:02 +02:00
|
|
|
parent::setUpBeforeClass();
|
2017-07-25 23:04:58 +02:00
|
|
|
self::$src_dir_path = getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
FileChecker::clearCache();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
$this->file_provider = new Provider\FakeFileProvider();
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$config = new TestConfig();
|
|
|
|
$parser_cache_provider = new Provider\FakeParserCacheProvider();
|
|
|
|
|
2018-01-02 03:17:23 +01:00
|
|
|
$this->project_checker = new ProjectChecker(
|
2018-01-21 19:38:51 +01:00
|
|
|
$config,
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->file_provider,
|
2018-02-04 00:52:35 +01:00
|
|
|
$parser_cache_provider,
|
2018-02-19 06:27:39 +01:00
|
|
|
new \Psalm\Provider\NoCache\NoFileStorageCacheProvider(),
|
|
|
|
new \Psalm\Provider\NoCache\NoClassLikeStorageCacheProvider(),
|
2018-02-04 00:52:35 +01:00
|
|
|
false,
|
|
|
|
true,
|
|
|
|
ProjectChecker::TYPE_CONSOLE,
|
|
|
|
1,
|
|
|
|
false
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2017-09-03 00:15:52 +02:00
|
|
|
$this->project_checker->infer_types_from_usage = true;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
* @param string $contents
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addFile($file_path, $contents)
|
|
|
|
{
|
|
|
|
$this->file_provider->registerFile($file_path, $contents);
|
2018-02-04 00:52:35 +01:00
|
|
|
$this->project_checker->getCodeBase()->scanner->queueFileForScanning($file_path);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2018-01-21 16:22:04 +01:00
|
|
|
|
|
|
|
/**
|
2018-01-21 19:38:51 +01:00
|
|
|
* @param string $file_path
|
|
|
|
* @param \Psalm\Context $context
|
2018-01-21 16:22:04 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-21 19:38:51 +01:00
|
|
|
public function analyzeFile($file_path, \Psalm\Context $context)
|
2018-01-21 16:22:04 +01:00
|
|
|
{
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase = $this->project_checker->getCodebase();
|
2018-02-04 00:52:35 +01:00
|
|
|
$codebase->addFilesToAnalyze([$file_path => $file_path]);
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase->scanFiles();
|
|
|
|
|
|
|
|
$file_checker = new FileChecker(
|
|
|
|
$this->project_checker,
|
|
|
|
$file_path,
|
|
|
|
$codebase->config->shortenFileName($file_path)
|
|
|
|
);
|
2018-01-21 16:22:04 +01:00
|
|
|
$file_checker->analyze($context);
|
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|