Skip to content

Commit

Permalink
Merge pull request #40 from nafiesl/39_db_factory_namespace_issue
Browse files Browse the repository at this point in the history
Resolving DB Factory Namespace Issue
  • Loading branch information
nafiesl authored Apr 20, 2021
2 parents 16ccf65 + bb77d92 commit cf4be00
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/Generators/ModelFactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ class ModelFactoryGenerator extends BaseGenerator
*/
public function generate(string $type = 'full')
{
$modelFactoryPath = $this->makeDirectory(database_path('factories'));
$modelFactoryPath = database_path('factories');
if ($this->modelNames['model_path'] != 'Models') {
$modelFactoryPath .= '/'.$this->modelNames['model_path'];
}
$modelFactoryPath = $this->makeDirectory($modelFactoryPath);
$modelFactoryClassPath = $modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php';

if ($this->files->exists($modelFactoryClassPath)) {
Expand Down Expand Up @@ -47,6 +51,12 @@ public function getContent(string $stubName)
$modelFactoryFileContent = str_replace($string, $replacement, $modelFactoryFileContent);
}

if ($this->modelNames['model_path'] != 'Models') {
$string = 'Database\Factories';
$replacement = $string.'\\'.str_replace('/', '\\', $this->modelNames['model_path']);
$modelFactoryFileContent = str_replace($string, $replacement, $modelFactoryFileContent);
}

return $this->replaceStubString($modelFactoryFileContent);
}
}
4 changes: 2 additions & 2 deletions tests/CrudApiMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function it_can_generate_crud_files_for_namespaced_model()
$this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php"));

$this->assertFileExists(app_path("Policies/{$modelName}Policy.php"));
$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(database_path("factories/{$inputName}Factory.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Policies/{$modelName}PolicyTest.php"));
$this->assertFileExists(base_path("tests/Feature/Api/Manage{$modelName}Test.php"));
Expand Down Expand Up @@ -148,7 +148,7 @@ public function it_can_generate_crud_files_with_parent_option()
$localeConfig = config('app.locale');
$this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php"));

$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(database_path("factories/{$inputName}Factory.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Policies/{$modelName}PolicyTest.php"));
$this->assertFileExists(app_path("Policies/{$parentName}/{$modelName}Policy.php"));
Expand Down
4 changes: 2 additions & 2 deletions tests/CrudMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function it_can_generate_crud_files_for_namespaced_model()
$this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php"));

$this->assertFileExists(app_path("Policies/{$modelName}Policy.php"));
$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(database_path("factories/{$inputName}Factory.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Policies/{$modelName}PolicyTest.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$modelName}Test.php"));
Expand Down Expand Up @@ -163,7 +163,7 @@ public function it_can_generate_crud_files_with_parent_option()
$localeConfig = config('app.locale');
$this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php"));

$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(database_path("factories/{$inputName}Factory.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Policies/{$modelName}PolicyTest.php"));
$this->assertFileExists(app_path("Policies/{$parentName}/{$modelName}Policy.php"));
Expand Down
4 changes: 2 additions & 2 deletions tests/CrudSimpleMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function it_can_generate_crud_files_for_namespaced_model()
$this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php"));

$this->assertFileExists(app_path("Policies/{$modelName}Policy.php"));
$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(database_path("factories/{$inputName}Factory.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Policies/{$modelName}PolicyTest.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$modelName}Test.php"));
Expand Down Expand Up @@ -154,7 +154,7 @@ public function it_can_generate_crud_files_with_parent_option()
$localeConfig = config('app.locale');
$this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php"));

$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(database_path("factories/{$inputName}Factory.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Policies/{$modelName}PolicyTest.php"));
$this->assertFileExists(app_path("Policies/{$parentName}/{$modelName}Policy.php"));
Expand Down
44 changes: 44 additions & 0 deletions tests/Generators/ModelFactoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,50 @@ public function definition()
$this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath));
}

/** @test */
public function it_creates_correct_model_factory_content_with_namespaced_model()
{
$inputName = 'Entities/References/Category';
$modelName = 'Category';
$fullModelName = 'App\Entities\References\Category';
$pluralModelName = 'Categories';
$tableName = 'categories';
$langName = 'category';
$modelPath = 'Entities/References';
$factoryNamespace = 'Entities\References';

$this->artisan('make:crud-api', ['name' => $inputName, '--no-interaction' => true]);

$modelFactoryPath = database_path('factories/'.$inputName.'Factory.php');
$this->assertFileExists($modelFactoryPath);
$modelFactoryContent = "<?php
namespace Database\Factories\\{$factoryNamespace};
use App\Models\User;
use {$fullModelName};
use Illuminate\Database\Eloquent\Factories\Factory;
class {$modelName}Factory extends Factory
{
protected \$model = {$modelName}::class;
public function definition()
{
return [
'title' => \$this->faker->word,
'description' => \$this->faker->sentence,
'creator_id' => function () {
return User::factory()->create()->id;
},
];
}
}
";
$this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath));
}


/** @test */
public function it_creates_model_factory_file_content_from_published_stub()
{
Expand Down

0 comments on commit cf4be00

Please sign in to comment.