Skip to content

Commit cc15b48

Browse files
authored
Merge pull request #149 from konarshankar07/142-import-test-rule
#142 : [New Rule] No imports from static tests namespaces
2 parents da46c5d + 4040cd5 commit cc15b48

5 files changed

+130
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Rule: Do not import from `Test` namespaces
2+
## Background
3+
Sometimes IDE imports the namespace with `Test` automatically for return data type like string, float etc or any other means.
4+
5+
## Reasoning
6+
Time to time we're getting issue with running tests on PRs in magento/magento2 repository because someone imported `\Magento\Tests\NamingConvention\true\string` by mistake. As result - we have "No build reports available" for "Database Compare build", "Functional Tests build", "Sample Data Tests build" while Static tests are shown as "failing" but in results - we don't really have reason
7+
8+
## How it works
9+
Any occurrence starts with `Magento\Tests` in import from the namespace will raise the warning.
10+
11+
## How to fix
12+
13+
Remove `Magento\Tests` from the imported namespaces
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Sniffs\Namespaces;
7+
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Files\File;
10+
use PHP_CodeSniffer\Util\Tokens;
11+
12+
/**
13+
* Detects static test namespace.
14+
*/
15+
class ImportsFromTestNamespaceSniff implements Sniff
16+
{
17+
/**
18+
* @var string
19+
*/
20+
private $prohibitNamespace = 'Magento\Tests';
21+
22+
/**
23+
* @var string
24+
*/
25+
protected $warningMessage = 'Application modules should not use classed from test modules.';
26+
27+
/**
28+
* @var string
29+
*/
30+
protected $warningCode = 'WrongImport';
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function register()
36+
{
37+
return [T_USE];
38+
}
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function process(File $phpcsFile, $stackPtr)
44+
{
45+
$next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP, T_CLOSE_TAG], ($stackPtr + 1));
46+
$tokens = $phpcsFile->getTokens();
47+
$getTokenAsContent = $phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr));
48+
if (strpos($getTokenAsContent, $this->prohibitNamespace) !== false) {
49+
$phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode);
50+
}
51+
if ($next !== false
52+
&& $tokens[$next]['code'] !== T_SEMICOLON
53+
&& $tokens[$next]['code'] !== T_CLOSE_TAG
54+
) {
55+
$baseUse = rtrim($phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr)));
56+
$baseUse = str_replace('use \\', '', $baseUse);
57+
$closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1));
58+
do {
59+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), $closingCurly, true);
60+
$groupedAsContent = $baseUse. $tokens[$next]['content'];
61+
$next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly);
62+
if (strpos($groupedAsContent, $this->prohibitNamespace) !== false) {
63+
$phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode);
64+
return;
65+
}
66+
} while ($next !== false);
67+
}
68+
}
69+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
use Magento\Tests;
3+
use Magento\Foo\Tests as FakeTest;
4+
use Magento\Real\Classes;
5+
use \Magento\{Tests\String, Tests\Int};
6+
use \Magento\{Foo\string, Bar\float};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Namespaces;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
/**
11+
* Class InterfaceNameUnitTest
12+
*/
13+
class ImportsFromTestNamespaceUnitTest extends AbstractSniffUnitTest
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function getErrorList()
19+
{
20+
return [];
21+
}
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
public function getWarningList()
27+
{
28+
return [
29+
2 => 1,
30+
5 => 1
31+
];
32+
}
33+
}

Magento2/ruleset.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@
189189
<exclude-pattern>*Test.php</exclude-pattern>
190190
<exclude-pattern>*/tests/*</exclude-pattern>
191191
</rule>
192+
<rule ref="Magento2.Namespaces.ImportsFromTestNamespace">
193+
<severity>8</severity>
194+
<type>warning</type>
195+
<exclude-pattern>*/_files/*</exclude-pattern>
196+
<exclude-pattern>*/Fixtures/*</exclude-pattern>
197+
<exclude-pattern>*/Test/*</exclude-pattern>
198+
<exclude-pattern>*Test.php</exclude-pattern>
199+
<exclude-pattern>*/tests/*</exclude-pattern>
200+
</rule>
192201
<rule ref="Magento2.NamingConvention.InterfaceName">
193202
<severity>8</severity>
194203
<type>warning</type>

0 commit comments

Comments
 (0)