Compare commits

...

13 Commits
master ... 1.x

Author SHA1 Message Date
Nafies Luthfi 7b4e0fb05f
Merge pull request #36 from nafiesl/33_bug_model_overrides 5 years ago
Nafies Luthfi fa601fa4ae Fix failed test 5 years ago
Nafies Luthfi ac2e69b2c8 Fix failed test 5 years ago
Nafies Luthfi e306d5d4b2 Add console warn for the extsting classes 5 years ago
Nafies Luthfi ab4aa6e65a Remove unused lines 5 years ago
Nafies Luthfi 3add51f754 Prevent overriding the existing model factory class 5 years ago
Nafies Luthfi 6cc0368c20 Prevent overriding the existing model policy class 5 years ago
Nafies Luthfi bcc7d9f4b3 Prevent overriding the existing model class 5 years ago
Nafies Luthfi ae4879f7b7
Merge pull request #31 from nafiesl/1x_publish_stub_files 5 years ago
Nafies Luthfi 0556252d69 Update readme.md file 5 years ago
Nafies Luthfi b4e7d5faef Add test for published stubs usage 5 years ago
Nafies Luthfi 6ae4d7a98d Use published stub file if it exists 5 years ago
Nafies Luthfi 6c5bdde99a Add stubs publisher script and 5 years ago
  1. 14
      readme.md
  2. 6
      src/Generators/BaseGenerator.php
  3. 8
      src/Generators/ModelFactoryGenerator.php
  4. 11
      src/Generators/ModelGenerator.php
  5. 11
      src/Generators/ModelPolicyGenerator.php
  6. 4
      src/ServiceProvider.php
  7. 66
      tests/Generators/ModelFactoryGeneratorTest.php
  8. 24
      tests/Generators/ModelGeneratorTest.php
  9. 35
      tests/Generators/ModelPolicyGeneratorTest.php
  10. 25
      tests/stubs/database/factories/model-factory.stub

14
readme.md

@ -218,7 +218,7 @@ The generated functional tests will give you examples of how to adapt this code
You can configure your own by publishing the config file:
```bash
$ php artisan vendor:publish --provider="Luthfi\CrudGenerator\ServiceProvider"
$ php artisan vendor:publish --provider="Luthfi\CrudGenerator\ServiceProvider" --tag=config
```
That will generate `config/simple-crud.php` file.
@ -242,6 +242,18 @@ return [
<br>
## Publishing Stub Files
Stub files is the templates which we use to generate the code for each model classes and files. We can customize the stub files as we needed by publishing them to our project directory.
```bash
$ php artisan vendor:publish --provider="Luthfi\CrudGenerator\ServiceProvider" --tag=stubs
```
That will generate stub files on `stubs/simple-crud` directory. Now we can change some stub files based on our project needs.
<br>
## Attention
- The package will creates the **Model** class file, the command will stop if the **Model already exists**.

6
src/Generators/BaseGenerator.php

@ -116,6 +116,12 @@ abstract class BaseGenerator implements GeneratorContract
*/
protected function getStubFileContent(string $stubName)
{
$publishedStubPath = base_path('stubs/simple-crud/'.$stubName.'.stub');
if (is_file($publishedStubPath)) {
return $this->files->get($publishedStubPath);
}
return $this->files->get(__DIR__.'/../stubs/'.$stubName.'.stub');
}

8
src/Generators/ModelFactoryGenerator.php

@ -13,9 +13,15 @@ class ModelFactoryGenerator extends BaseGenerator
public function generate(string $type = 'full')
{
$modelFactoryPath = $this->makeDirectory(database_path('factories'));
$modelFactoryClassPath = $modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php';
if ($this->files->exists($modelFactoryClassPath)) {
$this->command->warn('Use the existing '.$this->modelNames['model_name'].' model factory.');
return;
}
$this->generateFile(
$modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php',
$modelFactoryClassPath,
$this->getContent('database/factories/model-factory')
);

11
src/Generators/ModelGenerator.php

@ -14,11 +14,14 @@ class ModelGenerator extends BaseGenerator
{
$modelPath = $this->modelNames['model_path'];
$modelDirectory = $this->makeDirectory(app_path($modelPath));
$modelClassPath = $modelDirectory.'/'.$this->modelNames['model_name'].'.php';
$this->generateFile(
$modelDirectory.'/'.$this->modelNames['model_name'].'.php',
$this->getContent('models/model')
);
if ($this->files->exists($modelClassPath)) {
$this->command->warn('Use the existing '.$this->modelNames['model_name'].' model.');
return;
}
$this->generateFile($modelClassPath, $this->getContent('models/model'));
$this->command->info($this->modelNames['model_name'].' model generated.');
}

11
src/Generators/ModelPolicyGenerator.php

@ -17,11 +17,14 @@ class ModelPolicyGenerator extends BaseGenerator
$parentDirectory = '/'.$this->command->option('parent');
}
$modelPolicyPath = $this->makeDirectory(app_path('Policies'.$parentDirectory));
$modelPolicyClassPath = $modelPolicyPath.'/'.$this->modelNames['model_name'].'Policy.php';
$this->generateFile(
$modelPolicyPath.'/'.$this->modelNames['model_name'].'Policy.php',
$this->getContent('models/model-policy')
);
if ($this->files->exists($modelPolicyClassPath)) {
$this->command->warn('Use the existing '.$this->modelNames['model_name'].' model policy.');
return;
}
$this->generateFile($modelPolicyClassPath, $this->getContent('models/model-policy'));
$this->command->info($this->modelNames['model_name'].' model policy generated.');

4
src/ServiceProvider.php

@ -27,5 +27,9 @@ class ServiceProvider extends BaseServiceProvider
$this->publishes([
__DIR__.'/config.php' => config_path('simple-crud.php'),
], 'config');
$this->publishes([
__DIR__.'/stubs' => base_path('stubs/simple-crud'),
], 'stubs');
}
}

66
tests/Generators/ModelFactoryGeneratorTest.php

@ -32,4 +32,70 @@ use Faker\Generator as Faker;
";
$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);
app('files')->copy(
__DIR__.'/../stubs/database/factories/model-factory.stub',
base_path('stubs/simple-crud/database/factories/model-factory.stub')
);
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$modelFactoryPath = database_path('factories/'.$this->model_name.'Factory.php');
$this->assertFileExists($modelFactoryPath);
$modelFactoryContent = "<?php
declare(strict_types = 1);
namespace Database\Factories;
use App\Models\User;
use {$this->full_model_name};
use Illuminate\Database\Eloquent\Factories\Factory;
class {$this->model_name}Factory extends Factory
{
protected \$model = {$this->model_name}::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));
$this->removeFileOrDir(base_path('stubs'));
}
/** @test */
public function it_doesnt_override_the_existing_model_factory_content()
{
$this->artisan('make:factory', ['name' => $this->model_name.'Factory', '--no-interaction' => true]);
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$modelFactoryPath = database_path('factories/'.$this->model_name.'Factory.php');
$this->assertFileExists($modelFactoryPath);
$modelFactoryContent = "<?php
/* @var \$factory \Illuminate\Database\Eloquent\Factory */
use App\Model;
use Faker\Generator as Faker;
\$factory->define(Model::class, function (Faker \$faker) {
return [
//
];
});
";
$this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath));
}
}

24
tests/Generators/ModelGeneratorTest.php

@ -89,4 +89,28 @@ class Category extends Model
$this->removeFileOrDir(resource_path('views/categories'));
$this->removeFileOrDir(resource_path("lang/en/category.php"));
}
/** @test */
public function it_doesnt_override_the_existing_model()
{
$this->mockConsoleOutput = true;
$this->artisan('make:model', ['name' => $this->model_name, '--no-interaction' => true]);
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true])
->expectsQuestion('Model file exists, are you sure to generate CRUD files?', true);
$modelPath = app_path($this->model_name.'.php');
$this->assertFileExists($modelPath);
$modelClassContent = "<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class {$this->model_name} extends Model
{
//
}
";
$this->assertEquals($modelClassContent, file_get_contents($modelPath));
}
}

35
tests/Generators/ModelPolicyGeneratorTest.php

@ -229,4 +229,39 @@ class AuthServiceProvider extends ServiceProvider
";
$this->assertEquals($authSPContent, file_get_contents($authSPPath));
}
/** @test */
public function it_doesnt_override_the_existing_model_policy_content()
{
$userModel = config('auth.providers.users.model');
$this->artisan('make:policy', ['name' => $this->model_name.'Policy', '--no-interaction' => true]);
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$modelPolicyPath = app_path('Policies/'.$this->model_name.'Policy.php');
$this->assertFileExists($modelPolicyPath);
$modelPolicyContent = "<?php
namespace App\Policies;
use {$userModel};
use Illuminate\Auth\Access\HandlesAuthorization;
class {$this->model_name}Policy
{
use HandlesAuthorization;
/**
* Create a new policy instance.
*
* @return void
*/
public function __construct()
{
//
}
}
";
$this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath));
}
}

25
tests/stubs/database/factories/model-factory.stub

@ -0,0 +1,25 @@
<?php
declare(strict_types = 1);
namespace Database\Factories;
use App\Models\User;
use fullMstr;
use Illuminate\Database\Eloquent\Factories\Factory;
class MasterFactory extends Factory
{
protected $model = Master::class;
public function definition()
{
return [
'title' => $this->faker->word,
'description' => $this->faker->sentence,
'creator_id' => function () {
return User::factory()->create()->id;
},
];
}
}
Loading…
Cancel
Save