Browse Source

Merge pull request #40 from nafiesl/39_db_factory_namespace_issue

Resolving DB Factory Namespace Issue
tags/2.3.1 2.3.1
Nafies Luthfi 5 years ago
committed by GitHub
parent
commit
cf4be00941
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      src/Generators/ModelFactoryGenerator.php
  2. 4
      tests/CrudApiMakeCommandTest.php
  3. 4
      tests/CrudMakeCommandTest.php
  4. 4
      tests/CrudSimpleMakeCommandTest.php
  5. 44
      tests/Generators/ModelFactoryGeneratorTest.php

12
src/Generators/ModelFactoryGenerator.php

@ -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)) {
@ -47,6 +51,12 @@ class ModelFactoryGenerator extends BaseGenerator
$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
tests/CrudApiMakeCommandTest.php

@ -115,7 +115,7 @@ class CrudApiMakeCommandTest extends TestCase
$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"));
@ -148,7 +148,7 @@ class CrudApiMakeCommandTest extends TestCase
$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"));

4
tests/CrudMakeCommandTest.php

@ -128,7 +128,7 @@ class CrudMakeCommandTest extends TestCase
$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"));
@ -163,7 +163,7 @@ class CrudMakeCommandTest extends TestCase
$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"));

4
tests/CrudSimpleMakeCommandTest.php

@ -121,7 +121,7 @@ class CrudSimpleMakeCommandTest extends TestCase
$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"));
@ -154,7 +154,7 @@ class CrudSimpleMakeCommandTest extends TestCase
$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"));

44
tests/Generators/ModelFactoryGeneratorTest.php

@ -41,6 +41,50 @@ class {$this->model_name}Factory extends Factory
}
/** @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()
{
app('files')->makeDirectory(base_path('stubs/simple-crud/database/factories'), 0777, true, true);

Loading…
Cancel
Save