6 declare(strict_types=1);
29 private $componentRegistrar;
34 private $schemaConfig;
36 public function setUp()
43 $this->componentRegistrar =
$objectManager->get(ComponentRegistrarInterface::class);
44 $this->schemaConfig =
$objectManager->create(SchemaConfigInterface::class);
55 $undeclaredElements = [];
56 $resultMessage =
"New table elements that do not exist in the whitelist declaration:\n";
57 $whitelistTables = $this->getWhiteListTables();
58 $declarativeSchema = $this->schemaConfig->getDeclarationConfig();
60 foreach ($declarativeSchema->getTables() as $schemaTable) {
61 $tableNameWithoutPrefix = $schemaTable->getNameWithoutPrefix();
62 foreach ($schemaTable->getConstraints() as
$constraint) {
63 $constraintNameWithoutPrefix =
$constraint->getNameWithoutPrefix();
64 if (isset($whitelistTables[$tableNameWithoutPrefix][
Constraint::TYPE][$constraintNameWithoutPrefix])) {
68 $undeclaredElements[$tableNameWithoutPrefix][
Constraint::TYPE][] = $constraintNameWithoutPrefix;
71 foreach ($schemaTable->getIndexes() as
$index) {
72 $indexNameWithoutPrefix =
$index->getNameWithoutPrefix();
73 if (isset($whitelistTables[$tableNameWithoutPrefix][
Index::TYPE][$indexNameWithoutPrefix])) {
77 $undeclaredElements[$tableNameWithoutPrefix][
Index::TYPE][] = $indexNameWithoutPrefix;
81 $undeclaredElements = $this->filterUndeclaredElements($undeclaredElements);
83 if (!empty($undeclaredElements)) {
84 $resultMessage .= json_encode($undeclaredElements, JSON_PRETTY_PRINT);
87 $this->assertEmpty($undeclaredElements, $resultMessage);
96 private function filterUndeclaredElements(array $undeclaredElements): array
99 $ignoredElements = [];
100 foreach (
$files as $filePath) {
101 $ignoredElements = array_merge_recursive(
107 return $this->arrayRecursiveDiff($undeclaredElements, $ignoredElements);
117 private function arrayRecursiveDiff(array
$array1, array
$array2): array
122 if (array_key_exists($key,
$array2)) {
124 $recursiveDiffResult = $this->arrayRecursiveDiff(
$value,
$array2[$key]);
125 if (count($recursiveDiffResult)) {
126 $diffResult[$key] = $recursiveDiffResult;
134 $diffResult[$key] =
$value;
144 private function getWhiteListTables(): array
146 $whiteListTables = [];
149 $whiteListPath =
$path . DIRECTORY_SEPARATOR .
'etc' .
150 DIRECTORY_SEPARATOR .
'db_schema_whitelist.json';
152 if (file_exists($whiteListPath)) {
153 $whiteListTables = array_replace_recursive(
160 return $whiteListTables;
defined('TESTS_BP')||define('TESTS_BP' __DIR__
static getFiles(array $dirPatterns, $fileNamePattern, $recursive=true)
testConstraintsAndIndexesAreWhitelisted()
foreach($appDirs as $dir) $files
static getObjectManager()