1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 10:17:33 +01:00
psalm/tests/Internal/CliUtilsTest.php

137 lines
4.2 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\Internal;
use PHPUnit\Framework\TestCase;
use Psalm\Internal\CliUtils;
use function realpath;
use const DIRECTORY_SEPARATOR;
class CliUtilsTest extends TestCase
{
2021-12-24 10:59:39 +01:00
/**
* @var array<int, string>
*/
private $argv = [];
protected function setUp(): void
{
global $argv;
$this->argv = $argv;
}
protected function tearDown(): void
{
global $argv;
$argv = $this->argv;
}
2021-12-24 10:59:39 +01:00
/** @return iterable<string,array{list<string>,list<string>}> */
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' => [
['--plugin=vendor/vimeo/psalm/examples/plugins/ClassUnqualifier.php', '--dry-run'],
['/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 10:59:39 +01:00
public function testGetArgumentsWillReturnExpectedValue(array $expected, array $_input): void
{
global $argv;
2021-12-24 10:59:39 +01:00
$argv = $_input;
$result = CliUtils::getArguments();
self::assertEquals($expected, $result);
}
2021-12-24 10:59:39 +01:00
/** @return iterable<string,array{list<string>|null,list<string>,fpaths?:list<string>}> */
public function provideGetPathsToCheck(): iterable
{
$psalm = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'psalm';
$dummyProjectDir = (string)realpath(
__DIR__
. DIRECTORY_SEPARATOR . '..'
. DIRECTORY_SEPARATOR. 'fixtures'
. DIRECTORY_SEPARATOR . 'DummyProject'
);
2021-12-24 10:59:39 +01:00
$currentDir = (string)realpath('.');
yield 'withoutPaths' => [
null,
[$psalm, '--plugin=vendor/vimeo/psalm/examples/plugins/ClassUnqualifier.php', '--dry-run'],
];
yield 'withoutPathsAndArguments' => [
null,
[$psalm],
];
yield 'withPaths' => [
[
$dummyProjectDir . DIRECTORY_SEPARATOR . 'Bar.php',
$dummyProjectDir . DIRECTORY_SEPARATOR . 'Bat.php',
],
[
$psalm,
$dummyProjectDir . DIRECTORY_SEPARATOR . 'Bar.php',
$dummyProjectDir . DIRECTORY_SEPARATOR . 'Bat.php',
],
];
yield 'withPathsAndArgumentsMixed' => [
[
$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',
],
];
yield 'withFpathToCurrentDir' => [
2021-12-24 10:59:39 +01:00
[$currentDir],
[$psalm, '-f', '.'],
['.']
];
yield 'withFpathToProjectDir' => [
[$dummyProjectDir],
[$psalm, '-f', $dummyProjectDir],
[$dummyProjectDir]
];
}
/**
* @dataProvider provideGetPathsToCheck
* @param list<string>|null $expected
2021-12-24 10:59:39 +01:00
* @param list<string> $_input
*/
2021-12-24 10:59:39 +01:00
public function testGetPathsToCheckWillReturnExpectedValue(?array $expected, array $_input, array $fpaths = []): void
{
global $argv;
2021-12-24 10:59:39 +01:00
$argv = $_input;
$result = CliUtils::getPathsToCheck($fpaths);
self::assertEquals($expected, $result);
}
}