Skip to content

Commit 6a3b033

Browse files
committed
Added tests for the AbstractArraySniff class (currently failing due to bug #2745)
1 parent f4662e8 commit 6a3b033

File tree

4 files changed

+417
-0
lines changed

4 files changed

+417
-0
lines changed

package.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
126126
<file baseinstalldir="" name="RuleInclusionAbsoluteWindowsTest.php" role="test" />
127127
<file baseinstalldir="" name="RuleInclusionAbsoluteWindowsTest.xml" role="test" />
128128
</dir>
129+
<dir name="Sniffs">
130+
<file baseinstalldir="" name="AbstractArraySniffTest.inc" role="test" />
131+
<file baseinstalldir="" name="AbstractArraySniffTest.php" role="test" />
132+
<file baseinstalldir="" name="AbstractArraySniffTestable.php" role="test" />
133+
</dir>
129134
<dir name="Tokenizer">
130135
<file baseinstalldir="" name="AnonClassParenthesisOwnerTest.inc" role="test" />
131136
<file baseinstalldir="" name="AnonClassParenthesisOwnerTest.php" role="test" />
@@ -1976,6 +1981,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
19761981
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" />
19771982
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" />
19781983
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" />
1984+
<install as="CodeSniffer/Core/Sniffs/AbstractArraySniffTest.inc" name="tests/Core/Sniffs/AbstractArraySniffTest.inc" />
1985+
<install as="CodeSniffer/Core/Sniffs/AbstractArraySniffTest.php" name="tests/Core/Sniffs/AbstractArraySniffTest.php" />
1986+
<install as="CodeSniffer/Core/Sniffs/AbstractArraySniffTestable.php" name="tests/Core/Sniffs/AbstractArraySniffTestable.php" />
19791987
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" />
19801988
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" />
19811989
<install as="CodeSniffer/Core/Tokenizer/BackfillFnTokenTest.php" name="tests/Core/Tokenizer/BackfillFnTokenTest.php" />
@@ -2026,6 +2034,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
20262034
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" />
20272035
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" />
20282036
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" />
2037+
<install as="CodeSniffer/Core/Sniffs/AbstractArraySniffTest.inc" name="tests/Core/Sniffs/AbstractArraySniffTest.inc" />
2038+
<install as="CodeSniffer/Core/Sniffs/AbstractArraySniffTest.php" name="tests/Core/Sniffs/AbstractArraySniffTest.php" />
2039+
<install as="CodeSniffer/Core/Sniffs/AbstractArraySniffTestable.php" name="tests/Core/Sniffs/AbstractArraySniffTestable.php" />
20292040
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" />
20302041
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" />
20312042
<install as="CodeSniffer/Core/Tokenizer/BackfillFnTokenTest.php" name="tests/Core/Tokenizer/BackfillFnTokenTest.php" />
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/* testSimpleValues */
4+
$foo = [1,2,3];
5+
6+
/* testSimpleKeyValues */
7+
$foo = ['1'=>1,'2'=>2,'3'=>3];
8+
9+
/* testMissingKeys */
10+
$foo = ['1'=>1,2,'3'=>3];
11+
12+
/* testMultiTokenKeys */
13+
$paths = array(
14+
Init::ROOT_DIR.'/a' => 'a',
15+
Init::ROOT_DIR.'/b' => 'b',
16+
);
17+
18+
/* testMissingKeysCoalesceTernary */
19+
return [
20+
$a => static function () { return [1,2,3]; },
21+
$b ?? $c,
22+
$d ? [$e] : [$f],
23+
];
24+
25+
/* testTernaryValues */
26+
$foo = [
27+
'1' => $row['status'] === 'rejected'
28+
? self::REJECTED_CODE
29+
: self::VERIFIED_CODE,
30+
'2' => in_array($row['status'], array('notverified', 'unverified'), true)
31+
? self::STATUS_PENDING
32+
: self::STATUS_VERIFIED,
33+
'3' => strtotime($row['date']),
34+
];
35+
36+
/* testHeredocValues */
37+
$foo = array(
38+
<<<HERE
39+
HERE
40+
,
41+
<<<HERE
42+
HERE
43+
,
44+
);
45+
46+
/* testArrowFunctionValue */
47+
$foo = array(
48+
1 => '1',
49+
2 => fn ($x) => yield 'a' => $x,
50+
3 => '3',
51+
);
Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Sniffs\AbstractArraySniff.
4+
*
5+
* @author Greg Sherwood <[email protected]>
6+
* @copyright 2006-2020 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\Sniffs;
11+
12+
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
13+
14+
class AbstractArraySniffTest extends AbstractMethodUnitTest
15+
{
16+
17+
/**
18+
* The sniff objects we are testing.
19+
*
20+
* This extends the \PHP_CodeSniffer\Sniffs\AbstractArraySniff class to make the
21+
* internal workings of the sniff observable.
22+
*
23+
* @var \PHP_CodeSniffer\Sniffs\AbstractArraySniffTestable
24+
*/
25+
protected static $sniff;
26+
27+
28+
/**
29+
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
30+
*
31+
* The test case file for a unit test class has to be in the same directory
32+
* directory and use the same file name as the test class, using the .inc extension.
33+
*
34+
* @return void
35+
*/
36+
public static function setUpBeforeClass()
37+
{
38+
self::$sniff = new AbstractArraySniffTestable();
39+
parent::setUpBeforeClass();
40+
41+
}//end setUpBeforeClass()
42+
43+
44+
/**
45+
* Test an array of simple values only.
46+
*
47+
* @return void
48+
*/
49+
public function testSimpleValues()
50+
{
51+
$token = $this->getTargetToken('/* testSimpleValues */', T_OPEN_SHORT_ARRAY);
52+
self::$sniff->process(self::$phpcsFile, $token);
53+
54+
$expected = [
55+
0 => ['value_start' => ($token + 1)],
56+
1 => ['value_start' => ($token + 3)],
57+
2 => ['value_start' => ($token + 5)],
58+
];
59+
60+
$this->assertSame($expected, self::$sniff->indicies);
61+
62+
}//end testSimpleValues()
63+
64+
65+
/**
66+
* Test an array of simple keys and values.
67+
*
68+
* @return void
69+
*/
70+
public function testSimpleKeyValues()
71+
{
72+
$token = $this->getTargetToken('/* testSimpleKeyValues */', T_OPEN_SHORT_ARRAY);
73+
self::$sniff->process(self::$phpcsFile, $token);
74+
75+
$expected = [
76+
0 => [
77+
'index_start' => ($token + 1),
78+
'index_end' => ($token + 1),
79+
'arrow' => ($token + 2),
80+
'value_start' => ($token + 3),
81+
],
82+
1 => [
83+
'index_start' => ($token + 5),
84+
'index_end' => ($token + 5),
85+
'arrow' => ($token + 6),
86+
'value_start' => ($token + 7),
87+
],
88+
2 => [
89+
'index_start' => ($token + 9),
90+
'index_end' => ($token + 9),
91+
'arrow' => ($token + 10),
92+
'value_start' => ($token + 11),
93+
],
94+
];
95+
96+
$this->assertSame($expected, self::$sniff->indicies);
97+
98+
}//end testSimpleKeyValues()
99+
100+
101+
/**
102+
* Test an array of simple keys and values.
103+
*
104+
* @return void
105+
*/
106+
public function testMissingKeys()
107+
{
108+
$token = $this->getTargetToken('/* testMissingKeys */', T_OPEN_SHORT_ARRAY);
109+
self::$sniff->process(self::$phpcsFile, $token);
110+
111+
$expected = [
112+
0 => [
113+
'index_start' => ($token + 1),
114+
'index_end' => ($token + 1),
115+
'arrow' => ($token + 2),
116+
'value_start' => ($token + 3),
117+
],
118+
1 => [
119+
'value_start' => ($token + 5),
120+
],
121+
2 => [
122+
'index_start' => ($token + 7),
123+
'index_end' => ($token + 7),
124+
'arrow' => ($token + 8),
125+
'value_start' => ($token + 9),
126+
],
127+
];
128+
129+
$this->assertSame($expected, self::$sniff->indicies);
130+
131+
}//end testMissingKeys()
132+
133+
134+
/**
135+
* Test an array with keys that span multiple tokens.
136+
*
137+
* @return void
138+
*/
139+
public function testMultiTokenKeys()
140+
{
141+
$token = $this->getTargetToken('/* testMultiTokenKeys */', T_ARRAY);
142+
self::$sniff->process(self::$phpcsFile, $token);
143+
144+
$expected = [
145+
0 => [
146+
'index_start' => ($token + 4),
147+
'index_end' => ($token + 8),
148+
'arrow' => ($token + 10),
149+
'value_start' => ($token + 12),
150+
],
151+
1 => [
152+
'index_start' => ($token + 16),
153+
'index_end' => ($token + 20),
154+
'arrow' => ($token + 22),
155+
'value_start' => ($token + 24),
156+
],
157+
];
158+
159+
$this->assertSame($expected, self::$sniff->indicies);
160+
161+
}//end testMultiTokenKeys()
162+
163+
164+
/**
165+
* Test an array of simple keys and values.
166+
*
167+
* @return void
168+
*/
169+
public function testMissingKeysCoalesceTernary()
170+
{
171+
$token = $this->getTargetToken('/* testMissingKeysCoalesceTernary */', T_OPEN_SHORT_ARRAY);
172+
self::$sniff->process(self::$phpcsFile, $token);
173+
174+
$expected = [
175+
0 => [
176+
'index_start' => ($token + 3),
177+
'index_end' => ($token + 3),
178+
'arrow' => ($token + 5),
179+
'value_start' => ($token + 7),
180+
],
181+
1 => [
182+
'value_start' => ($token + 31),
183+
],
184+
2 => [
185+
'value_start' => ($token + 39),
186+
],
187+
];
188+
189+
$this->assertSame($expected, self::$sniff->indicies);
190+
191+
}//end testMissingKeysCoalesceTernary()
192+
193+
194+
/**
195+
* Test an array of ternary values.
196+
*
197+
* @return void
198+
*/
199+
public function testTernaryValues()
200+
{
201+
$token = $this->getTargetToken('/* testTernaryValues */', T_ARRAY);
202+
self::$sniff->process(self::$phpcsFile, $token);
203+
204+
$expected = [
205+
0 => [
206+
'index_start' => ($token + 3),
207+
'index_end' => ($token + 3),
208+
'arrow' => ($token + 5),
209+
'value_start' => ($token + 7),
210+
],
211+
1 => [
212+
'index_start' => ($token + 32),
213+
'index_end' => ($token + 32),
214+
'arrow' => ($token + 34),
215+
'value_start' => ($token + 36),
216+
],
217+
2 => [
218+
'index_start' => ($token + 72),
219+
'index_end' => ($token + 72),
220+
'arrow' => ($token + 74),
221+
'value_start' => ($token + 76),
222+
],
223+
];
224+
225+
$this->assertSame($expected, self::$sniff->indicies);
226+
227+
}//end testTernaryValues()
228+
229+
230+
/**
231+
* Test an array of heredocs.
232+
*
233+
* @return void
234+
*/
235+
public function testHeredocValues()
236+
{
237+
$token = $this->getTargetToken('/* testHeredocValues */', T_ARRAY);
238+
self::$sniff->process(self::$phpcsFile, $token);
239+
240+
$expected = [
241+
0 => [
242+
'value_start' => ($token + 4),
243+
],
244+
1 => [
245+
'value_start' => ($token + 10),
246+
],
247+
];
248+
249+
$this->assertSame($expected, self::$sniff->indicies);
250+
251+
}//end testHeredocValues()
252+
253+
254+
/**
255+
* Test an array of with an arrow function as a value.
256+
*
257+
* @return void
258+
*/
259+
public function testArrowFunctionValue()
260+
{
261+
$token = $this->getTargetToken('/* testArrowFunctionValue */', T_ARRAY);
262+
self::$sniff->process(self::$phpcsFile, $token);
263+
264+
$expected = [
265+
0 => [
266+
'index_start' => ($token + 4),
267+
'index_end' => ($token + 4),
268+
'arrow' => ($token + 6),
269+
'value_start' => ($token + 8),
270+
],
271+
1 => [
272+
'index_start' => ($token + 12),
273+
'index_end' => ($token + 12),
274+
'arrow' => ($token + 14),
275+
'value_start' => ($token + 16),
276+
],
277+
2 => [
278+
'index_start' => ($token + 34),
279+
'index_end' => ($token + 34),
280+
'arrow' => ($token + 36),
281+
'value_start' => ($token + 38),
282+
],
283+
];
284+
285+
$this->assertSame($expected, self::$sniff->indicies);
286+
287+
}//end testArrowFunctionValue()
288+
289+
290+
}//end class

0 commit comments

Comments
 (0)