Skip to content

Commit 2acead8

Browse files
committed
MC-38349: Create automated test for SVC changes
- Completed expanding db_schema cases
1 parent 2f7c598 commit 2acead8

File tree

5 files changed

+92
-43
lines changed

5 files changed

+92
-43
lines changed

src/Analyzer/DBSchema/DbSchemaWhitelistAnalyzer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public function analyze($registryBefore, $registryAfter)
5757
foreach ($registryTablesAfter as $moduleName => $tablesData) {
5858
$whiteListFileAfter = $registryAfter->mapping['whitelist_json'][$moduleName] ?? '';
5959
if (!file_exists($whiteListFileAfter)) {
60+
$tableFileAfter = $registryAfter->mapping['table'][$moduleName];
61+
$whiteListFileAfter = dirname($tableFileAfter) . '/db_schema_whitelist.json';
6062
$operation = new WhiteListWasRemoved($whiteListFileAfter, $moduleName);
6163
$this->report->add('database', $operation);
6264
continue;

tests/Unit/Console/Command/CompareSourceCommandDatabaseSchemasTest.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public function changesDataProvider()
109109
$pathToFixtures . '/change-primary-key/source-code-after',
110110
[
111111
'/Database \(MAJOR\)/',
112+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/change-primary-key/source-code-after/Magento/DbSchema/etc/db_schema\.xml:0#',
113+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/change-primary-key/source-code-before/Magento/DbSchema/etc/db_schema\.xml:0#',
112114
'/unit_test_table\/PRIMARY\s*\|\s*Primary key was changed\s*\|\s*M206/'
113115
],
114116
'Major change is detected.'
@@ -118,6 +120,7 @@ public function changesDataProvider()
118120
$pathToFixtures . '/add-primary-key/source-code-after',
119121
[
120122
'/Database \(MAJOR\)/',
123+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/add-primary-key/source-code-after/Magento/DbSchema/etc/db_schema\.xml:0#',
121124
'/unit_test_table\/PRIMARY\s*\|\s*Primary key was added\s*\|\s*M205/'
122125
],
123126
'Major change is detected.'
@@ -127,7 +130,10 @@ public function changesDataProvider()
127130
$pathToFixtures . '/drop-unique-key/source-code-after',
128131
[
129132
'/Database \(MAJOR\)/',
130-
'/unit_test_table\/UNIQUE_KEY\s*\|\s*Unique key was removed\s*\|\s*M209/'
133+
'/unit_test_table\/UNIQUE_KEY\s*\|\s*Unique key was removed\s*\|\s*M209/',
134+
'#unit_test_table/constraint\s*\|\s*Module db schema whitelist reduced \(unit_test_table/constraint\)\.\s*\|\s*M110#',
135+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/drop-unique-key/source-code-before/Magento/DbSchema/etc/db_schema.xml:0#',
136+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/drop-unique-key/source-code-before/Magento/DbSchema/etc/db_schema_whitelist.json:0#'
131137
],
132138
'Major change is detected.'
133139
],
@@ -136,7 +142,9 @@ public function changesDataProvider()
136142
$pathToFixtures . '/change-unique-key/source-code-after',
137143
[
138144
'/Database \(MAJOR\)/',
139-
'/unit_test_table\/UNIQUE_KEY\s*\|\s*Unique key was changed\s*\|\s*M210/'
145+
'/unit_test_table\/UNIQUE_KEY\s*\|\s*Unique key was changed\s*\|\s*M210/',
146+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/change-unique-key/source-code-before/Magento/DbSchema/etc/db_schema\.xml:0#',
147+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/change-unique-key/source-code-after/Magento/DbSchema/etc/db_schema\.xml:0#'
140148
],
141149
'Major change is detected.'
142150
],
@@ -145,7 +153,8 @@ public function changesDataProvider()
145153
$pathToFixtures . '/add-unique-key/source-code-after',
146154
[
147155
'/Database \(MAJOR\)/',
148-
'/unit_test_table\/UNIQUE_KEY\s*\|\s*Unique key was added\s*\|\s*M208/'
156+
'/unit_test_table\/UNIQUE_KEY\s*\|\s*Unique key was added\s*\|\s*M208/',
157+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/add-unique-key/source-code-after/Magento/DbSchema/etc/db_schema\.xml:0#'
149158
],
150159
'Major change is detected.'
151160
],
@@ -155,7 +164,9 @@ public function changesDataProvider()
155164
[
156165
'/Database \(MAJOR\)/',
157166
'/unit_test_table\/time_occurred\s*\|\s*Column was removed\s*\|\s*M107/',
158-
'/Module db schema whitelist reduced \(unit\_test\_table\/column\).\s*\|\s*M110/'
167+
'/Module db schema whitelist reduced \(unit\_test\_table\/column\).\s*\|\s*M110/',
168+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/column-removed/source-code-before/Magento/DbSchema/etc/db_schema_whitelist\.json:0#',
169+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/column-removed/source-code-before/Magento/DbSchema/etc/db_schema\.xml:0#'
159170
],
160171
'Major change is detected.'
161172
],
@@ -164,6 +175,7 @@ public function changesDataProvider()
164175
$pathToFixtures . '/column-added/source-code-after',
165176
[
166177
'/Database \(MINOR\)/',
178+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/column-added/source-code-after/Magento/DbSchema/etc/db_schema\.xml:0#',
167179
'/unit_test_table\/time_occurred\s*\|\s*Column was added\s*\|\s*M203/'
168180
],
169181
'Minor change is detected.'
@@ -174,7 +186,9 @@ public function changesDataProvider()
174186
[
175187
'/Database \(MAJOR\)/',
176188
'/other_unit_test_table\s*\|\s*Table was dropped\s*\|\s*M104/',
177-
'/Module db schema whitelist reduced \(other\_unit\_test\_table\).\s*\|\s*M110/'
189+
'/Module db schema whitelist reduced \(other\_unit\_test\_table\).\s*\|\s*M110/',
190+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/table-dropped/source-code-before/Magento/DbSchema/etc/db_schema\.xml:0#',
191+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/table-dropped/source-code-before/Magento/DbSchema/etc/db_schema_whitelist\.json:0#'
178192
],
179193
'Major change is detected.'
180194
],
@@ -183,7 +197,12 @@ public function changesDataProvider()
183197
$pathToFixtures . '/table-added/source-code-after',
184198
[
185199
'/Database \(MINOR\)/',
186-
'/other_unit_test_table\s*\|\s*Table was added\s*\|\s*M202/'
200+
'/other_unit_test_table\s*\|\s*Table was added\s*\|\s*M202/',
201+
'#other_table\s*\|\s*Table was added\s*\|\s*M202#',
202+
'#other_table\s*\|\s*Whitelist do not have table other_table declared in db_schema\.xml\s*\|\s*M109#',
203+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/table-added/source-code-after/Magento/DbSchema/etc/db_schema\.xml:0#',
204+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/table-added/source-code-after/Magento/DbSchema/etc/db_schema_whitelist\.json:0#',
205+
187206
],
188207
'Minor change is detected.'
189208
],
@@ -193,6 +212,9 @@ public function changesDataProvider()
193212
[
194213
'/Database \(MAJOR\)/',
195214
'/unit_test_table\s*\|\s*Table was dropped\s*\|\s*M104/',
215+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/table-changed/source-code-after/Magento/DbSchema/etc/db_schema\.xml:0#',
216+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/table-changed/source-code-before/Magento/DbSchema/etc/db_schema\.xml:0#',
217+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/table-changed/source-code-before/Magento/DbSchema/etc/db_schema_whitelist\.json:0#',
196218
'/unit_test_table\s*\|\s*Module db schema whitelist reduced \(unit\_test\_table\).\s*\|\s*M110/',
197219
'/new_unit_test_table\s*\|\s*Table was added\s*\|\s*M202/'
198220
],
@@ -203,6 +225,7 @@ public function changesDataProvider()
203225
$pathToFixtures . '/table-resource-changed/source-code-after',
204226
[
205227
'/Database \(MAJOR\)/',
228+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/table-resource-changed/source-code-before/Magento/DbSchema/etc/db_schema\.xml:0#',
206229
'/unit_test_table\s*\|\s*Table chard was changed from default to sales\s*\|\s*M105/'
207230
],
208231
'Major change is detected.'
@@ -215,7 +238,9 @@ public function changesDataProvider()
215238
'/Magento\/DbSchema\/etc\/db_schema_whitelist.json:0\s*\|\s*unit_test_table\s*\|\s*Module db schema whitelist reduced \(unit\_test\_table\).\s*\|\s*M110/',
216239
'/Magento\/DbSchemaSecond\/etc\/db_schema_whitelist\.json:0\s*\|\s*unit_test_table3\s*\|\s*Module db schema whitelist reduced \(unit\_test\_table3\).\s*\|\s*M110/',
217240
'/Magento\/DbSchemaSecond\/etc\/db_schema_whitelist\.json:0\s*\|\s*unit_test_table2\s*\|\s*Module db schema whitelist reduced \(unit\_test\_table2\).\s*\|\s*M110/',
218-
'/Magento\/DbSchema\/etc\/db_schema_whitelist.json:0\s*\|\s*unit_test_table3\s*\|\s*Module db schema whitelist reduced \(unit\_test\_table3\).\s*\|\s*M110/'
241+
'/Magento\/DbSchema\/etc\/db_schema_whitelist.json:0\s*\|\s*unit_test_table3\s*\|\s*Module db schema whitelist reduced \(unit\_test\_table3\).\s*\|\s*M110/',
242+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/whitelist-was-reduced/source-code-before/Magento/DbSchemaSecond/etc/db_schema\.xml:0#',
243+
'#Console/Command/CompareSourceCommandTest/_files/db_schema/whitelist-was-reduced/source-code-before/Magento/DbSchema/etc/db_schema\.xml:0#'
219244
],
220245
'Major change is detected.'
221246
],

tests/Unit/Console/Command/CompareSourceCommandTest/AbstractHtmlTestCaseForHtml.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private static function assertJsonContent(array $expectedJson, DOMDocument $docD
9797
} else {
9898
$docXpath = new DOMXPath($docDom);
9999
$xpathQuery = '//*[@id="packageChangesJson"]/text()';
100-
static::assertHtml($xpathQuery, null, $docDom);
100+
static::assertHtml($xpathQuery, null, $docDom); //ensure xpath resolves
101101
$jsonText = $docDom->saveHTML($docXpath->query($xpathQuery)->item(0));
102102
$encodedJson = json_decode($jsonText);
103103
//store expectedJson in same format
@@ -107,30 +107,29 @@ private static function assertJsonContent(array $expectedJson, DOMDocument $docD
107107
}
108108

109109
/**
110-
* Assert HTML document resolves xpath, finding pattern, or finding pattern within resolving xpath
110+
* Assert HTML document resolves xpath, resolves finding pattern, or resolves finding pattern within resolved xpath
111111
* @param HtmlParseInfoContainer $container
112112
* @param DOMXPath $docXpath
113113
*/
114-
public static function assertHtml(?string $xpathQuery, ?string $pattern, DOMDocument $docDom) {
114+
public static function assertHtml($xpathQuery, $regex, DOMDocument $docDom)
115+
{
115116
$docXpath = new DOMXPath($docDom);
116-
117117
if ($xpathQuery) {
118118
$nodeList = $docXpath->query($xpathQuery);
119119
if (!$nodeList || !$nodeList->length) {
120120
$body = $docXpath->document->saveHTML();
121121
static::fail('xpath selector: ' . $xpathQuery . " was invalid. Unable to return result from document:\n" . $body); //throws exception
122122
}
123-
$body = $docDom->saveHTML($nodeList->item(-1));
124-
}
125-
else {
123+
if ($regex) {
124+
$body = $docDom->saveHTML($nodeList->item(0));
125+
static::assertRegExp($regex, $body);
126+
}
127+
} else {
126128
$body = $docXpath->document->saveHTML();
127-
}
128-
if ($pattern) {
129-
static::assertRegExp($pattern, $body);
129+
static::assertRegExp($regex, $body);
130130
}
131131
}
132132

133-
134133
/**
135134
* Executes {@link CompareSourceCommandTest::$command} via {@link CommandTester}, using the arguments as command
136135
* line parameters.

tests/Unit/Console/Command/CompareSourceCommandTest/HtmlParseInfoContainer.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,13 @@ class HtmlParseInfoContainer
1818
*/
1919
public $pattern;
2020

21-
const checkOnlyFirstXPathMatch = 0;
22-
const checkAllXpathMatches = 1;
23-
24-
/**
25-
* @var bool
26-
*/
27-
public $isRegex;
28-
29-
/**
30-
* @var int
31-
*/
32-
public $xpathSearchType;
33-
34-
public function __construct(?string $pattern,
35-
?string $xpath = null,
36-
bool $isRegex = false,
37-
int $xpathSearchType = self::checkOnlyFirstXPathMatch)
21+
public function __construct(?string $pattern, ?string $xpath = null)
3822
{
23+
if( !($pattern || $xpath) ) {
24+
throw new \InvalidArgumentException('$pattern and $xpath can not both be empty');
25+
}
3926
$this->xpath = $xpath;
4027
$this->pattern = $pattern;
41-
$this->isRegex = $isRegex;
42-
$this->xpathSearchType = $xpathSearchType;
4328
}
4429

4530
}

tests/Unit/Console/Command/HtmlTest.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@ class HtmlTest extends AbstractHtmlTestCaseForHtml
2121
*
2222
* @param string $pathToSourceCodeBefore
2323
* @param string $pathToSourceCodeAfter
24-
* @param string[] $expectedLogEntries
24+
* @param int $allowedChangeLevel
25+
* @param $expectedHtmlEntries
26+
* @param $expectedPackageSection
2527
* @param string $expectedOutput
26-
* @param string[] $unexpectedLogEntries
28+
* @param $expectedStatusCode
29+
* @param bool $shouldSkipTest
2730
* @return void
2831
* @throws \Exception
2932
* @dataProvider changesDataProvider
3033
*/
3134
public function testExecute(
3235
$pathToSourceCodeBefore,
3336
$pathToSourceCodeAfter,
37+
$allowedChangeLevel,
3438
$expectedHtmlEntries,
3539
$expectedPackageSection,
3640
$expectedOutput,
@@ -40,6 +44,7 @@ public function testExecute(
4044
$this->doTestExecute(
4145
$pathToSourceCodeBefore,
4246
$pathToSourceCodeAfter,
47+
$allowedChangeLevel,
4348
$expectedHtmlEntries,
4449
$expectedPackageSection,
4550
$expectedOutput,
@@ -53,13 +58,13 @@ public function changesDataProvider()
5358
$pathToFixtures = __DIR__ . '/CompareSourceCommandTest/_files/all';
5459

5560
return [
56-
'test all changes of all types' => [
61+
'test all levels of changes of all types' => [
5762
$pathToFixtures . '/source-code-before',
5863
$pathToFixtures . '/source-code-after',
5964
Level::NONE,
6065
[
61-
new HtmlParseInfoContainer('MAJOR', '//html/body/table/tbody/tr[1]/td[2]'),
62-
new HtmlParseInfoContainer('Package Level Changes', '//html/body/table/tbody/tr[last()]/td[1]'),
66+
new HtmlParseInfoContainer('#MAJOR#', '//html/body/table/tbody/tr[1]/td[2]'),
67+
new HtmlParseInfoContainer('#Package Level Changes#', '//html/body/table/tbody/tr[last()]/td[1]'),
6368
],
6469
[
6570
['name' => 'test/api-class', 'level' => 'MINOR' ],
@@ -72,7 +77,40 @@ public function changesDataProvider()
7277
],
7378
'Major change is detected.',
7479
-1,
75-
]
80+
],
81+
'test only Major changes for all types' => [
82+
$pathToFixtures . '/source-code-before',
83+
$pathToFixtures . '/source-code-after',
84+
Level::MINOR,
85+
[
86+
new HtmlParseInfoContainer('#MAJOR#', '//html/body/table/tbody/tr[1]/td[2]'),
87+
new HtmlParseInfoContainer('#Package Level Changes#', '//html/body/table/tbody/tr[last()]/td[1]'),
88+
],
89+
[
90+
['name' => 'test/api-trait', 'level' => 'MAJOR' ],
91+
['name' => 'test/layout_xml', 'level' => 'MAJOR' ],
92+
['name' => 'test/di_xml', 'level' => 'MAJOR' ],
93+
['name' => 'test/system_xml', 'level' => 'MAJOR' ],
94+
['name' => 'test/xsd-schema', 'level' => 'MAJOR' ],
95+
['name' => 'test/less-schema', 'level' => 'MAJOR' ],
96+
],
97+
'Major change is detected.',
98+
-1,
99+
],
100+
'test allowing all changes for all types' => [
101+
$pathToFixtures . '/source-code-before',
102+
$pathToFixtures . '/source-code-after',
103+
Level::MAJOR,
104+
[
105+
new HtmlParseInfoContainer('#MAJOR#', '//html/body/table/tbody/tr[1]/td[2]'),
106+
new HtmlParseInfoContainer('#Package Level Changes#', '//html/body/table/tbody/tr[last()]/td[1]'),
107+
],
108+
[],
109+
'Major change is detected.',
110+
0,
111+
],
112+
113+
76114
];
77115
}
78116
}

0 commit comments

Comments
 (0)