2021-12-24 09:54:37 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Psalm\Tests\Internal;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psalm\Internal\CliUtils;
|
|
|
|
|
|
|
|
use function realpath;
|
|
|
|
|
2021-12-24 15:55:24 +01:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2021-12-24 09:54:37 +01:00
|
|
|
class CliUtilsTest extends TestCase
|
|
|
|
{
|
2021-12-24 10:59:39 +01:00
|
|
|
/**
|
2022-09-15 19:49:36 +02:00
|
|
|
* @var list<string>
|
2021-12-24 10:59:39 +01:00
|
|
|
*/
|
2023-02-02 17:19:22 +01:00
|
|
|
private array $argv = [];
|
2021-12-24 09:54:37 +01:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
global $argv;
|
2022-09-15 19:49:36 +02:00
|
|
|
$this->argv = $argv;
|
2021-12-24 09:54:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
global $argv;
|
|
|
|
$argv = $this->argv;
|
|
|
|
}
|
|
|
|
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @return iterable<string,array{list<string>,list<string>}> */
|
2021-12-24 09:54:37 +01:00
|
|
|
public function provideGetArguments(): iterable
|
|
|
|
{
|
|
|
|
$psalter = __DIR__ . '/../../psalter';
|
|
|
|
yield 'standardCallWithPsalter' => [
|
|
|
|
['--plugin=vendor/vimeo/psalm/examples/plugins/ClassUnqualifier.php', '--dry-run'],
|
|
|
|
[$psalter, '--plugin=vendor/vimeo/psalm/examples/plugins/ClassUnqualifier.php', '--dry-run'],
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'specialCaseWithBinary' => [
|
|
|
|
['--plugin=vendor/vimeo/psalm/examples/plugins/ClassUnqualifier.php', '--dry-run'],
|
|
|
|
['/bin/true', '--plugin=vendor/vimeo/psalm/examples/plugins/ClassUnqualifier.php', '--dry-run'],
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'specialCaseWhichWouldFail' => [
|
2021-12-24 10:05:52 +01:00
|
|
|
['--plugin=vendor/vimeo/psalm/examples/plugins/ClassUnqualifier.php', '--dry-run'],
|
2021-12-24 09:54:37 +01:00
|
|
|
['/directory-does-not-exist/file-does-not-exist', '--plugin=vendor/vimeo/psalm/examples/plugins/ClassUnqualifier.php', '--dry-run'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideGetArguments
|
|
|
|
* @param list<string> $expected
|
2021-12-24 10:59:39 +01:00
|
|
|
* @param list<string> $_input
|
2021-12-24 09:54:37 +01:00
|
|
|
*/
|
2021-12-24 10:59:39 +01:00
|
|
|
public function testGetArgumentsWillReturnExpectedValue(array $expected, array $_input): void
|
2021-12-24 09:54:37 +01:00
|
|
|
{
|
|
|
|
global $argv;
|
2021-12-24 10:59:39 +01:00
|
|
|
$argv = $_input;
|
2021-12-24 09:54:37 +01:00
|
|
|
$result = CliUtils::getArguments();
|
|
|
|
self::assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @return iterable<string,list{0: list<string>|null, 1: list<string>, 2?: list<string>}> */
|
2021-12-24 09:54:37 +01:00
|
|
|
public function provideGetPathsToCheck(): iterable
|
|
|
|
{
|
2021-12-24 15:55:24 +01:00
|
|
|
$psalm = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'psalm';
|
|
|
|
$dummyProjectDir = (string)realpath(
|
|
|
|
__DIR__
|
|
|
|
. DIRECTORY_SEPARATOR . '..'
|
2023-02-02 17:19:22 +01:00
|
|
|
. DIRECTORY_SEPARATOR . 'fixtures'
|
2022-12-18 17:15:15 +01:00
|
|
|
. DIRECTORY_SEPARATOR . 'DummyProject',
|
2021-12-24 15:55:24 +01:00
|
|
|
);
|
2021-12-24 10:59:39 +01:00
|
|
|
$currentDir = (string)realpath('.');
|
|
|
|
|
2021-12-24 09:54:37 +01:00
|
|
|
yield 'withoutPaths' => [
|
|
|
|
null,
|
|
|
|
[$psalm, '--plugin=vendor/vimeo/psalm/examples/plugins/ClassUnqualifier.php', '--dry-run'],
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'withoutPathsAndArguments' => [
|
|
|
|
null,
|
|
|
|
[$psalm],
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'withPaths' => [
|
2021-12-24 15:55:24 +01:00
|
|
|
[
|
|
|
|
$dummyProjectDir . DIRECTORY_SEPARATOR . 'Bar.php',
|
|
|
|
$dummyProjectDir . DIRECTORY_SEPARATOR . 'Bat.php',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
$psalm,
|
|
|
|
$dummyProjectDir . DIRECTORY_SEPARATOR . 'Bar.php',
|
|
|
|
$dummyProjectDir . DIRECTORY_SEPARATOR . 'Bat.php',
|
|
|
|
],
|
2021-12-24 09:54:37 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
yield 'withPathsAndArgumentsMixed' => [
|
2021-12-24 15:55:24 +01:00
|
|
|
[
|
|
|
|
$dummyProjectDir . DIRECTORY_SEPARATOR . 'Bar.php',
|
|
|
|
$dummyProjectDir . DIRECTORY_SEPARATOR . 'Bat.php',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
$psalm,
|
|
|
|
'--plugin=vendor/vimeo/psalm/examples/plugins/ClassUnqualifier.php',
|
|
|
|
$dummyProjectDir . DIRECTORY_SEPARATOR . 'Bar.php',
|
|
|
|
$dummyProjectDir . DIRECTORY_SEPARATOR . 'Bat.php',
|
|
|
|
],
|
2021-12-24 09:54:37 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
yield 'withFpathToCurrentDir' => [
|
2021-12-24 10:59:39 +01:00
|
|
|
[$currentDir],
|
2021-12-24 09:54:37 +01:00
|
|
|
[$psalm, '-f', '.'],
|
2022-12-18 17:15:15 +01:00
|
|
|
['.'],
|
2021-12-24 09:54:37 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
yield 'withFpathToProjectDir' => [
|
|
|
|
[$dummyProjectDir],
|
|
|
|
[$psalm, '-f', $dummyProjectDir],
|
2022-12-18 17:15:15 +01:00
|
|
|
[$dummyProjectDir],
|
2021-12-24 09:54:37 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideGetPathsToCheck
|
|
|
|
* @param list<string>|null $expected
|
2021-12-24 10:59:39 +01:00
|
|
|
* @param list<string> $_input
|
2021-12-24 09:54:37 +01:00
|
|
|
*/
|
2021-12-24 10:59:39 +01:00
|
|
|
public function testGetPathsToCheckWillReturnExpectedValue(?array $expected, array $_input, array $fpaths = []): void
|
2021-12-24 09:54:37 +01:00
|
|
|
{
|
|
|
|
global $argv;
|
2021-12-24 10:59:39 +01:00
|
|
|
$argv = $_input;
|
2021-12-24 09:54:37 +01:00
|
|
|
$result = CliUtils::getPathsToCheck($fpaths);
|
|
|
|
self::assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
}
|