Skip to content

Instantly share code, notes, and snippets.

@dereuromark
Last active November 15, 2024 02:18
Show Gist options
  • Save dereuromark/5afa93a72969c4beb5a90196a742659d to your computer and use it in GitHub Desktop.
Save dereuromark/5afa93a72969c4beb5a90196a742659d to your computer and use it in GitHub Desktop.
tests/schema.php
<?php
use Cake\Core\Plugin;
use Cake\Utility\Inflector;
$tables = [];
$plugins = [
'Queue',
'QueueScheduler',
'Geo',
'Tags',
'Captcha',
...
];
/**
* @var \DirectoryIterator<\DirectoryIterator> $iterator
*/
$iterator = new DirectoryIterator(__DIR__ . DS . 'Fixture');
foreach ($iterator as $file) {
if (!preg_match('/(\w+)Fixture.php$/', (string)$file, $matches)) {
continue;
}
$name = $matches[1];
$tableName = null;
$class = 'App\\Test\\Fixture\\' . $name . 'Fixture';
try {
$fieldsObject = (new ReflectionClass($class))->getProperty('fields');
$tableObject = (new ReflectionClass($class))->getProperty('table');
$tableName = $tableObject->getDefaultValue();
} catch (ReflectionException $e) {
continue;
}
if (!$tableName) {
$tableName = Inflector::underscore($name);
}
$array = $fieldsObject->getDefaultValue();
$constraints = $array['_constraints'] ?? [];
$indexes = $array['_indexes'] ?? [];
unset($array['_constraints'], $array['_indexes'], $array['_options']);
$table = [
'table' => $tableName,
'columns' => $array,
'constraints' => $constraints,
'indexes' => $indexes,
];
$tables[$tableName] = $table;
}
foreach ($plugins as $plugin) {
$path = Plugin::path($plugin) . 'tests' . DS . 'Fixture';
if (!is_dir($path)) {
throw new RuntimeException('No fixtures directory for ' . $plugin);
}
/**
* @var \DirectoryIterator<\DirectoryIterator> $iterator
*/
$iterator = new DirectoryIterator($path);
foreach ($iterator as $file) {
if (!preg_match('/(\w+)Fixture.php$/', (string)$file, $matches)) {
continue;
}
$name = $matches[1];
$tableName = null;
$class = $plugin . '\\Test\\Fixture\\' . $name . 'Fixture';
try {
$fieldsObject = (new ReflectionClass($class))->getProperty('fields');
$tableObject = (new ReflectionClass($class))->getProperty('table');
$tableName = $tableObject->getDefaultValue();
} catch (ReflectionException $e) {
continue;
}
if (!$tableName) {
$tableName = Inflector::underscore($name);
}
$array = $fieldsObject->getDefaultValue();
$constraints = $array['_constraints'] ?? [];
$indexes = $array['_indexes'] ?? [];
unset($array['_constraints'], $array['_indexes'], $array['_options']);
$table = [
'table' => $tableName,
'columns' => $array,
'constraints' => $constraints,
'indexes' => $indexes,
];
if (isset($tables[$tableName])) {
continue;
//throw new RuntimeException('Already existing: ' . $tableName); //PHP_EOL . print_r($tables[$tableName], true) . PHP_EOL . 'vs.' . PHP_EOL . print_r($table, true)
}
$tables[$tableName] = $table;
}
}
ksort($tables);
return $tables;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment