From 22f2bd6c166d3597f1e2ff9be161cf493dfde5b6 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Wed, 27 Jan 2021 05:59:03 +0800 Subject: [PATCH] Add test for published stubs usage --- tests/Generators/ModelFactoryGeneratorTest.php | 42 +++++++++++++++++++++++ tests/stubs/database/factories/model-factory.stub | 25 ++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 tests/stubs/database/factories/model-factory.stub diff --git a/tests/Generators/ModelFactoryGeneratorTest.php b/tests/Generators/ModelFactoryGeneratorTest.php index 377fffd..0d2fa30 100644 --- a/tests/Generators/ModelFactoryGeneratorTest.php +++ b/tests/Generators/ModelFactoryGeneratorTest.php @@ -39,4 +39,46 @@ class {$this->model_name}Factory extends Factory "; $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 = "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')); + } } diff --git a/tests/stubs/database/factories/model-factory.stub b/tests/stubs/database/factories/model-factory.stub new file mode 100644 index 0000000..23c4374 --- /dev/null +++ b/tests/stubs/database/factories/model-factory.stub @@ -0,0 +1,25 @@ + $this->faker->word, + 'description' => $this->faker->sentence, + 'creator_id' => function () { + return User::factory()->create()->id; + }, + ]; + } +}