|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the Config::prepareConfigDataForDisplay() method. |
| 4 | + * |
| 5 | + * @copyright 2025 PHPCSStandards and contributors |
| 6 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 7 | + */ |
| 8 | + |
| 9 | +namespace PHP_CodeSniffer\Tests\Core\Config; |
| 10 | + |
| 11 | +use PHP_CodeSniffer\Tests\ConfigDouble; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * Tests for the Config::prepareConfigDataForDisplay() method. |
| 16 | + * |
| 17 | + * @covers \PHP_CodeSniffer\Config::prepareConfigDataForDisplay |
| 18 | + * @covers \PHP_CodeSniffer\Config::printConfigData |
| 19 | + */ |
| 20 | +final class PrepareConfigDataForDisplayTest extends TestCase |
| 21 | +{ |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + * Verify that the config data is prepared for display correctly. |
| 26 | + * |
| 27 | + * - The method always returns a string. |
| 28 | + * - Config data is sorted based on the keys. |
| 29 | + * - The display formatting takes the length of the keys into account correctly. |
| 30 | + * |
| 31 | + * @param array<string, string> $data The Config data. |
| 32 | + * @param string $expected Expected formatted string. |
| 33 | + * |
| 34 | + * @dataProvider dataPrepareConfigDataForDisplay |
| 35 | + * |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public function testPrepareConfigDataForDisplay($data, $expected) |
| 39 | + { |
| 40 | + $config = new ConfigDouble(); |
| 41 | + $actual = $config->prepareConfigDataForDisplay($data); |
| 42 | + |
| 43 | + $this->assertSame($expected, $actual); |
| 44 | + |
| 45 | + }//end testPrepareConfigDataForDisplay() |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * Data provider. |
| 50 | + * |
| 51 | + * @return array<string, array<string, array<string, string>|string>> |
| 52 | + */ |
| 53 | + public static function dataPrepareConfigDataForDisplay() |
| 54 | + { |
| 55 | + // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- Readability is more important. |
| 56 | + return [ |
| 57 | + 'No config data' => [ |
| 58 | + 'data' => [], |
| 59 | + 'expected' => '', |
| 60 | + ], |
| 61 | + 'Only one config item' => [ |
| 62 | + 'data' => [ |
| 63 | + 'colors' => '1', |
| 64 | + ], |
| 65 | + 'expected' => 'colors: 1'.PHP_EOL, |
| 66 | + ], |
| 67 | + 'Data with keys of varying length in non-alphabetically order' => [ |
| 68 | + 'data' => [ |
| 69 | + 'tab_width' => '2', |
| 70 | + 'encoding' => 'utf-8', |
| 71 | + 'ignore_errors_on_exit' => '1', |
| 72 | + 'default_standard' => 'PSR12', |
| 73 | + ], |
| 74 | + 'expected' => 'default_standard: PSR12'.PHP_EOL |
| 75 | + .'encoding: utf-8'.PHP_EOL |
| 76 | + .'ignore_errors_on_exit: 1'.PHP_EOL |
| 77 | + .'tab_width: 2'.PHP_EOL, |
| 78 | + ], |
| 79 | + 'Invalid config data: no keys' => [ |
| 80 | + 'data' => [ |
| 81 | + '1', |
| 82 | + '2', |
| 83 | + ], |
| 84 | + 'expected' => '0: 1'.PHP_EOL.'1: 2'.PHP_EOL, |
| 85 | + ], |
| 86 | + 'Unexpected, but handled, data with non-string scalar values' => [ |
| 87 | + 'data' => [ |
| 88 | + 'encoding' => 'utf-8', |
| 89 | + 'ignore_errors_on_exit' => true, |
| 90 | + 'tab_width' => 2, |
| 91 | + ], |
| 92 | + 'expected' => 'encoding: utf-8'.PHP_EOL |
| 93 | + .'ignore_errors_on_exit: 1'.PHP_EOL |
| 94 | + .'tab_width: 2'.PHP_EOL, |
| 95 | + ], |
| 96 | + ]; |
| 97 | + // phpcs:enable |
| 98 | + |
| 99 | + }//end dataPrepareConfigDataForDisplay() |
| 100 | + |
| 101 | + |
| 102 | + /** |
| 103 | + * Perfunctory test for the deprecated Config::printConfigData() method. |
| 104 | + * |
| 105 | + * @return void |
| 106 | + */ |
| 107 | + public function testPrintConfigData() |
| 108 | + { |
| 109 | + $this->expectOutputString(''); |
| 110 | + |
| 111 | + $config = new ConfigDouble(); |
| 112 | + $config->printConfigData([]); |
| 113 | + |
| 114 | + }//end testPrintConfigData() |
| 115 | + |
| 116 | + |
| 117 | +}//end class |
0 commit comments