class SortTest

Same name in this branch
  1. 10 core/modules/jsonapi/tests/src/Unit/Query/SortTest.php \Drupal\Tests\jsonapi\Unit\Query\SortTest
  2. 10 core/modules/views/tests/src/Kernel/Handler/SortTest.php \Drupal\Tests\views\Kernel\Handler\SortTest
  3. 10 core/tests/Drupal/Tests/CSpell/SortTest.php \Drupal\Tests\CSpell\SortTest
Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Unit/Query/SortTest.php \Drupal\Tests\jsonapi\Unit\Query\SortTest
  2. 9 core/modules/views/tests/src/Kernel/Handler/SortTest.php \Drupal\Tests\views\Kernel\Handler\SortTest
  3. 9 core/tests/Drupal/Tests/PhpCs/SortTest.php \Drupal\Tests\PhpCs\SortTest
  4. 8.9.x core/modules/jsonapi/tests/src/Unit/Query/SortTest.php \Drupal\Tests\jsonapi\Unit\Query\SortTest
  5. 8.9.x core/modules/views/tests/src/Kernel/Handler/SortTest.php \Drupal\Tests\views\Kernel\Handler\SortTest
  6. 11.x core/modules/jsonapi/tests/src/Unit/Query/SortTest.php \Drupal\Tests\jsonapi\Unit\Query\SortTest
  7. 11.x core/modules/views/tests/src/Kernel/Handler/SortTest.php \Drupal\Tests\views\Kernel\Handler\SortTest
  8. 11.x core/tests/Drupal/Tests/PhpCs/SortTest.php \Drupal\Tests\PhpCs\SortTest
  9. 11.x core/tests/Drupal/Tests/CSpell/SortTest.php \Drupal\Tests\CSpell\SortTest

Tests that phpcs.xml.dist is properly sorted.

@group phpcs

Hierarchy

  • class \Drupal\Tests\PhpCs\SortTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of SortTest

File

core/tests/Drupal/Tests/PhpCs/SortTest.php, line 15

Namespace

Drupal\Tests\PhpCs
View source
class SortTest extends TestCase {
    
    /**
     * The path of phpcs.xml.dist file.
     *
     * @var string
     */
    private $filePath;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        $this->filePath = __DIR__ . '/../../../../../core/phpcs.xml.dist';
    }
    
    /**
     * Tests that the phpcs.xml.dist file exists.
     */
    public function testFileExists() : void {
        $this->assertFileExists($this->filePath);
    }
    
    /**
     * Tests that the phpcs.xml.dist file is properly sorted.
     */
    public function testSorted() : void {
        $content = file_get_contents($this->filePath);
        $xml_encoder = new XmlEncoder();
        $xml_encoded = $xml_encoder->decode($content, 'xml');
        $this->assertIsArray($xml_encoded);
        $top_level_keys = array_keys($xml_encoded);
        $this->assertSorted($top_level_keys);
        $this->assertArrayHasKey('file', $xml_encoded);
        $files = $xml_encoded['file'];
        $this->assertSorted($files);
        $this->assertArrayHasKey('exclude-pattern', $xml_encoded);
        $excluded_patterns = $xml_encoded['exclude-pattern'];
        $this->assertSorted($excluded_patterns);
        $this->assertArrayHasKey('rule', $xml_encoded);
        $rules = $xml_encoded['rule'];
        $this->assertSorted($rules, '@ref');
        foreach ($rules as $item) {
            if (array_key_exists('exclude', $item)) {
                $excluded = $item['exclude'];
                $excluded = array_filter($excluded, static function ($item) {
                    return is_array($item) && array_key_exists('@name', $item);
                });
                $this->assertSorted($excluded, '@name');
            }
        }
    }
    
    /**
     * A helper method to assert that an input array is sorted.
     *
     * Compared by values, if the $column is not null, the column of the value is
     * used for comparing.
     *
     * @param array $input
     *   The input array.
     * @param null|string $column
     *   The column of the value or NULL.
     */
    private function assertSorted(array $input, ?string $column = NULL) {
        $input_sorted = $input;
        if ($column === NULL) {
            usort($input_sorted, static function ($a, $b) {
                return strcmp($a, $b);
            });
        }
        else {
            usort($input_sorted, static function ($a, $b) use ($column) {
                return strcmp($a[$column], $b[$column]);
            });
        }
        $this->assertEquals($input, $input_sorted);
    }

}

Members

Title Sort descending Modifiers Object type Summary
SortTest::$filePath private property The path of phpcs.xml.dist file.
SortTest::assertSorted private function A helper method to assert that an input array is sorted.
SortTest::setUp protected function
SortTest::testFileExists public function Tests that the phpcs.xml.dist file exists.
SortTest::testSorted public function Tests that the phpcs.xml.dist file is properly sorted.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.