Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/lib/PhpCsFixer/Sets/AbstractIbexaRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ public function getRules(): array
'non_printable_character' => true,
'normalize_index_brace' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => true,
'ordered_imports' => [
'imports_order' => ['class', 'function', 'const'],
'sort_algorithm' => 'alpha',
],
'php_unit_construct' => true,
'php_unit_fqcn_annotation' => true,
'php_unit_mock_short_will_return' => false,
Expand Down
122 changes: 122 additions & 0 deletions tests/lib/PhpCsFixer/Rule/OrderedImportsFixerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\CodeStyle\PhpCsFixer\Rule;

use Ibexa\CodeStyle\PhpCsFixer\Sets\Ibexa46RuleSet;
use Ibexa\CodeStyle\PhpCsFixer\Sets\Ibexa50RuleSet;
use Ibexa\CodeStyle\PhpCsFixer\Sets\RuleSetInterface;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Fixer\Import\OrderedImportsFixer;
use PhpCsFixer\Fixer\Whitespace\BlankLineBetweenImportGroupsFixer;
use PhpCsFixer\Tokenizer\Tokens;
use PHPUnit\Framework\TestCase;
use SplFileInfo;

final class OrderedImportsFixerTest extends TestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFixGroupsAndSortsImportsByType(
RuleSetInterface $ruleSet,
string $input,
string $expected
): void {
$orderedImportsRule = $ruleSet->getRules()['ordered_imports'];
self::assertIsArray($orderedImportsRule);

$orderedImportsFixer = new OrderedImportsFixer();
$orderedImportsFixer->configure($orderedImportsRule);

/** @var array<int, FixerInterface> $fixers */
$fixers = [
$orderedImportsFixer,
new BlankLineBetweenImportGroupsFixer(),
];

$tokens = Tokens::fromCode($input);

foreach ($fixers as $fixer) {
if (!$fixer->isCandidate($tokens)) {
continue;
}

$fixer->fix(new SplFileInfo(__FILE__), $tokens);
}

self::assertSame($expected, $tokens->generateCode());
}

/**
* @return iterable<string, array{0: RuleSetInterface, 1: string, 2: string}>
*/
public static function provideFixCases(): iterable
{
$ruleSets = [
'50 ruleset' => new Ibexa50RuleSet(),
'46 ruleset' => new Ibexa46RuleSet(),
];

foreach ($ruleSets as $ruleSetName => $ruleSet) {
yield $ruleSetName . ' function import is moved to its own group after class imports' => [
$ruleSet,
<<<'PHP'
<?php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;
use Symfony\Component\DependencyInjection\Reference;
PHP,
<<<'PHP'
<?php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Reference;

use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;
PHP,
];

yield $ruleSetName . ' const import is moved to its own group after class imports' => [
$ruleSet,
<<<'PHP'
<?php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use const Symfony\Component\DependencyInjection\Loader\Configurator\SOME_CONST;
use Symfony\Component\DependencyInjection\Reference;
PHP,
<<<'PHP'
<?php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Reference;

use const Symfony\Component\DependencyInjection\Loader\Configurator\SOME_CONST;
PHP,
];

yield $ruleSetName . ' mixed import types are sorted alphabetically within separate groups' => [
$ruleSet,
<<<'PHP'
<?php
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;
use Symfony\Component\DependencyInjection\Reference;
use const Symfony\Component\DependencyInjection\Loader\Configurator\SOME_CONST;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
PHP,
<<<'PHP'
<?php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Reference;

use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;

use const Symfony\Component\DependencyInjection\Loader\Configurator\SOME_CONST;
PHP,
];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@
'non_printable_character' => true,
'normalize_index_brace' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => true,
'ordered_imports' => [
'imports_order' => ['class', 'function', 'const'],
'sort_algorithm' => 'alpha',
],
'php_unit_construct' => true,
'php_unit_fqcn_annotation' => true,
'php_unit_mock_short_will_return' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
'no_blank_lines_after_class_opening' => true,
'no_leading_import_slash' => true,
'no_whitespace_in_blank_line' => true,
'ordered_imports' => true,
'ordered_imports' => [
'imports_order' => ['class', 'function', 'const'],
'sort_algorithm' => 'alpha',
],
'return_type_declaration' => true,
'short_scalar_cast' => true,
'single_import_per_statement' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@
'non_printable_character' => true,
'normalize_index_brace' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => true,
'ordered_imports' => [
'imports_order' => ['class', 'function', 'const'],
'sort_algorithm' => 'alpha',
],
'php_unit_construct' => true,
'php_unit_fqcn_annotation' => true,
'php_unit_mock_short_will_return' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
'no_blank_lines_after_class_opening' => true,
'no_leading_import_slash' => true,
'no_whitespace_in_blank_line' => true,
'ordered_imports' => true,
'ordered_imports' => [
'imports_order' => ['class', 'function', 'const'],
'sort_algorithm' => 'alpha',
],
'return_type_declaration' => true,
'short_scalar_cast' => true,
'single_import_per_statement' => true,
Expand Down
Loading