Skip to content

Commit e5a5848

Browse files
authored
Merge pull request #72 from magento-commerce/imported-svera-magento-coding-standard-281
[Imported] AC-669 install upgrade test
2 parents 8e4a639 + d974210 commit e5a5848

15 files changed

+171
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types = 1);
7+
8+
namespace Magento2\Sniffs\Legacy;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
use SplFileInfo;
13+
14+
class InstallUpgradeSniff implements Sniff
15+
{
16+
private const ERROR_CODE = 'obsoleteScript';
17+
18+
/**
19+
* @var string[]
20+
*/
21+
private $wrongPrefixes = [
22+
'install-' => 'Install scripts are obsolete. '
23+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
24+
'InstallSchema' => 'InstallSchema scripts are obsolete. '
25+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
26+
'InstallData' => 'InstallData scripts are obsolete. '
27+
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
28+
'data-install-' => 'Install scripts are obsolete. Please create class InstallData in module\'s Setup folder',
29+
'upgrade-' => 'Upgrade scripts are obsolete. '
30+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
31+
'UpgradeSchema' => 'UpgradeSchema scripts are obsolete. '
32+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
33+
'UpgradeData' => 'UpgradeSchema scripts are obsolete. '
34+
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
35+
'data-upgrade-' => 'Upgrade scripts are obsolete. '
36+
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
37+
'recurring' => 'Recurring scripts are obsolete. Please create class Recurring in module\'s Setup folder',
38+
];
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function register(): array
44+
{
45+
return [
46+
T_OPEN_TAG
47+
];
48+
}
49+
50+
/**
51+
* @inheritDoc
52+
*/
53+
public function process(File $phpcsFile, $stackPtr)
54+
{
55+
if ($stackPtr > 0) {
56+
return;
57+
}
58+
59+
$fileInfo = new SplFileInfo($phpcsFile->getFilename());
60+
61+
foreach ($this->wrongPrefixes as $prefix => $errorMessage) {
62+
if (strpos($fileInfo->getFilename(), $prefix) === 0) {
63+
$phpcsFile->addError($errorMessage, 0, self::ERROR_CODE);
64+
}
65+
}
66+
67+
if (preg_match('/(sql|data)/', $fileInfo->getPath()) === 1) {
68+
$phpcsFile->addError(
69+
$fileInfo->getFilename()." is in an invalid directory ".$fileInfo->getPath().":\n"
70+
. "- Create a data patch within module's Setup/Patch/Data folder for data upgrades.\n"
71+
. "- Use declarative schema approach in module's etc/db_schema.xml file for schema changes.",
72+
0,
73+
self::ERROR_CODE
74+
);
75+
}
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Legacy;
7+
8+
use DirectoryIterator;
9+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
10+
use RecursiveDirectoryIterator;
11+
use RecursiveIteratorIterator;
12+
13+
class InstallUpgradeUnitTest extends AbstractSniffUnitTest
14+
{
15+
private $wrongFileNames = [
16+
'data-install-.inc',
17+
'data-upgrade-.inc',
18+
'install-sample.inc',
19+
'InstallData.inc',
20+
'InstallSchema.inc',
21+
'recurring.inc',
22+
'upgrade-.inc',
23+
'UpgradeData.inc',
24+
'UpgradeSchema.inc',
25+
'file.inc',
26+
'file2.inc',
27+
];
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
protected function getTestFiles($testFileBase): array
33+
{
34+
$testFiles = [];
35+
36+
$dir = __DIR__.'/_files/InstallUpgradeUnitTest';
37+
$di = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
38+
39+
/**
40+
* @var DirectoryIterator $file
41+
*/
42+
foreach ($di as $file) {
43+
if ($file->isDir()) {
44+
continue;
45+
}
46+
$path = $file->getPathname();
47+
if ($path !== $testFileBase.'php' && substr($path, -5) !== 'fixed' && substr($path, -4) !== '.bak') {
48+
$testFiles[] = $path;
49+
}
50+
}
51+
52+
// Put them in order.
53+
sort($testFiles);
54+
55+
return $testFiles;
56+
}
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
public function getErrorList($testFile = '')
62+
{
63+
if (in_array($testFile, $this->wrongFileNames)) {
64+
return [
65+
1 => 1
66+
];
67+
}
68+
return [];
69+
}
70+
71+
/**
72+
* @inheritdoc
73+
*/
74+
public function getWarningList()
75+
{
76+
return [];
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

Magento2/ruleset.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,15 @@
130130
<severity>10</severity>
131131
<type>error</type>
132132
</rule>
133+
<rule ref="Magento2.Legacy.InstallUpgrade">
134+
<severity>10</severity>
135+
<type>error</type>
136+
</rule>
133137
<rule ref="Magento2.PHP.AutogeneratedClassNotInConstructor">
134138
<include-pattern>*\.php$</include-pattern>
135139
<severity>10</severity>
136140
<type>error</type>
137141
</rule>
138-
139142
<rule ref="Magento2.Html.HtmlSelfClosingTags">
140143
<severity>10</severity>
141144
<type>error</type>

0 commit comments

Comments
 (0)