123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests;
- use PhpCsFixer\Fixer\FixerInterface;
- use PhpCsFixer\Tokenizer\Token;
- use PhpCsFixer\Utils;
- use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
- /**
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- * @author Graham Campbell <hello@gjcampbell.co.uk>
- * @author Odín del Río <odin.drp@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Utils
- */
- final class UtilsTest extends TestCase
- {
- use ExpectDeprecationTrait;
- /**
- * @var null|false|string
- */
- private $originalValueOfFutureMode;
- protected function setUp(): void
- {
- $this->originalValueOfFutureMode = getenv('PHP_CS_FIXER_FUTURE_MODE');
- }
- protected function tearDown(): void
- {
- putenv("PHP_CS_FIXER_FUTURE_MODE={$this->originalValueOfFutureMode}");
- }
- /**
- * @param string $expected Camel case string
- *
- * @dataProvider provideCamelCaseToUnderscoreCases
- */
- public function testCamelCaseToUnderscore(string $expected, string $input = null): void
- {
- if (null !== $input) {
- self::assertSame($expected, Utils::camelCaseToUnderscore($input));
- }
- self::assertSame($expected, Utils::camelCaseToUnderscore($expected));
- }
- public static function provideCamelCaseToUnderscoreCases(): iterable
- {
- yield [
- 'dollar_close_curly_braces',
- 'DollarCloseCurlyBraces',
- ];
- yield [
- 'utf8_encoder_fixer',
- 'utf8EncoderFixer',
- ];
- yield [
- 'terminated_with_number10',
- 'TerminatedWithNumber10',
- ];
- yield [
- 'utf8_encoder_fixer',
- ];
- yield [
- 'a',
- 'A',
- ];
- yield [
- 'aa',
- 'AA',
- ];
- yield [
- 'foo',
- 'FOO',
- ];
- yield [
- 'foo_bar_baz',
- 'FooBarBAZ',
- ];
- yield [
- 'foo_bar_baz',
- 'FooBARBaz',
- ];
- yield [
- 'foo_bar_baz',
- 'FOOBarBaz',
- ];
- yield [
- 'mr_t',
- 'MrT',
- ];
- yield [
- 'voyage_éclair',
- 'VoyageÉclair',
- ];
- }
- /**
- * @param array{int, string}|string $input token prototype
- *
- * @dataProvider provideCalculateTrailingWhitespaceIndentCases
- */
- public function testCalculateTrailingWhitespaceIndent(string $spaces, $input): void
- {
- $token = new Token($input);
- self::assertSame($spaces, Utils::calculateTrailingWhitespaceIndent($token));
- }
- public static function provideCalculateTrailingWhitespaceIndentCases(): iterable
- {
- yield [' ', [T_WHITESPACE, "\n\n "]];
- yield [' ', [T_WHITESPACE, "\r\n\r\r\r "]];
- yield ["\t", [T_WHITESPACE, "\r\n\t"]];
- yield ['', [T_WHITESPACE, "\t\n\r"]];
- yield ['', [T_WHITESPACE, "\n"]];
- yield ['', ''];
- }
- public function testCalculateTrailingWhitespaceIndentFail(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('The given token must be whitespace, got "T_STRING".');
- $token = new Token([T_STRING, 'foo']);
- Utils::calculateTrailingWhitespaceIndent($token);
- }
- /**
- * @param list<mixed> $expected
- * @param list<mixed> $elements
- *
- * @dataProvider provideStableSortCases
- */
- public function testStableSort(
- array $expected,
- array $elements,
- callable $getComparableValueCallback,
- callable $compareValuesCallback
- ): void {
- self::assertSame(
- $expected,
- Utils::stableSort($elements, $getComparableValueCallback, $compareValuesCallback)
- );
- }
- public static function provideStableSortCases(): iterable
- {
- yield [
- ['a', 'b', 'c', 'd', 'e'],
- ['b', 'd', 'e', 'a', 'c'],
- static fn ($element) => $element,
- 'strcmp',
- ];
- yield [
- ['b', 'd', 'e', 'a', 'c'],
- ['b', 'd', 'e', 'a', 'c'],
- static fn (): string => 'foo',
- 'strcmp',
- ];
- yield [
- ['b', 'd', 'e', 'a', 'c'],
- ['b', 'd', 'e', 'a', 'c'],
- static fn ($element) => $element,
- static fn (): int => 0,
- ];
- yield [
- ['bar1', 'baz1', 'foo1', 'bar2', 'baz2', 'foo2'],
- ['foo1', 'foo2', 'bar1', 'bar2', 'baz1', 'baz2'],
- static fn ($element) => preg_replace('/([a-z]+)(\d+)/', '$2$1', $element),
- 'strcmp',
- ];
- }
- public function testSortFixers(): void
- {
- $fixers = [
- $this->createFixerDouble('f1', 0),
- $this->createFixerDouble('f2', -10),
- $this->createFixerDouble('f3', 10),
- $this->createFixerDouble('f4', -10),
- ];
- self::assertSame(
- [
- $fixers[2],
- $fixers[0],
- $fixers[1],
- $fixers[3],
- ],
- Utils::sortFixers($fixers)
- );
- }
- public function testNaturalLanguageJoinThrowsInvalidArgumentExceptionForEmptyArray(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Array of names cannot be empty.');
- Utils::naturalLanguageJoin([]);
- }
- public function testNaturalLanguageJoinThrowsInvalidArgumentExceptionForMoreThanOneCharWrapper(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Wrapper should be a single-char string or empty.');
- Utils::naturalLanguageJoin(['a', 'b'], 'foo');
- }
- /**
- * @dataProvider provideNaturalLanguageJoinCases
- *
- * @param list<string> $names
- */
- public function testNaturalLanguageJoin(string $joined, array $names, string $wrapper = '"'): void
- {
- self::assertSame($joined, Utils::naturalLanguageJoin($names, $wrapper));
- }
- /**
- * @return iterable<array<null|array<string>|string>>
- */
- public static function provideNaturalLanguageJoinCases(): iterable
- {
- yield [
- '"a"',
- ['a'],
- ];
- yield [
- '"a" and "b"',
- ['a', 'b'],
- ];
- yield [
- '"a", "b" and "c"',
- ['a', 'b', 'c'],
- ];
- yield [
- '\'a\'',
- ['a'],
- '\'',
- ];
- yield [
- '\'a\' and \'b\'',
- ['a', 'b'],
- '\'',
- ];
- yield [
- '\'a\', \'b\' and \'c\'',
- ['a', 'b', 'c'],
- '\'',
- ];
- yield [
- '?a?',
- ['a'],
- '?',
- ];
- yield [
- '?a? and ?b?',
- ['a', 'b'],
- '?',
- ];
- yield [
- '?a?, ?b? and ?c?',
- ['a', 'b', 'c'],
- '?',
- ];
- yield [
- 'a',
- ['a'],
- '',
- ];
- yield [
- 'a and b',
- ['a', 'b'],
- '',
- ];
- yield [
- 'a, b and c',
- ['a', 'b', 'c'],
- '',
- ];
- }
- public function testNaturalLanguageJoinWithBackticksThrowsInvalidArgumentExceptionForEmptyArray(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- Utils::naturalLanguageJoinWithBackticks([]);
- }
- /**
- * @param list<string> $names
- *
- * @dataProvider provideNaturalLanguageJoinWithBackticksCases
- */
- public function testNaturalLanguageJoinWithBackticks(string $joined, array $names): void
- {
- self::assertSame($joined, Utils::naturalLanguageJoinWithBackticks($names));
- }
- public static function provideNaturalLanguageJoinWithBackticksCases(): iterable
- {
- yield [
- '`a`',
- ['a'],
- ];
- yield [
- '`a` and `b`',
- ['a', 'b'],
- ];
- yield [
- '`a`, `b` and `c`',
- ['a', 'b', 'c'],
- ];
- }
- /**
- * @group legacy
- */
- public function testTriggerDeprecationWhenFutureModeIsOff(): void
- {
- putenv('PHP_CS_FIXER_FUTURE_MODE=0');
- $message = __METHOD__.'::The message';
- $this->expectDeprecation($message);
- Utils::triggerDeprecation(new \DomainException($message));
- $triggered = Utils::getTriggeredDeprecations();
- self::assertContains($message, $triggered);
- }
- public function testTriggerDeprecationWhenFutureModeIsOn(): void
- {
- putenv('PHP_CS_FIXER_FUTURE_MODE=1');
- $message = __METHOD__.'::The message';
- $exception = new \DomainException($message);
- $futureModeException = null;
- try {
- Utils::triggerDeprecation($exception);
- } catch (\Exception $futureModeException) {
- }
- self::assertInstanceOf(\RuntimeException::class, $futureModeException);
- self::assertSame($exception, $futureModeException->getPrevious());
- $triggered = Utils::getTriggeredDeprecations();
- self::assertNotContains($message, $triggered);
- }
- /**
- * @param mixed $input
- *
- * @dataProvider provideToStringCases
- */
- public function testToString(string $expected, $input): void
- {
- self::assertSame($expected, Utils::toString($input));
- }
- public static function provideToStringCases(): iterable
- {
- yield ["['a' => 3, 'b' => 'c']", ['a' => 3, 'b' => 'c']];
- yield ['[[1], [2]]', [[1], [2]]];
- yield ['[0 => [1], \'a\' => [2]]', [[1], 'a' => [2]]];
- yield ['[1, 2, \'foo\', null]', [1, 2, 'foo', null]];
- yield ['[1, 2]', [1, 2]];
- yield ['[]', []];
- yield ['1.5', 1.5];
- yield ['false', false];
- yield ['true', true];
- yield ['1', 1];
- yield ["'foo'", 'foo'];
- }
- private function createFixerDouble(string $name, int $priority): FixerInterface
- {
- $fixer = $this->prophesize(FixerInterface::class);
- $fixer->getName()->willReturn($name);
- $fixer->getPriority()->willReturn($priority);
- return $fixer->reveal();
- }
- }
|