Skip to content

Commit 80bfe89

Browse files
Readme file and feedback changes
1 parent 9185a9f commit 80bfe89

5 files changed

+21
-8
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

Magento2/Sniffs/Namespaces/UseDeclarationSniff.php renamed to Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111
/**
1212
* Detects static test namespace.
1313
*/
14-
class UseDeclarationSniff implements Sniff
14+
class ImportsFromTestNamespaceSniff implements Sniff
1515
{
1616

1717
/**
1818
* @var string
1919
*/
20-
private $_prohibitNamespace = 'Magento\Tests';
20+
private $prohibitNamespace = 'Magento\Tests';
2121

2222
/**
2323
* @var string
2424
*/
25-
protected $warningMessage = 'Incorrect namespace has been imported.';
25+
protected $warningMessage = 'Application modules should not use classed from test modules.';
2626

2727
/**
2828
* @var string
2929
*/
30-
protected $warningCode = 'WrongImportNamespaces';
30+
protected $warningCode = 'WrongImport';
3131

3232
/**
3333
* @inheritdoc
@@ -44,7 +44,7 @@ public function process(File $phpcsFile, $stackPtr)
4444
{
4545
$next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP, T_CLOSE_TAG], ($stackPtr + 1));
4646
$getTokenAsContent = $phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr));
47-
if (strpos($getTokenAsContent, $this->_prohibitNamespace) !== false) {
47+
if (strpos($getTokenAsContent, $this->prohibitNamespace) !== false) {
4848
$phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode);
4949
}
5050
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22
use Magento\Tests;
33
use Magento\Foo\Tests as FakeTest;
4-
use Magento\Real\Classes;
4+
use Magento\Real\Classes;

Magento2/Tests/Namespaces/UseDeclarationUnitTest.php renamed to Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Class InterfaceNameUnitTest
1212
*/
13-
class UseDeclarationUnitTest extends AbstractSniffUnitTest
13+
class ImportsFromTestNamespaceUnitTest extends AbstractSniffUnitTest
1414
{
1515
/**
1616
* @inheritdoc

Magento2/ruleset.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@
189189
<exclude-pattern>*Test.php</exclude-pattern>
190190
<exclude-pattern>*/tests/*</exclude-pattern>
191191
</rule>
192-
<rule ref="Magento2.Namespaces.UseDeclaration">
192+
<rule ref="Magento2.Namespaces.ImportsFromTestNamespaceSniff">
193193
<severity>8</severity>
194194
<type>warning</type>
195195
</rule>

0 commit comments

Comments
 (0)