SortTest.php in Drupal 10
File
core/tests/Drupal/Tests/PhpCs/SortTest.php
View source
<?php
namespace Drupal\Tests\PhpCs;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
class SortTest extends TestCase {
private $filePath;
protected function setUp() : void {
$this->filePath = __DIR__ . '/../../../../../core/phpcs.xml.dist';
}
public function testFileExists() {
$this
->assertFileExists($this->filePath);
}
public function testSorted() {
$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');
}
}
}
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);
}
}
Classes
Name |
Description |
SortTest |
Tests that phpcs.xml.dist is properly sorted. |