Skip to content

Commit 511a538

Browse files
committed
Implement use of the new AbstractMethodUnitTest class
1 parent a9d2351 commit 511a538

7 files changed

+140
-448
lines changed

tests/Core/File/FindEndOfStatementTest.php

Lines changed: 32 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,11 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\File;
1111

12-
use PHP_CodeSniffer\Config;
13-
use PHP_CodeSniffer\Ruleset;
14-
use PHP_CodeSniffer\Files\DummyFile;
15-
use PHPUnit\Framework\TestCase;
12+
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
1613

17-
class FindEndOfStatementTest extends TestCase
14+
class FindEndOfStatementTest extends AbstractMethodUnitTest
1815
{
1916

20-
/**
21-
* The PHP_CodeSniffer_File object containing parsed contents of the test case file.
22-
*
23-
* @var \PHP_CodeSniffer\Files\File
24-
*/
25-
private $phpcsFile;
26-
27-
28-
/**
29-
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
30-
*
31-
* Methods used for these tests can be found in a test case file in the same
32-
* directory and with the same name, using the .inc extension.
33-
*
34-
* @return void
35-
*/
36-
public function setUp()
37-
{
38-
$config = new Config();
39-
$config->standards = ['Generic'];
40-
41-
$ruleset = new Ruleset($config);
42-
43-
$pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc';
44-
$this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config);
45-
$this->phpcsFile->process();
46-
47-
}//end setUp()
48-
49-
50-
/**
51-
* Clean up after finished test.
52-
*
53-
* @return void
54-
*/
55-
public function tearDown()
56-
{
57-
unset($this->phpcsFile);
58-
59-
}//end tearDown()
60-
6117

6218
/**
6319
* Test a simple assignment.
@@ -66,10 +22,10 @@ public function tearDown()
6622
*/
6723
public function testSimpleAssignment()
6824
{
69-
$start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSimpleAssignment */') + 2);
70-
$found = $this->phpcsFile->findEndOfStatement($start);
25+
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSimpleAssignment */') + 2);
26+
$found = self::$phpcsFile->findEndOfStatement($start);
7127

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

7531
}//end testSimpleAssignment()
@@ -82,10 +38,10 @@ public function testSimpleAssignment()
8238
*/
8339
public function testControlStructure()
8440
{
85-
$start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testControlStructure */') + 2);
86-
$found = $this->phpcsFile->findEndOfStatement($start);
41+
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testControlStructure */') + 2);
42+
$found = self::$phpcsFile->findEndOfStatement($start);
8743

88-
$tokens = $this->phpcsFile->getTokens();
44+
$tokens = self::$phpcsFile->getTokens();
8945
$this->assertSame($tokens[($start + 6)], $tokens[$found]);
9046

9147
}//end testControlStructure()
@@ -98,10 +54,10 @@ public function testControlStructure()
9854
*/
9955
public function testClosureAssignment()
10056
{
101-
$start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testClosureAssignment */') + 2);
102-
$found = $this->phpcsFile->findEndOfStatement($start);
57+
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testClosureAssignment */') + 2);
58+
$found = self::$phpcsFile->findEndOfStatement($start);
10359

104-
$tokens = $this->phpcsFile->getTokens();
60+
$tokens = self::$phpcsFile->getTokens();
10561
$this->assertSame($tokens[($start + 13)], $tokens[$found]);
10662

10763
}//end testClosureAssignment()
@@ -115,24 +71,24 @@ public function testClosureAssignment()
11571
public function testHeredocFunctionArg()
11672
{
11773
// Find the end of the function.
118-
$start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testHeredocFunctionArg */') + 2);
119-
$found = $this->phpcsFile->findEndOfStatement($start);
74+
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testHeredocFunctionArg */') + 2);
75+
$found = self::$phpcsFile->findEndOfStatement($start);
12076

121-
$tokens = $this->phpcsFile->getTokens();
77+
$tokens = self::$phpcsFile->getTokens();
12278
$this->assertSame($tokens[($start + 10)], $tokens[$found]);
12379

12480
// Find the end of the heredoc.
12581
$start += 2;
126-
$found = $this->phpcsFile->findEndOfStatement($start);
82+
$found = self::$phpcsFile->findEndOfStatement($start);
12783

128-
$tokens = $this->phpcsFile->getTokens();
84+
$tokens = self::$phpcsFile->getTokens();
12985
$this->assertSame($tokens[($start + 4)], $tokens[$found]);
13086

13187
// Find the end of the last arg.
13288
$start = ($found + 2);
133-
$found = $this->phpcsFile->findEndOfStatement($start);
89+
$found = self::$phpcsFile->findEndOfStatement($start);
13490

135-
$tokens = $this->phpcsFile->getTokens();
91+
$tokens = self::$phpcsFile->getTokens();
13692
$this->assertSame($tokens[$start], $tokens[$found]);
13793

13894
}//end testHeredocFunctionArg()
@@ -146,24 +102,24 @@ public function testHeredocFunctionArg()
146102
public function testSwitch()
147103
{
148104
// Find the end of the switch.
149-
$start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSwitch */') + 2);
150-
$found = $this->phpcsFile->findEndOfStatement($start);
105+
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSwitch */') + 2);
106+
$found = self::$phpcsFile->findEndOfStatement($start);
151107

152-
$tokens = $this->phpcsFile->getTokens();
108+
$tokens = self::$phpcsFile->getTokens();
153109
$this->assertSame($tokens[($start + 28)], $tokens[$found]);
154110

155111
// Find the end of the case.
156112
$start += 9;
157-
$found = $this->phpcsFile->findEndOfStatement($start);
113+
$found = self::$phpcsFile->findEndOfStatement($start);
158114

159-
$tokens = $this->phpcsFile->getTokens();
115+
$tokens = self::$phpcsFile->getTokens();
160116
$this->assertSame($tokens[($start + 8)], $tokens[$found]);
161117

162118
// Find the end of default case.
163119
$start += 11;
164-
$found = $this->phpcsFile->findEndOfStatement($start);
120+
$found = self::$phpcsFile->findEndOfStatement($start);
165121

166-
$tokens = $this->phpcsFile->getTokens();
122+
$tokens = self::$phpcsFile->getTokens();
167123
$this->assertSame($tokens[($start + 6)], $tokens[$found]);
168124

169125
}//end testSwitch()
@@ -177,24 +133,24 @@ public function testSwitch()
177133
public function testStatementAsArrayValue()
178134
{
179135
// Test short array syntax.
180-
$start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testStatementAsArrayValue */') + 7);
181-
$found = $this->phpcsFile->findEndOfStatement($start);
136+
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testStatementAsArrayValue */') + 7);
137+
$found = self::$phpcsFile->findEndOfStatement($start);
182138

183-
$tokens = $this->phpcsFile->getTokens();
139+
$tokens = self::$phpcsFile->getTokens();
184140
$this->assertSame($tokens[($start + 2)], $tokens[$found]);
185141

186142
// Test long array syntax.
187143
$start += 12;
188-
$found = $this->phpcsFile->findEndOfStatement($start);
144+
$found = self::$phpcsFile->findEndOfStatement($start);
189145

190-
$tokens = $this->phpcsFile->getTokens();
146+
$tokens = self::$phpcsFile->getTokens();
191147
$this->assertSame($tokens[($start + 2)], $tokens[$found]);
192148

193149
// Test same statement outside of array.
194150
$start += 10;
195-
$found = $this->phpcsFile->findEndOfStatement($start);
151+
$found = self::$phpcsFile->findEndOfStatement($start);
196152

197-
$tokens = $this->phpcsFile->getTokens();
153+
$tokens = self::$phpcsFile->getTokens();
198154
$this->assertSame($tokens[($start + 3)], $tokens[$found]);
199155

200156
}//end testStatementAsArrayValue()

tests/Core/File/FindExtendedClassNameTest.php

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,11 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\File;
1111

12-
use PHP_CodeSniffer\Config;
13-
use PHP_CodeSniffer\Ruleset;
14-
use PHP_CodeSniffer\Files\DummyFile;
15-
use PHPUnit\Framework\TestCase;
12+
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
1613

17-
class FindExtendedClassNameTest extends TestCase
14+
class FindExtendedClassNameTest extends AbstractMethodUnitTest
1815
{
1916

20-
/**
21-
* The PHP_CodeSniffer_File object containing parsed contents of the test case file.
22-
*
23-
* @var \PHP_CodeSniffer\Files\File
24-
*/
25-
private $phpcsFile;
26-
27-
28-
/**
29-
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
30-
*
31-
* Methods used for these tests can be found in a test case file in the same
32-
* directory and with the same name, using the .inc extension.
33-
*
34-
* @return void
35-
*/
36-
public function setUp()
37-
{
38-
$config = new Config();
39-
$config->standards = ['Generic'];
40-
41-
$ruleset = new Ruleset($config);
42-
43-
$pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc';
44-
$this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config);
45-
$this->phpcsFile->process();
46-
47-
}//end setUp()
48-
49-
50-
/**
51-
* Clean up after finished test.
52-
*
53-
* @return void
54-
*/
55-
public function tearDown()
56-
{
57-
unset($this->phpcsFile);
58-
59-
}//end tearDown()
60-
6117

6218
/**
6319
* Test retrieving the name of the class being extended by another class
@@ -72,17 +28,17 @@ public function tearDown()
7228
*/
7329
public function testFindExtendedClassName($identifier, $expected)
7430
{
75-
$start = ($this->phpcsFile->numTokens - 1);
76-
$delim = $this->phpcsFile->findPrevious(
31+
$start = (self::$phpcsFile->numTokens - 1);
32+
$delim = self::$phpcsFile->findPrevious(
7733
T_COMMENT,
7834
$start,
7935
null,
8036
false,
8137
$identifier
8238
);
83-
$OOToken = $this->phpcsFile->findNext([T_CLASS, T_ANON_CLASS, T_INTERFACE], ($delim + 1));
39+
$OOToken = self::$phpcsFile->findNext([T_CLASS, T_ANON_CLASS, T_INTERFACE], ($delim + 1));
8440

85-
$result = $this->phpcsFile->findExtendedClassName($OOToken);
41+
$result = self::$phpcsFile->findExtendedClassName($OOToken);
8642
$this->assertSame($expected, $result);
8743

8844
}//end testFindExtendedClassName()

tests/Core/File/FindImplementedInterfaceNamesTest.php

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,11 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\File;
1111

12-
use PHP_CodeSniffer\Config;
13-
use PHP_CodeSniffer\Ruleset;
14-
use PHP_CodeSniffer\Files\DummyFile;
15-
use PHPUnit\Framework\TestCase;
12+
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
1613

17-
class FindImplementedInterfaceNamesTest extends TestCase
14+
class FindImplementedInterfaceNamesTest extends AbstractMethodUnitTest
1815
{
1916

20-
/**
21-
* The \PHP_CodeSniffer\Files\File object containing parsed contents of the test case file.
22-
*
23-
* @var \PHP_CodeSniffer\Files\File
24-
*/
25-
private $phpcsFile;
26-
27-
28-
/**
29-
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
30-
*
31-
* Methods used for these tests can be found in a test case file in the same
32-
* directory and with the same name, using the .inc extension.
33-
*
34-
* @return void
35-
*/
36-
public function setUp()
37-
{
38-
$config = new Config();
39-
$config->standards = ['Generic'];
40-
41-
$ruleset = new Ruleset($config);
42-
43-
$pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc';
44-
$this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config);
45-
$this->phpcsFile->process();
46-
47-
}//end setUp()
48-
49-
50-
/**
51-
* Clean up after finished test.
52-
*
53-
* @return void
54-
*/
55-
public function tearDown()
56-
{
57-
unset($this->phpcsFile);
58-
59-
}//end tearDown()
60-
6117

6218
/**
6319
* Test retrieving the name(s) of the interfaces being implemented by a class.
@@ -71,17 +27,17 @@ public function tearDown()
7127
*/
7228
public function testFindImplementedInterfaceNames($identifier, $expected)
7329
{
74-
$start = ($this->phpcsFile->numTokens - 1);
75-
$delim = $this->phpcsFile->findPrevious(
30+
$start = (self::$phpcsFile->numTokens - 1);
31+
$delim = self::$phpcsFile->findPrevious(
7632
T_COMMENT,
7733
$start,
7834
null,
7935
false,
8036
$identifier
8137
);
82-
$OOToken = $this->phpcsFile->findNext([T_CLASS, T_ANON_CLASS, T_INTERFACE], ($delim + 1));
38+
$OOToken = self::$phpcsFile->findNext([T_CLASS, T_ANON_CLASS, T_INTERFACE], ($delim + 1));
8339

84-
$result = $this->phpcsFile->findImplementedInterfaceNames($OOToken);
40+
$result = self::$phpcsFile->findImplementedInterfaceNames($OOToken);
8541
$this->assertSame($expected, $result);
8642

8743
}//end testFindImplementedInterfaceNames()

0 commit comments

Comments
 (0)