Skip to content

Commit 8b675eb

Browse files
committed
Generic/ESLint: add missing unit tests
[Sniff completeness series PR] Note: it may be worth it to check if `eslint` is available on Travis by default or do a `npm i eslint` in the Travis `before_script` section to install it, so the sniff will actually be tested properly.
1 parent 5ea1be7 commit 8b675eb

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

package.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
437437
<file baseinstalldir="PHP/CodeSniffer" name="InlineControlStructureUnitTest.js.fixed" role="test" />
438438
<file baseinstalldir="PHP/CodeSniffer" name="InlineControlStructureUnitTest.php" role="test" />
439439
</dir>
440+
<dir name="Debug">
441+
<file baseinstalldir="PHP/CodeSniffer" name="ESLintUnitTest.js" role="test" />
442+
<file baseinstalldir="PHP/CodeSniffer" name="ESLintUnitTest.php" role="test" />
443+
</dir>
440444
<dir name="Files">
441445
<file baseinstalldir="PHP/CodeSniffer" name="ByteOrderMarkUnitTest.inc" role="test" />
442446
<file baseinstalldir="PHP/CodeSniffer" name="ByteOrderMarkUnitTest.php" role="test" />
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var foo = bar;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Unit test class for the ESLint sniff.
4+
*
5+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
6+
* @copyright 2019 Juliette Reinders Folmer. All rights reserved.
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\Generic\Tests\Debug;
11+
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
use PHP_CodeSniffer\Config;
14+
15+
class ESLintUnitTest extends AbstractSniffUnitTest
16+
{
17+
18+
/**
19+
* Basic ESLint config to use for testing the sniff.
20+
*
21+
* @var string
22+
*/
23+
const ESLINT_CONFIG = '{
24+
"parserOptions": {
25+
"ecmaVersion": 5,
26+
"sourceType": "script",
27+
"ecmaFeatures": {}
28+
},
29+
"rules": {
30+
"no-undef": 2,
31+
"no-unused-vars": 2
32+
}
33+
}';
34+
35+
36+
/**
37+
* Sets up this unit test.
38+
*
39+
* @return void
40+
*/
41+
protected function setUp()
42+
{
43+
parent::setUp();
44+
45+
$cwd = getcwd();
46+
file_put_contents($cwd.'/.eslintrc.json', self::ESLINT_CONFIG);
47+
48+
}//end setUp()
49+
50+
51+
/**
52+
* Remove artifact.
53+
*
54+
* @return void
55+
*/
56+
protected function tearDown()
57+
{
58+
parent::tearDown();
59+
60+
$cwd = getcwd();
61+
unlink($cwd.'/.eslintrc.json');
62+
63+
}//end tearDown()
64+
65+
66+
/**
67+
* Should this test be skipped for some reason.
68+
*
69+
* @return void
70+
*/
71+
protected function shouldSkipTest()
72+
{
73+
$eslintPath = Config::getExecutablePath('eslint');
74+
if ($eslintPath === null) {
75+
return true;
76+
}
77+
78+
return false;
79+
80+
}//end shouldSkipTest()
81+
82+
83+
/**
84+
* Returns the lines where errors should occur.
85+
*
86+
* The key of the array should represent the line number and the value
87+
* should represent the number of errors that should occur on that line.
88+
*
89+
* @return array<int, int>
90+
*/
91+
public function getErrorList()
92+
{
93+
return [1 => 2];
94+
95+
}//end getErrorList()
96+
97+
98+
/**
99+
* Returns the lines where warnings should occur.
100+
*
101+
* The key of the array should represent the line number and the value
102+
* should represent the number of warnings that should occur on that line.
103+
*
104+
* @return array<int, int>
105+
*/
106+
public function getWarningList()
107+
{
108+
return [];
109+
110+
}//end getWarningList()
111+
112+
113+
}//end class

0 commit comments

Comments
 (0)