WhitespacesAnalyzerTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Tokenizer\Analyzer;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\Analyzer\WhitespacesAnalyzer;
  15. use PhpCsFixer\Tokenizer\Tokens;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Tokenizer\Analyzer\WhitespacesAnalyzer
  20. */
  21. final class WhitespacesAnalyzerTest extends TestCase
  22. {
  23. /**
  24. * @dataProvider provideIndentCases
  25. */
  26. public function testIndent(string $code, string $indent, int $index): void
  27. {
  28. $tokens = Tokens::fromCode($code);
  29. self::assertSame($indent, WhitespacesAnalyzer::detectIndent($tokens, $index));
  30. }
  31. /**
  32. * @return iterable<array{string, string, int}>
  33. */
  34. public static function provideIndentCases(): iterable
  35. {
  36. yield ['<?php function foo() { return true; }', '', 10];
  37. yield [
  38. '<?php
  39. function foo() { return true; }
  40. ',
  41. ' ',
  42. 8,
  43. ];
  44. $code = '<?php
  45. // wrong indent
  46. function foo() { /* foo */ return true; }
  47. ';
  48. $tokens = Tokens::fromCode($code);
  49. foreach (range(4, $tokens->count() - 2) as $index) {
  50. yield [$code, ' ', $index];
  51. }
  52. }
  53. }