* Dariusz RumiƄski * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author John Paul E. Balandan, CPA * * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\OrderedTypesFixer */ final class OrderedTypesFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases * * @param null|array $config */ public function testFix(string $expected, ?string $input = null, ?array $config = null): void { if (null !== $config) { $this->fixer->configure($config); } $this->doTest($expected, $input); } /** * @return iterable|string)[]|string[]> */ public static function provideFixCases(): iterable { yield 'catch with default, no spaces, with both leading slash' => [ 'foo(); } catch (\LogicException|\RuntimeException $e) { // $e } ', 'foo(); } catch (\RuntimeException|\LogicException $e) { // $e } ', ]; yield 'catch with default, with spaces, with both leading slash' => [ 'foo(); } catch (\LogicException|\RuntimeException $e) { // $e } ', 'foo(); } catch (\RuntimeException | \LogicException $e) { // $e } ', ]; yield 'catch with default, no spaces, with no leading slash' => [ 'save(); } catch (CacheException|SimpleCacheException $e) { // $e } ', 'save(); } catch (SimpleCacheException|CacheException $e) { // $e } ', ]; yield 'catch with default, with spaces, with one leading slash' => [ 'save(); } catch (CacheException|\RuntimeException $e) { // $e } ', 'save(); } catch (\RuntimeException | CacheException $e) { // $e } ', ]; yield 'catch with no sorting' => [ 'foo(); } catch (\RuntimeException|\LogicException $e) { // $e } ', null, ['sort_algorithm' => 'none'], ]; yield 'nothing to fix' => [ 'foo(); } catch (\LogicException $e) { // $e } ', ]; yield 'already fixed' => [ 'foo(); } catch (LogicException|RuntimeException $e) { // $e } ', ]; } /** * @dataProvider provideFixPhp80Cases * * @param null|array $config * * @requires PHP 8.0 */ public function testFixPhp80(string $expected, ?string $input = null, ?array $config = null): void { if (null !== $config) { $this->fixer->configure($config); } $this->doTest($expected, $input); } /** * @return iterable<(null|array|string)[]> */ public static function provideFixPhp80Cases(): iterable { yield 'sort alpha, null none' => [ " 'alpha', 'null_adjustment' => 'none'], ]; yield 'sort alpha, null first' => [ " 'alpha', 'null_adjustment' => 'always_first'], ]; yield 'sort alpha, null last' => [ " 'alpha', 'null_adjustment' => 'always_last'], ]; yield 'sort none, null first' => [ " 'none', 'null_adjustment' => 'always_first'], ]; yield 'sort none, null last' => [ " 'none', 'null_adjustment' => 'always_last'], ]; yield 'sort none, null none' => [ " 'none', 'null_adjustment' => 'none'], ]; } /** * @dataProvider provideFixDefaultCases * * @requires PHP 8.0 */ public function testFixDefault(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<(null|array|string)[]|string[]> */ public static function provideFixDefaultCases(): iterable { yield [ " $number;', ' $number;', ]; yield [ "baz(); } catch (Exception $e) { return $e; } ', ]; } /** * @dataProvider provideFixWithAlphaAlgorithmAndNullAlwaysLastCases * * @requires PHP 8.0 */ public function testFixWithAlphaAlgorithmAndNullAlwaysLast(string $expected, ?string $input = null): void { $this->fixer->configure([ 'sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last', ]); $this->doTest($expected, $input); } /** * @return iterable<(null|string|string[])[]|string[]> */ public static function provideFixWithAlphaAlgorithmAndNullAlwaysLastCases(): iterable { yield [ " $number;', ' $number;', ]; yield [ "baz(); } catch (Exception $e) { return $e; } ', ]; } /** * @dataProvider provideFixWithAlphaAlgorithmOnlyCases * * @requires PHP 8.0 */ public function testFixWithAlphaAlgorithmOnly(string $expected, ?string $input = null): void { $this->fixer->configure([ 'sort_algorithm' => 'alpha', 'null_adjustment' => 'none', ]); $this->doTest($expected, $input); } /** * @return iterable<(null|array|string)[]|string[]> */ public static function provideFixWithAlphaAlgorithmOnlyCases(): iterable { yield [ " $number;', ' $number;', ]; yield [ "baz(); } catch (Exception $e) { return $e; } ', ]; } /** * @dataProvider provideFixWithSandwichedWhitespaceOrCommentInTypeCases * * @requires PHP 8.0 */ public function testFixWithSandwichedWhitespaceOrCommentInType(string $expected, ?string $input = null): void { // The current design of the fixer uses the `TypeAnalysis` class which collects the // types and ignores whitespaces and comments. These ignored tokens are not accounted // during instantiation of the TypeAnalysis class, thus we have no way of recovering // those token information. This test case mainly proves this fact. $this->doTest($expected, $input); } /** * @return iterable<(null|array|string)[]|string[]> */ public static function provideFixWithSandwichedWhitespaceOrCommentInTypeCases(): iterable { yield [ " $config * * @requires PHP 8.1 */ public function testFixPhp81(string $expected, ?string $input = null, ?array $config = null): void { if (null !== $config) { $this->fixer->configure($config); } $this->doTest($expected, $input); } /** * @return iterable<(null|array|string)[]|string[]> */ public static function provideFixPhp81Cases(): iterable { yield [ " 'always_last'], ]; yield [ " 'none'], ]; yield [ " 'none'], ]; } /** * Provisional support for PHP 8.2's Disjunctive Normal Form (DNF) Types. * * @dataProvider provideFixPhp82Cases * * @param null|array $config * * @requires PHP 8.2 */ public function testFixPhp82(string $expected, ?string $input = null, ?array $config = null): void { if (null !== $config) { $this->fixer->configure($config); } $this->doTest($expected, $input); } /** * @return iterable<(null|array|string)[]|string[]> */ public static function provideFixPhp82Cases(): iterable { yield [ " 'always_last'], ]; yield [ " 'none'], ]; yield [ " 'alpha'], ]; yield [ " 'alpha'], ]; yield [ " 'alpha'], ]; yield [ " 'alpha'], ]; } /** * @dataProvider provideFixWithCaseSensitiveCases * * @requires PHP 8.0 */ public function testFixWithCaseSensitive(string $expected, ?string $input = null): void { $this->fixer->configure([ 'case_sensitive' => true, ]); $this->doTest($expected, $input); } /** * @return iterable<(null|array|string)[]|string[]> */ public static function provideFixWithCaseSensitiveCases(): iterable { yield [ "