Skip to content

Commit

Permalink
Merge pull request #47 from lion-packages/supp
Browse files Browse the repository at this point in the history
Generation of double rules for optional properties is validated
  • Loading branch information
Sleon4 authored Mar 22, 2024
2 parents ed6606c + 5c042f9 commit d5c9bf0
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 77 deletions.
190 changes: 121 additions & 69 deletions src/LionBundle/Commands/Lion/DB/RulesDBCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$entity = $input->getArgument('entity');

$selectedConnection = $this->selectConnectionByEnviroment($input, $output);

$entityPascal = $this->str->of($entity)->replace('_', ' ')->replace('-', ' ')->pascal()->get();

$connectionPascal = $this->str->of($selectedConnection)->replace('_', ' ')->replace('-', ' ')->pascal()->get();

$columns = DB::connection($selectedConnection)->show()->full()->columns()->from($entity)->getAll();

if (isset($columns->status)) {
Expand Down Expand Up @@ -106,79 +109,128 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if (!$isForeign) {
$ruleName = $this->str
->of($column->Field)
->replace('-', '_')
->replace('_', ' ')
->trim()
->pascal()
->concat('Rule')
->get();

$this
->getApplication()
->find('new:rule')
->run(new ArrayInput(['rule' => "{$connectionPascal}/MySQL/{$entityPascal}/{$ruleName}"]), $output);

$this->fileWriter->readFileRows(
"app/Rules/{$connectionPascal}/MySQL/{$entityPascal}/{$ruleName}.php",
[
12 => [
'replace' => true,
'content' => "'" . strtolower($column->Field) . "'",
'search' => "''"
],
19 => [
'replace' => true,
'content' => "'" . strtolower($column->Field) . "'",
'search' => "''"
],
23 => [
'replace' => true,
'content' => "'" . strtolower($column->Field) . "'",
'search' => "''"
],
26 => [
'replace' => true,
'content' => "'" . strtolower($column->Field) . "'",
'search' => "''"
],
30 => [
'replace' => true,
'content' => "'{$column->Comment}'",
'search' => "''"
],
33 => [
'replace' => true,
'content' => "'" . strtolower($column->Field) . "'",
'search' => "''"
],
37 => [
'replace' => true,
'content' => "'{$column->Default}'",
'search' => "''"
],
44 => [
'replace' => true,
'content' => ($column->Null === 'NO' ? 'false' : 'true'),
'search' => 'false'
],
52 => [
'replace' => true,
'multiple' => [
[
'content' => ($column->Null === 'NO' ? 'required' : 'optional'),
'search' => 'required'
]
]
]
]
);
if ($column->Null === 'YES') {
$this->generateRule(
$connectionPascal,
$entityPascal,
$column,
'Optional',
$output
);

$this->generateRule(
$connectionPascal,
$entityPascal,
$column,
'Required',
$output
);
} else {
$this->generateRule(
$connectionPascal,
$entityPascal,
$column,
'',
$output
);
}
} else {
$output->writeln($this->infoOutput("\t>> RULE: the rule for '{$column->Field}' property has been omitted, it is a foreign"));
$output->writeln(
$this->infoOutput(
"\t>> RULE: the rule for '{$column->Field}' property has been omitted, it is a foreign"
)
);
}
}

return Command::SUCCESS;
}

/**
* Generate rules for an entity
*
* @param string $connectionPascal [Connection name in PascalCase format]
* @param string $entityPascal [Entity name in PascalCase format]
* @param object $column [Property object]
* @param string $type [Defines whether the rule type is optional or
* required]
* @param OutputInterface $output [OutputInterface is the interface
* implemented by all Output classes]
*
* @return void
*/
private function generateRule(
string $connectionPascal,
string $entityPascal,
object $column,
string $type,
OutputInterface $output
): void {
$ruleName = $this->str
->of($column->Field)
->replace('-', '_')
->replace('_', ' ')
->trim()
->pascal()
->concat($type)
->concat('Rule')
->get();

$this
->getApplication()
->find('new:rule')
->run(new ArrayInput(['rule' => "{$connectionPascal}/MySQL/{$entityPascal}/{$ruleName}"]), $output);

$this->fileWriter->readFileRows("app/Rules/{$connectionPascal}/MySQL/{$entityPascal}/{$ruleName}.php", [
12 => [
'replace' => true,
'content' => "'" . strtolower($column->Field) . "'",
'search' => "''"
],
19 => [
'replace' => true,
'content' => "'" . strtolower($column->Field) . "'",
'search' => "''"
],
23 => [
'replace' => true,
'content' => "'" . strtolower($column->Field) . "'",
'search' => "''"
],
26 => [
'replace' => true,
'content' => "'" . strtolower($column->Field) . "'",
'search' => "''"
],
30 => [
'replace' => true,
'content' => "'{$column->Comment}'",
'search' => "''"
],
33 => [
'replace' => true,
'content' => "'" . strtolower($column->Field) . "'",
'search' => "''"
],
37 => [
'replace' => true,
'content' => "'{$column->Default}'",
'search' => "''"
],
44 => [
'replace' => true,
'content' => ($type === 'Required' ? 'false' : ($type === 'Optional' ? 'true' : 'false')),
'search' => 'false'
],
52 => [
'replace' => true,
'multiple' => [
[
'content' => ($column->Null === 'NO' ? 'required' : 'optional'),
'search' => 'required'
]
]
]
]);
}
}
20 changes: 12 additions & 8 deletions tests/Commands/Lion/DB/RulesDBCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ class RulesDBCommandTest extends Test
const ENTITY = 'users';
const IDRULE_NAMESPACE = 'App\\Rules\\LionDatabase\\MySQL\\Users\\IdRule';
const NAMERULE_NAMESPACE = 'App\\Rules\\LionDatabase\\MySQL\\Users\\NameRule';
const LASTNAMERULE_NAMESPACE = 'App\\Rules\\LionDatabase\\MySQL\\Users\\LastNameRule';
// const LASTNAMERULE_NAMESPACE = 'App\\Rules\\LionDatabase\\MySQL\\Users\\LastNameRule';
const LASTNAMERULE_NAMESPACE_REQUIRED = 'App\\Rules\\LionDatabase\\MySQL\\Users\\LastNameRequiredRule';
const LASTNAMERULE_NAMESPACE_OPTIONAL = 'App\\Rules\\LionDatabase\\MySQL\\Users\\LastNameOptionalRule';
const EMAILRULE_NAMESPACE = 'App\\Rules\\LionDatabase\\MySQL\\Users\\EmailRule';
const RULES = [
self::IDRULE_NAMESPACE => 'app/Rules/LionDatabase/MySQL/Users/IdRule.php',
self::NAMERULE_NAMESPACE => 'app/Rules/LionDatabase/MySQL/Users/NameRule.php',
self::LASTNAMERULE_NAMESPACE => 'app/Rules/LionDatabase/MySQL/Users/LastNameRule.php',
self::EMAILRULE_NAMESPACE => 'app/Rules/LionDatabase/MySQL/Users/EmailRule.php'
];

private CommandTester $commandTester;

Expand All @@ -55,6 +51,7 @@ protected function setUp(): void
protected function tearDown(): void
{
$this->rmdirRecursively('./app/');

Schema::dropTable(self::ENTITY)->execute();
}

Expand Down Expand Up @@ -90,13 +87,20 @@ public function testExecute(): void
'disabled' => false
]);

$this->assertColumn($display, self::LASTNAMERULE_NAMESPACE, [
$this->assertColumn($display, self::LASTNAMERULE_NAMESPACE_OPTIONAL, [
'field' => 'last_name',
'desc' => '',
'value' => 'N/A',
'disabled' => true
]);

$this->assertColumn($display, self::LASTNAMERULE_NAMESPACE_REQUIRED, [
'field' => 'last_name',
'desc' => '',
'value' => 'N/A',
'disabled' => false
]);

$this->assertColumn($display, self::EMAILRULE_NAMESPACE, [
'field' => 'email',
'desc' => 'user email',
Expand Down

0 comments on commit d5c9bf0

Please sign in to comment.