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;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2019-03-23 19:27:54 +01:00
|
|
|
use Psalm\Internal\Provider\Providers;
|
2018-11-12 16:57:05 +01:00
|
|
|
use Psalm\Tests\Internal\Provider;
|
2018-07-13 23:44:50 +02:00
|
|
|
use RuntimeException;
|
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-11-06 03:57:36 +01:00
|
|
|
/** @var ProjectAnalyzer */
|
2018-11-11 18:01:14 +01:00
|
|
|
protected $project_analyzer;
|
2017-04-25 05:45:02 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
/** @var Provider\FakeFileProvider */
|
|
|
|
protected $file_provider;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-05-17 00:36:36 +02:00
|
|
|
public static function setUpBeforeClass() : void
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
2018-01-09 15:21:54 +01:00
|
|
|
ini_set('memory_limit', '-1');
|
2018-04-25 03:02:07 +02:00
|
|
|
|
|
|
|
if (!defined('PSALM_VERSION')) {
|
|
|
|
define('PSALM_VERSION', '2.0.0');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined('PHP_PARSER_VERSION')) {
|
|
|
|
define('PHP_PARSER_VERSION', '4.0.0');
|
|
|
|
}
|
2018-04-24 13:19:25 +02:00
|
|
|
|
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
|
|
|
|
*/
|
2019-05-17 00:36:36 +02:00
|
|
|
public function setUp() : void
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
FileAnalyzer::clearCache();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-11-12 16:57:05 +01:00
|
|
|
$this->file_provider = new \Psalm\Tests\Internal\Provider\FakeFileProvider();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$config = new TestConfig();
|
2018-09-28 22:18:45 +02:00
|
|
|
|
|
|
|
$providers = new Providers(
|
|
|
|
$this->file_provider,
|
|
|
|
new Provider\FakeParserCacheProvider()
|
|
|
|
);
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
2018-01-21 19:38:51 +01:00
|
|
|
$config,
|
2018-09-28 22:18:45 +02:00
|
|
|
$providers,
|
2018-02-04 00:52:35 +01:00
|
|
|
false,
|
|
|
|
true,
|
2018-11-06 03:57:36 +01:00
|
|
|
ProjectAnalyzer::TYPE_CONSOLE,
|
2018-02-04 00:52:35 +01:00
|
|
|
1,
|
2019-05-27 19:07:02 +02:00
|
|
|
false
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
2019-02-07 20:08:35 +01:00
|
|
|
|
|
|
|
$this->project_analyzer->setPhpVersion('7.3');
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-05-17 00:36:36 +02:00
|
|
|
public function tearDown() : void
|
|
|
|
{
|
|
|
|
FileAnalyzer::clearCache();
|
|
|
|
}
|
|
|
|
|
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);
|
2019-01-12 16:52:23 +01:00
|
|
|
$this->project_analyzer->getCodebase()->scanner->addFileToShallowScan($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-11-11 18:01:14 +01:00
|
|
|
$codebase = $this->project_analyzer->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();
|
|
|
|
|
2018-06-30 21:29:37 +02:00
|
|
|
$codebase->config->visitStubFiles($codebase);
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$file_analyzer = new FileAnalyzer(
|
|
|
|
$this->project_analyzer,
|
2018-01-21 19:38:51 +01:00
|
|
|
$file_path,
|
|
|
|
$codebase->config->shortenFileName($file_path)
|
|
|
|
);
|
2018-11-11 18:01:14 +01:00
|
|
|
$file_analyzer->analyze($context);
|
2018-01-21 16:22:04 +01:00
|
|
|
}
|
2018-07-23 01:29:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $withDataSet
|
2019-03-23 19:27:54 +01:00
|
|
|
*
|
2018-07-23 01:29:04 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getTestName($withDataSet = true)
|
2018-07-13 23:44:50 +02:00
|
|
|
{
|
|
|
|
$name = parent::getName($withDataSet);
|
|
|
|
/** @psalm-suppress DocblockTypeContradiction PHPUnit 7 introduced nullable name */
|
|
|
|
if (null === $name) {
|
|
|
|
throw new RuntimeException('anonymous test - shouldn\'t happen');
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
|
2018-07-13 23:44:50 +02:00
|
|
|
return $name;
|
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|