Skip to content

Commit ce9a08d

Browse files
committed
Merge branch 'feature/config-add-initial-tests'
2 parents 66052c9 + 193f9a5 commit ce9a08d

File tree

2 files changed

+309
-0
lines changed

2 files changed

+309
-0
lines changed

package.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
134134
</dir>
135135
<file baseinstalldir="" name="DetermineLoadedClassTest.php" role="test" />
136136
</dir>
137+
<dir name="Config">
138+
<file baseinstalldir="" name="ReportWidthTest.php" role="test" />
139+
</dir>
137140
<dir name="File">
138141
<file baseinstalldir="" name="FindEndOfStatementTest.inc" role="test" />
139142
<file baseinstalldir="" name="FindEndOfStatementTest.php" role="test" />
@@ -2143,6 +2146,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
21432146
<install as="CodeSniffer/Core/Autoloader/TestFiles/B.inc" name="tests/Core/Autoloader/TestFiles/B.inc" />
21442147
<install as="CodeSniffer/Core/Autoloader/TestFiles/C.inc" name="tests/Core/Autoloader/TestFiles/C.inc" />
21452148
<install as="CodeSniffer/Core/Autoloader/TestFiles/Sub/C.inc" name="tests/Core/Autoloader/TestFiles/Sub/C.inc" />
2149+
<install as="CodeSniffer/Core/Config/ReportWidthTest.php" name="tests/Core/Config/ReportWidthTest.php" />
21462150
<install as="CodeSniffer/Core/File/FindEndOfStatementTest.php" name="tests/Core/File/FindEndOfStatementTest.php" />
21472151
<install as="CodeSniffer/Core/File/FindEndOfStatementTest.inc" name="tests/Core/File/FindEndOfStatementTest.inc" />
21482152
<install as="CodeSniffer/Core/File/FindExtendedClassNameTest.php" name="tests/Core/File/FindExtendedClassNameTest.php" />
@@ -2264,6 +2268,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
22642268
<install as="CodeSniffer/Core/Autoloader/TestFiles/B.inc" name="tests/Core/Autoloader/TestFiles/B.inc" />
22652269
<install as="CodeSniffer/Core/Autoloader/TestFiles/C.inc" name="tests/Core/Autoloader/TestFiles/C.inc" />
22662270
<install as="CodeSniffer/Core/Autoloader/TestFiles/Sub/C.inc" name="tests/Core/Autoloader/TestFiles/Sub/C.inc" />
2271+
<install as="CodeSniffer/Core/Config/ReportWidthTest.php" name="tests/Core/Config/ReportWidthTest.php" />
22672272
<install as="CodeSniffer/Core/File/FindEndOfStatementTest.php" name="tests/Core/File/FindEndOfStatementTest.php" />
22682273
<install as="CodeSniffer/Core/File/FindEndOfStatementTest.inc" name="tests/Core/File/FindEndOfStatementTest.inc" />
22692274
<install as="CodeSniffer/Core/File/FindExtendedClassNameTest.php" name="tests/Core/File/FindExtendedClassNameTest.php" />

tests/Core/Config/ReportWidthTest.php

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Config reportWidth value.
4+
*
5+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
6+
* @copyright 2006-2023 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Config;
11+
12+
use PHP_CodeSniffer\Config;
13+
use PHPUnit\Framework\TestCase;
14+
use ReflectionProperty;
15+
16+
class ReportWidthTest extends TestCase
17+
{
18+
19+
20+
/**
21+
* Set static properties in the Config class to prevent tests influencing each other.
22+
*
23+
* @before
24+
*
25+
* @return void
26+
*/
27+
public static function cleanConfig()
28+
{
29+
// Set to the property's default value to clear out potentially set values from other tests.
30+
self::setStaticProperty('executablePaths', []);
31+
32+
// Set to a usable value to circumvent Config trying to find a phpcs.xml config file.
33+
self::setStaticProperty('overriddenDefaults', ['standards' => ['PSR1']]);
34+
35+
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
36+
// from being read and influencing the tests.
37+
self::setStaticProperty('configData', []);
38+
self::setStaticProperty('configDataFile', '');
39+
40+
}//end cleanConfig()
41+
42+
43+
/**
44+
* Clean up after each finished test.
45+
*
46+
* @after
47+
*
48+
* @return void
49+
*/
50+
public function resetConfig()
51+
{
52+
$_SERVER['argv'] = [];
53+
54+
}//end resetConfig()
55+
56+
57+
/**
58+
* Reset the static properties in the Config class to their true defaults to prevent this class
59+
* from unfluencing other tests.
60+
*
61+
* @afterClass
62+
*
63+
* @return void
64+
*/
65+
public static function resetConfigToDefaults()
66+
{
67+
self::setStaticProperty('overriddenDefaults', []);
68+
self::setStaticProperty('executablePaths', []);
69+
self::setStaticProperty('configData', null);
70+
self::setStaticProperty('configDataFile', null);
71+
$_SERVER['argv'] = [];
72+
73+
}//end resetConfigToDefaults()
74+
75+
76+
/**
77+
* Test that report width without overrules will always be set to a non-0 positive integer.
78+
*
79+
* @return void
80+
*/
81+
public function testReportWidthDefault()
82+
{
83+
$config = new Config();
84+
85+
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
86+
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
87+
$this->assertGreaterThan(0, $config->reportWidth, 'Report width is not greater than 0');
88+
89+
}//end testReportWidthDefault()
90+
91+
92+
/**
93+
* Test that the report width will be set to a non-0 positive integer when not found in the CodeSniffer.conf file.
94+
*
95+
* @return void
96+
*/
97+
public function testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
98+
{
99+
$phpCodeSnifferConfig = [
100+
'default_standard' => 'PSR2',
101+
'show_warnings' => '0',
102+
];
103+
104+
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
105+
106+
$config = new Config();
107+
108+
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
109+
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
110+
$this->assertGreaterThan(0, $config->reportWidth, 'Report width is not greater than 0');
111+
112+
}//end testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
113+
114+
115+
/**
116+
* Test that the report width will be set correctly when found in the CodeSniffer.conf file.
117+
*
118+
* @return void
119+
*/
120+
public function testReportWidthCanBeSetFromConfFile()
121+
{
122+
$phpCodeSnifferConfig = [
123+
'default_standard' => 'PSR2',
124+
'report_width' => '120',
125+
];
126+
127+
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
128+
129+
$config = new Config();
130+
$this->assertSame(120, $config->reportWidth);
131+
132+
}//end testReportWidthCanBeSetFromConfFile()
133+
134+
135+
/**
136+
* Test that the report width will be set correctly when passed as a CLI argument.
137+
*
138+
* @return void
139+
*/
140+
public function testReportWidthCanBeSetFromCLI()
141+
{
142+
$_SERVER['argv'] = [
143+
'phpcs',
144+
'--report-width=100',
145+
];
146+
147+
$config = new Config();
148+
$this->assertSame(100, $config->reportWidth);
149+
150+
}//end testReportWidthCanBeSetFromCLI()
151+
152+
153+
/**
154+
* Test that the report width will be set correctly when multiple report widths are passed on the CLI.
155+
*
156+
* @return void
157+
*/
158+
public function testReportWidthWhenSetFromCLIFirstValuePrevails()
159+
{
160+
$_SERVER['argv'] = [
161+
'phpcs',
162+
'--report-width=100',
163+
'--report-width=200',
164+
];
165+
166+
$config = new Config();
167+
$this->assertSame(100, $config->reportWidth);
168+
169+
}//end testReportWidthWhenSetFromCLIFirstValuePrevails()
170+
171+
172+
/**
173+
* Test that a report width passed as a CLI argument will overrule a report width set in a CodeSniffer.conf file.
174+
*
175+
* @return void
176+
*/
177+
public function testReportWidthSetFromCLIOverrulesConfFile()
178+
{
179+
$phpCodeSnifferConfig = [
180+
'default_standard' => 'PSR2',
181+
'report_format' => 'summary',
182+
'show_warnings' => '0',
183+
'show_progress' => '1',
184+
'report_width' => '120',
185+
];
186+
187+
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
188+
189+
$cliArgs = [
190+
'phpcs',
191+
'--report-width=180',
192+
];
193+
194+
$config = new Config($cliArgs);
195+
$this->assertSame(180, $config->reportWidth);
196+
197+
}//end testReportWidthSetFromCLIOverrulesConfFile()
198+
199+
200+
/**
201+
* Test that the report width will be set to a non-0 positive integer when set to "auto".
202+
*
203+
* @return void
204+
*/
205+
public function testReportWidthInputHandlingForAuto()
206+
{
207+
$config = new Config();
208+
$config->reportWidth = 'auto';
209+
210+
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
211+
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
212+
$this->assertGreaterThan(0, $config->reportWidth, 'Report width is not greater than 0');
213+
214+
}//end testReportWidthInputHandlingForAuto()
215+
216+
217+
/**
218+
* Test that the report width will be set correctly for various types of input.
219+
*
220+
* @param mixed $input Input value received.
221+
* @param int $expected Expected report width.
222+
*
223+
* @dataProvider dataReportWidthInputHandling
224+
*
225+
* @return void
226+
*/
227+
public function testReportWidthInputHandling($input, $expected)
228+
{
229+
$config = new Config();
230+
$config->reportWidth = $input;
231+
232+
$this->assertSame($expected, $config->reportWidth);
233+
234+
}//end testReportWidthInputHandling()
235+
236+
237+
/**
238+
* Data provider.
239+
*
240+
* @return array
241+
*/
242+
public function dataReportWidthInputHandling()
243+
{
244+
return [
245+
'No value (empty string)' => [
246+
'value' => '',
247+
'expected' => Config::DEFAULT_REPORT_WIDTH,
248+
],
249+
'Value: invalid input type null' => [
250+
'value' => null,
251+
'expected' => Config::DEFAULT_REPORT_WIDTH,
252+
],
253+
'Value: invalid input type false' => [
254+
'value' => false,
255+
'expected' => Config::DEFAULT_REPORT_WIDTH,
256+
],
257+
'Value: invalid input type float' => [
258+
'value' => 100.50,
259+
'expected' => Config::DEFAULT_REPORT_WIDTH,
260+
],
261+
'Value: invalid string value "invalid"' => [
262+
'value' => 'invalid',
263+
'expected' => Config::DEFAULT_REPORT_WIDTH,
264+
],
265+
'Value: invalid string value, non-integer string "50.25"' => [
266+
'value' => '50.25',
267+
'expected' => Config::DEFAULT_REPORT_WIDTH,
268+
],
269+
'Value: valid numeric string value' => [
270+
'value' => '250',
271+
'expected' => 250,
272+
],
273+
'Value: valid int value' => [
274+
'value' => 220,
275+
'expected' => 220,
276+
],
277+
'Value: negative int value becomes positive int' => [
278+
'value' => -180,
279+
'expected' => 180,
280+
],
281+
];
282+
283+
}//end dataReportWidthInputHandling()
284+
285+
286+
/**
287+
* Helper function to set a static property on the Config class.
288+
*
289+
* @param string $name The name of the property to set.
290+
* @param mixed $value The value to set the propert to.
291+
*
292+
* @return void
293+
*/
294+
public static function setStaticProperty($name, $value)
295+
{
296+
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
297+
$property->setAccessible(true);
298+
$property->setValue($value);
299+
$property->setAccessible(false);
300+
301+
}//end setStaticProperty()
302+
303+
304+
}//end class

0 commit comments

Comments
 (0)