BlocksAnalyzerTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\BlocksAnalyzer;
  15. use PhpCsFixer\Tokenizer\Tokens;
  16. /**
  17. * @author Kuba Werłos <werlos@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Tokenizer\Analyzer\BlocksAnalyzer
  22. */
  23. final class BlocksAnalyzerTest extends TestCase
  24. {
  25. /**
  26. * @dataProvider provideBlocksCases
  27. */
  28. public function testBlocks(string $code, int $openIndex, int $closeIndex): void
  29. {
  30. $tokens = Tokens::fromCode($code);
  31. $analyzer = new BlocksAnalyzer();
  32. self::assertTrue($analyzer->isBlock($tokens, $openIndex, $closeIndex));
  33. }
  34. /**
  35. * @return iterable<array{string, int, int}>
  36. */
  37. public static function provideBlocksCases(): iterable
  38. {
  39. yield ['<?php foo(1);', 2, 4];
  40. yield ['<?php foo((1));', 3, 5];
  41. yield ['<?php foo((1));', 2, 6];
  42. yield ['<?php foo(1, 2, 3);', 2, 10];
  43. yield ['<?php foo(1, bar(2, 3), 4);', 2, 16];
  44. yield ['<?php $foo["bar"];', 2, 4];
  45. yield ['<?php [1, 2, 3];', 1, 9];
  46. yield ['<?php $foo = function ($x) { return $x + 10; };', 7, 9];
  47. yield ['<?php $foo = function ($x) { return $x + 10; };', 11, 22];
  48. yield ['<?php list($a, $b, $c) = [1, 2, 3];', 2, 10];
  49. yield ['<?php list($a, $b, $c) = [1, 2, 3];', 14, 22];
  50. yield ['<?php list($a, $b, $c) = array(1, 2, 3);', 15, 23];
  51. yield ['<?php $fn = fn($x) => $x + 10;', 6, 8];
  52. }
  53. /**
  54. * @dataProvider provideNonBlocksCases
  55. */
  56. public function testNonBlocks(string $code, int $openIndex, int $closeIndex): void
  57. {
  58. $tokens = Tokens::fromCode($code);
  59. $analyzer = new BlocksAnalyzer();
  60. self::assertFalse($analyzer->isBlock($tokens, $openIndex, $closeIndex));
  61. }
  62. /**
  63. * @return iterable<array{string, int, int}>
  64. */
  65. public static function provideNonBlocksCases(): iterable
  66. {
  67. yield ['<?php foo(1);', 1, 4];
  68. yield ['<?php foo(1);', 3, 4];
  69. yield ['<?php foo(1);', 2, 3];
  70. yield ['<?php foo((1));', 2, 5];
  71. yield ['<?php foo((1));', 3, 6];
  72. }
  73. /**
  74. * @dataProvider provideInvalidIndexCases
  75. */
  76. public function testInvalidIndex(string $code, int $openIndex, int $closeIndex): void
  77. {
  78. $tokens = Tokens::fromCode($code);
  79. $analyzer = new BlocksAnalyzer();
  80. $this->expectException(\InvalidArgumentException::class);
  81. $analyzer->isBlock($tokens, $openIndex, $closeIndex);
  82. }
  83. /**
  84. * @return iterable<array{string, int, int}>
  85. */
  86. public static function provideInvalidIndexCases(): iterable
  87. {
  88. yield ['<?php foo(1);', 1_000, 4];
  89. yield ['<?php foo(1);', 2, 1_000];
  90. }
  91. }