2021-11-12 16:59:20 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2021-11-12 16:59:20 +01:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Exception\CodeException;
|
|
|
|
|
|
|
|
class UnresolvableIncludeTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider providerUnresolvableInclude
|
|
|
|
*/
|
|
|
|
public function testShouldThrowUnresolvableInclude(string $phpCode, int $expectedExceptionLine): void
|
|
|
|
{
|
|
|
|
$this->addFile('somefile.php', $phpCode);
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->expectException(CodeException::class);
|
|
|
|
$this->expectExceptionMessage("UnresolvableInclude - somefile.php:$expectedExceptionLine:");
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return array<string,array{code:string,expectedExceptionLine:int}>
|
2021-11-12 16:59:20 +01:00
|
|
|
*/
|
|
|
|
public function providerUnresolvableInclude(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'basic' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-11-12 16:59:20 +01:00
|
|
|
function requireFile(string $s) : void {
|
|
|
|
require_once($s);
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'expectedExceptionLine' => 3,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerNoUnresolvableInclude
|
|
|
|
*/
|
|
|
|
public function testShouldNotThrowUnresolvableInclude(string $phpCode): void
|
|
|
|
{
|
|
|
|
$this->addFile('somefile.php', $phpCode);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return array<string,array{code:string}>
|
2021-11-12 16:59:20 +01:00
|
|
|
*/
|
|
|
|
public function providerNoUnresolvableInclude(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'github_issue_908_file_exists' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-11-12 16:59:20 +01:00
|
|
|
function requireFile(string $s) : void {
|
|
|
|
if (file_exists($s)) {
|
|
|
|
require_once($s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'
|
|
|
|
],
|
|
|
|
'github_issue_4788_is_file' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-11-12 16:59:20 +01:00
|
|
|
function requireFile(string $s) : void {
|
|
|
|
if (is_file($s)) {
|
|
|
|
require_once($s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|