Skip to content

Commit

Permalink
Minor changes - add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdlrahmanSaberAbdo committed Jan 11, 2022
1 parent 5230772 commit 6d283fb
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 79 deletions.
12 changes: 6 additions & 6 deletions src/Logic/Validations/UnitValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected function checkAttributes($root_dir, $module_collection, $unit_collecti
* @param $unit_collection
*/
protected function typeCase($type_value, $column, $unit_collection) {
$type = explode(":", strtolower($type_value))[0];
$type = explode(":", $type_value)[0];
$schema_types = $this->schema['types'];

// ========= (3) ========
Expand All @@ -188,9 +188,9 @@ protected function typeCase($type_value, $column, $unit_collection) {
* @param $type_value
*/
protected function handleTypeValue($schema_types, $type_value) {
$type = explode(":", strtolower($type_value))[0];
$type = explode(":", $type_value)[0];
$has_value = $schema_types[$type]['has_value'];
$values = explode(":", strtolower($type_value));
$values = explode(":", $type_value);

if($has_value) {
// ======= (4) =======
Expand All @@ -215,7 +215,7 @@ protected function handleTypeValue($schema_types, $type_value) {
protected function definitionCase($definition_value) {
$definitions = explode('|', $definition_value);
foreach ($definitions as $single_definition) {
$definition = explode(":", strtolower($single_definition))[0];
$definition = explode(":", $single_definition)[0];
$schema_definitions = $this->schema['definitions'];

// ===== (6) =====
Expand All @@ -235,9 +235,9 @@ protected function definitionCase($definition_value) {
* @param $definition_value
*/
protected function handleDefinitionValue($schema_definitions, $definition_value) {
$definition = explode(":", strtolower($definition_value))[0];
$definition = explode(":", $definition_value)[0];
$has_value = $schema_definitions[$definition]['has_value'];
$values = explode(":", strtolower($definition_value));
$values = explode(":", $definition_value);

if($has_value) {
// ======= (7) =======
Expand Down
51 changes: 27 additions & 24 deletions src/Processors/FactoryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,33 @@ public function process(): string
$this->type_str = '';
$this->definition_str = '';

$type = explode(':', $cases['type'])[0];

if(isset($cases['definition'])) {
$this->handleDefinitionCase($cases['definition']);
}
if (!$this->isRelationType($type)) {
if(isset($cases['definition'])) {
$this->handleDefinitionCase($cases['definition']);
}

$this->handleTypeCase($cases['type'], $column);

// @codeCoverageIgnoreStart
if($this->definition_str === '' && $this->type_str === '') {
$this->processor .= <<<STR
'$column' => ''
STR;
} else {
$this->processor .= <<<STR
'$column' => \$this->faker->
STR;
}
// @codeCoverageIgnoreEnd
/**
* Check if type_str is empty or not
*/
$this->handleTypeCase($cases['type'], $column);

// @codeCoverageIgnoreStart
if($this->definition_str === '' && $this->type_str === '') {
$this->processor .= <<<STR
'$column' => ''
STR;
} else {
$this->processor .= <<<STR
'$column' => \$this->faker->
STR;
}
// @codeCoverageIgnoreEnd
/**
* Check if type_str is empty or not
*/

$this->processor .= ($this->definition_str !== '' ? $this->definition_str . '->': '') . ($this->type_str !== '' ? $this->type_str : '');
$this->processor .= array_key_last($this->json['attributes']) == $column ? ',' : ",\n";
$this->processor .= ($this->definition_str !== '' ? $this->definition_str . '->': '') . ($this->type_str !== '' ? $this->type_str : '');
$this->processor .= ",\n";
}
}

return $this->processor;
Expand All @@ -64,12 +67,12 @@ public function process(): string
*/
private function handleDefinitionCase($single_definition): void
{
$definitions = explode("|", strtolower($single_definition));
$definitions = explode("|", $single_definition);

$schema_definitions = $this->schema['definitions'];

foreach ($definitions as $definition) {
$definition = explode(":", strtolower($definition))[0];
$definition = explode(":", $definition)[0];
if($schema_definitions[$definition] && $schema_definitions[$definition]['factory'] !== '') {
$this->definition_str .= $schema_definitions[$definition]['factory'] . '()';
}
Expand All @@ -84,7 +87,7 @@ private function handleDefinitionCase($single_definition): void
*/
private function handleTypeCase($type, $column_name): void
{
$type = explode(":", strtolower($type))[0];
$type = explode(":", $type)[0];

$schema_types = $this->schema['types'];

Expand Down
10 changes: 5 additions & 5 deletions src/Processors/MigrationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public function process(): string
public function typeCase($type_str, $column)
{
$schema_types = $this->schema['types'];
$type = explode(":", strtolower($type_str))[0];
$type = explode(":", $type_str)[0];

if(($this->isSchemaFound('types', $type))) {
$has_value = $schema_types[$type]['has_value'];

if($has_value) {
$values = explode(",", explode(":", strtolower($type_str))[1]);
$values = explode(",", explode(":", $type_str)[1]);
$type_value = '';
foreach($values as $value) {
if (is_numeric($value)) {
Expand All @@ -82,16 +82,16 @@ public function typeCase($type_str, $column)

public function definitionCase($definition_str) {
$schema_definitions = $this->schema['definitions'];
$definitions = explode("|", strtolower($definition_str));
$definitions = explode("|", $definition_str);

foreach ($definitions as $definition) {
$single_definition = explode(":", strtolower($definition))[0];
$single_definition = explode(":", $definition)[0];

if($this->isSchemaFound('definitions', $single_definition)) {
$has_value = $schema_definitions[$single_definition]['has_value'];

if($has_value) {
$values = explode(",", explode(":", strtolower($definition))[1]);
$values = explode(",", explode(":", $definition)[1]);
$definition_value = '';

foreach($values as $value) {
Expand Down
6 changes: 6 additions & 0 deletions src/Processors/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,10 @@ protected function isOneValueType($str): bool
{
return $str == 'one_value_argument';
}

protected function isRelationType($type): bool
{
return $type === 'morphs' || $type === 'nullableMorphs' ||
$type === 'nullableUuidMorphs' || $type === 'uuidMorphs';
}
}
70 changes: 36 additions & 34 deletions src/Processors/RequestProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,54 @@ public function process(): string
* === Type case
* will extract it to different functions to make the code more readable
*/
$type = strtolower($cases['type']);
$schema_types = $this->schema['types'];
$type = $schema_types[$type]['request'] !== "" ? $schema_types[$type]['request'] . '|' : "";
$type = $cases['type'];
if (!$this->isRelationType($type)) {
$schema_types = $this->schema['types'];
$type = $schema_types[$type]['request'] !== "" ? $schema_types[$type]['request'] . '|' : "";

// @codeCoverageIgnoreStart
$this->processor .= <<<STR
// @codeCoverageIgnoreStart
$this->processor .= <<<STR
'$column' => '$type
STR;
// @codeCoverageIgnoreEnd
// @codeCoverageIgnoreEnd

/**
* === definition case
*/
if(isset($cases['definition'])) {
$definitions = explode("|", strtolower($cases['definition']));
$schema_definitions = $this->schema['definitions'];
/**
* === definition case
*/
if(isset($cases['definition'])) {
$definitions = explode("|", $cases['definition']);
$schema_definitions = $this->schema['definitions'];

foreach ($definitions as $key => $value) {
$value = explode(":", strtolower($value))[0];
foreach ($definitions as $key => $value) {
$value = explode(":", $value)[0];

if ($value == 'nullable') {
$nullable = true;
}
if ($value == 'nullable') {
$nullable = true;
}

if($schema_definitions[$value]['request'] == 'unique') {
$this->processor .= $schema_definitions[$value]['request'] . ':' . $this->unit_collection['plural_lower_case'];
} else {
$this->processor .= $schema_definitions[$value]['request'];
}
/**
* need some updates here (it's not working properly here)
*/

if($schema_definitions[$value]['request'] == 'unique') {
$this->processor .= $schema_definitions[$value]['request'] . ':' . $this->unit_collection['plural_lower_case'];
} else {
$this->processor .= $schema_definitions[$value]['request'];
$this->processor .= array_key_last($definitions) === $key ||
$schema_definitions[$value]['request'] === '' ? '' : '|';
}
/**
* need some updates here (it's not working properly here)
*/
}

$this->processor .= array_key_last($definitions) === $key ||
$schema_definitions[$value]['request'] === '' ? '' : '|';
if($nullable) {
$this->processor .= "'";
} else {
$this->processor .= 'required' . "'";
}
}

if($nullable) {
$this->processor .= "'";
} else {
$this->processor .= 'required' . "'";
$nullable = false;
$this->processor .= ",\n";
}

$nullable = false;
$this->processor .= array_key_last($attributes) == $column ? ',' : ",\n";
}

return $this->processor;
Expand Down
7 changes: 5 additions & 2 deletions src/Processors/ResourceProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ class ResourceProcessor extends Processor
public function process(): string
{
foreach ($this->json['attributes'] as $key => $value) {
$this->setInitStr($key);
$this->processor .= array_key_last($this->json['attributes']) == $key ? ',' : ",\n";
$type = explode(':', $value['type'])[0];
if (!$this->isRelationType($type)) {
$this->setInitStr($key);
$this->processor .= ",\n";
}
}

return $this->processor;
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Processors/FactoryProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function test_output_str()
'image_url' => \$this->faker->unique()->url(),
'phone' => \$this->faker->phoneNumber(),
'enum_str' => '',
STR;
$expected_str = preg_replace("/\r/", "", $expected_str);
$this->assertEquals($expected_str, $output_str);
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Processors/MigrationProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function test_output_str()
\$table->string('image_url')->unique()->nullable();
\$table->string('phone')->nullable();
\$table->enum('enum_str', [1,2,3]);
\$table->morphs('morph_text');
\$table->nullableMorphs('nullable_morph_test');
STR;
$expected_str = preg_replace("/\r/", "", $expected_str);
$this->assertEquals($expected_str, $output_str);
Expand Down
14 changes: 8 additions & 6 deletions tests/Unit/Processors/ProcessorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ protected function setUp(): void

$data = [
'attributes' => [
'name' => ['type' => 'string', 'definition' => 'default:test2'],
'email' => ['type' => 'string', 'definition' => 'unique|nullable'],
'type' => ['type' => 'char:8', 'definition' => 'unique|nullable'],
'image_url' => ['type' => 'string', 'definition' => 'unique|nullable'],
'phone' => ['type' => 'string', 'definition' => 'nullable'],
'enum_str' => ['type' => 'enum:1,2,3']
'name' => ['type' => 'string', 'definition' => 'default:test2'],
'email' => ['type' => 'string', 'definition' => 'unique|nullable'],
'type' => ['type' => 'char:8', 'definition' => 'unique|nullable'],
'image_url' => ['type' => 'string', 'definition' => 'unique|nullable'],
'phone' => ['type' => 'string', 'definition' => 'nullable'],
'enum_str' => ['type' => 'enum:1,2,3'],
'morph_text' => ['type' => 'morphs'],
'nullable_morph_test' => ['type' => 'nullableMorphs']
]
];

Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Processors/RequestProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function test_output_str()
'image_url' => 'string|unique:units|nullable',
'phone' => 'string|nullable',
'enum_str' => 'required',
STR;
$expected_str = preg_replace("/\r/", "", $expected_str);
$this->assertEquals($expected_str, $output_str);
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/Processors/ResourceProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Yepwoo\Laragine\Tests\Unit\Processors;

use Yepwoo\Laragine\Logic\StringManipulator;
use Yepwoo\Laragine\Processors\Processor;
use Yepwoo\Laragine\Processors\ResourceProcessor;

class ResourceProcessorTest extends ProcessorTestCase
Expand All @@ -19,6 +17,7 @@ public function test_output_str()
'image_url' => \$this->image_url,
'phone' => \$this->phone,
'enum_str' => \$this->enum_str,
STR;
$expected_str = preg_replace("/\r/", "", $expected_str);
$this->assertEquals($expected_str, $output_str);
Expand Down

0 comments on commit 6d283fb

Please sign in to comment.