Skip to content

Commit ce78f87

Browse files
committed
Merge branch '4.0' of github.com:squizlabs/PHP_CodeSniffer into 4.0
2 parents 7538100 + 559557d commit ce78f87

File tree

1 file changed

+294
-0
lines changed

1 file changed

+294
-0
lines changed

tests/Core/Config/ReportWidthTest.php

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

0 commit comments

Comments
 (0)