Skip to content

Commit 75ff420

Browse files
committed
Merge branch 'feature/simplify-findendofstatement-tests' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents ce62dee + 82b8c5d commit 75ff420

File tree

1 file changed

+31
-48
lines changed

1 file changed

+31
-48
lines changed

tests/Core/File/FindEndOfStatementTest.php

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ class FindEndOfStatementTest extends AbstractMethodUnitTest
2222
*/
2323
public function testSimpleAssignment()
2424
{
25-
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSimpleAssignment */') + 2);
25+
$start = $this->getTargetToken('/* testSimpleAssignment */', T_VARIABLE);
2626
$found = self::$phpcsFile->findEndOfStatement($start);
2727

28-
$tokens = self::$phpcsFile->getTokens();
29-
$this->assertSame($tokens[($start + 5)], $tokens[$found]);
28+
$this->assertSame(($start + 5), $found);
3029

3130
}//end testSimpleAssignment()
3231

@@ -38,11 +37,10 @@ public function testSimpleAssignment()
3837
*/
3938
public function testControlStructure()
4039
{
41-
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testControlStructure */') + 2);
40+
$start = $this->getTargetToken('/* testControlStructure */', T_WHILE);
4241
$found = self::$phpcsFile->findEndOfStatement($start);
4342

44-
$tokens = self::$phpcsFile->getTokens();
45-
$this->assertSame($tokens[($start + 6)], $tokens[$found]);
43+
$this->assertSame(($start + 6), $found);
4644

4745
}//end testControlStructure()
4846

@@ -54,11 +52,10 @@ public function testControlStructure()
5452
*/
5553
public function testClosureAssignment()
5654
{
57-
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testClosureAssignment */') + 2);
55+
$start = $this->getTargetToken('/* testClosureAssignment */', T_VARIABLE, '$a');
5856
$found = self::$phpcsFile->findEndOfStatement($start);
5957

60-
$tokens = self::$phpcsFile->getTokens();
61-
$this->assertSame($tokens[($start + 13)], $tokens[$found]);
58+
$this->assertSame(($start + 13), $found);
6259

6360
}//end testClosureAssignment()
6461

@@ -71,25 +68,22 @@ public function testClosureAssignment()
7168
public function testHeredocFunctionArg()
7269
{
7370
// Find the end of the function.
74-
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testHeredocFunctionArg */') + 2);
71+
$start = $this->getTargetToken('/* testHeredocFunctionArg */', T_STRING, 'myFunction');
7572
$found = self::$phpcsFile->findEndOfStatement($start);
7673

77-
$tokens = self::$phpcsFile->getTokens();
78-
$this->assertSame($tokens[($start + 10)], $tokens[$found]);
74+
$this->assertSame(($start + 10), $found);
7975

8076
// Find the end of the heredoc.
8177
$start += 2;
8278
$found = self::$phpcsFile->findEndOfStatement($start);
8379

84-
$tokens = self::$phpcsFile->getTokens();
85-
$this->assertSame($tokens[($start + 4)], $tokens[$found]);
80+
$this->assertSame(($start + 4), $found);
8681

8782
// Find the end of the last arg.
8883
$start = ($found + 2);
8984
$found = self::$phpcsFile->findEndOfStatement($start);
9085

91-
$tokens = self::$phpcsFile->getTokens();
92-
$this->assertSame($tokens[$start], $tokens[$found]);
86+
$this->assertSame($start, $found);
9387

9488
}//end testHeredocFunctionArg()
9589

@@ -102,25 +96,22 @@ public function testHeredocFunctionArg()
10296
public function testSwitch()
10397
{
10498
// Find the end of the switch.
105-
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSwitch */') + 2);
99+
$start = $this->getTargetToken('/* testSwitch */', T_SWITCH);
106100
$found = self::$phpcsFile->findEndOfStatement($start);
107101

108-
$tokens = self::$phpcsFile->getTokens();
109-
$this->assertSame($tokens[($start + 28)], $tokens[$found]);
102+
$this->assertSame(($start + 28), $found);
110103

111104
// Find the end of the case.
112105
$start += 9;
113106
$found = self::$phpcsFile->findEndOfStatement($start);
114107

115-
$tokens = self::$phpcsFile->getTokens();
116-
$this->assertSame($tokens[($start + 8)], $tokens[$found]);
108+
$this->assertSame(($start + 8), $found);
117109

118110
// Find the end of default case.
119111
$start += 11;
120112
$found = self::$phpcsFile->findEndOfStatement($start);
121113

122-
$tokens = self::$phpcsFile->getTokens();
123-
$this->assertSame($tokens[($start + 6)], $tokens[$found]);
114+
$this->assertSame(($start + 6), $found);
124115

125116
}//end testSwitch()
126117

@@ -133,25 +124,22 @@ public function testSwitch()
133124
public function testStatementAsArrayValue()
134125
{
135126
// Test short array syntax.
136-
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testStatementAsArrayValue */') + 7);
127+
$start = $this->getTargetToken('/* testStatementAsArrayValue */', T_NEW);
137128
$found = self::$phpcsFile->findEndOfStatement($start);
138129

139-
$tokens = self::$phpcsFile->getTokens();
140-
$this->assertSame($tokens[($start + 2)], $tokens[$found]);
130+
$this->assertSame(($start + 2), $found);
141131

142132
// Test long array syntax.
143133
$start += 12;
144134
$found = self::$phpcsFile->findEndOfStatement($start);
145135

146-
$tokens = self::$phpcsFile->getTokens();
147-
$this->assertSame($tokens[($start + 2)], $tokens[$found]);
136+
$this->assertSame(($start + 2), $found);
148137

149138
// Test same statement outside of array.
150139
$start += 10;
151140
$found = self::$phpcsFile->findEndOfStatement($start);
152141

153-
$tokens = self::$phpcsFile->getTokens();
154-
$this->assertSame($tokens[($start + 3)], $tokens[$found]);
142+
$this->assertSame(($start + 3), $found);
155143

156144
}//end testStatementAsArrayValue()
157145

@@ -163,27 +151,25 @@ public function testStatementAsArrayValue()
163151
*/
164152
public function testUseGroup()
165153
{
166-
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testUseGroup */') + 2);
154+
$start = $this->getTargetToken('/* testUseGroup */', T_USE);
167155
$found = self::$phpcsFile->findEndOfStatement($start);
168156

169-
$tokens = self::$phpcsFile->getTokens();
170-
$this->assertSame($tokens[($start + 23)], $tokens[$found]);
157+
$this->assertSame(($start + 23), $found);
171158

172159
}//end testUseGroup()
173160

174161

175162
/**
176-
* Test a use group.
163+
* Test arrow function as array value.
177164
*
178165
* @return void
179166
*/
180167
public function testArrowFunctionArrayValue()
181168
{
182-
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testArrowFunctionArrayValue */') + 7);
169+
$start = $this->getTargetToken('/* testArrowFunctionArrayValue */', T_FN);
183170
$found = self::$phpcsFile->findEndOfStatement($start);
184171

185-
$tokens = self::$phpcsFile->getTokens();
186-
$this->assertSame($tokens[($start + 9)], $tokens[$found]);
172+
$this->assertSame(($start + 9), $found);
187173

188174
}//end testArrowFunctionArrayValue()
189175

@@ -195,8 +181,8 @@ public function testArrowFunctionArrayValue()
195181
*/
196182
public function testStaticArrowFunction()
197183
{
198-
$static = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testStaticArrowFunction */') + 2);
199-
$fn = self::$phpcsFile->findNext(T_FN, ($static + 1));
184+
$static = $this->getTargetToken('/* testStaticArrowFunction */', T_STATIC);
185+
$fn = $this->getTargetToken('/* testStaticArrowFunction */', T_FN);
200186

201187
$endOfStatementStatic = self::$phpcsFile->findEndOfStatement($static);
202188
$endOfStatementFn = self::$phpcsFile->findEndOfStatement($fn);
@@ -213,11 +199,10 @@ public function testStaticArrowFunction()
213199
*/
214200
public function testArrowFunctionReturnValue()
215201
{
216-
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testArrowFunctionReturnValue */') + 2);
202+
$start = $this->getTargetToken('/* testArrowFunctionReturnValue */', T_FN);
217203
$found = self::$phpcsFile->findEndOfStatement($start);
218204

219-
$tokens = self::$phpcsFile->getTokens();
220-
$this->assertSame($tokens[($start + 18)], $tokens[$found]);
205+
$this->assertSame(($start + 18), $found);
221206

222207
}//end testArrowFunctionReturnValue()
223208

@@ -229,11 +214,10 @@ public function testArrowFunctionReturnValue()
229214
*/
230215
public function testArrowFunctionAsArgument()
231216
{
232-
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testArrowFunctionAsArgument */') + 10);
217+
$start = $this->getTargetToken('/* testArrowFunctionAsArgument */', T_FN);
233218
$found = self::$phpcsFile->findEndOfStatement($start);
234219

235-
$tokens = self::$phpcsFile->getTokens();
236-
$this->assertSame($tokens[($start + 8)], $tokens[$found]);
220+
$this->assertSame(($start + 8), $found);
237221

238222
}//end testArrowFunctionAsArgument()
239223

@@ -245,11 +229,10 @@ public function testArrowFunctionAsArgument()
245229
*/
246230
public function testArrowFunctionWithArrayAsArgument()
247231
{
248-
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testArrowFunctionWithArrayAsArgument */') + 10);
232+
$start = $this->getTargetToken('/* testArrowFunctionWithArrayAsArgument */', T_FN);
249233
$found = self::$phpcsFile->findEndOfStatement($start);
250234

251-
$tokens = self::$phpcsFile->getTokens();
252-
$this->assertSame($tokens[($start + 17)], $tokens[$found]);
235+
$this->assertSame(($start + 17), $found);
253236

254237
}//end testArrowFunctionWithArrayAsArgument()
255238

0 commit comments

Comments
 (0)