diff --git a/src/Generators/RepositoryGenerator.php b/src/Generators/RepositoryGenerator.php index a046397f..b20b3871 100644 --- a/src/Generators/RepositoryGenerator.php +++ b/src/Generators/RepositoryGenerator.php @@ -12,7 +12,7 @@ public function generate(): void if (!$this->classExists('models', $this->model)) { $this->throwFailureException( ClassNotExistsException::class, - "Cannot create {$this->model} Model cause {$this->model} Model does not exists.", + "Cannot create {$this->model}Repository cause {$this->model} Model does not exists.", "Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'." ); } diff --git a/tests/FactoryGeneratorTest.php b/tests/FactoryGeneratorTest.php index b46b5eb5..82cb2201 100644 --- a/tests/FactoryGeneratorTest.php +++ b/tests/FactoryGeneratorTest.php @@ -69,7 +69,6 @@ className: ViewException::class, ]) ->setModel('Post') ->generate(); - } public function testCreateSuccess() diff --git a/tests/RepositoryGeneratorTest.php b/tests/RepositoryGeneratorTest.php new file mode 100644 index 00000000..14cb0452 --- /dev/null +++ b/tests/RepositoryGeneratorTest.php @@ -0,0 +1,67 @@ +mockClass(RepositoryGenerator::class, [ + $this->classExistsMethodCall(['models', 'Post'], false), + ]); + + $this->assertExceptionThrew( + className: ClassNotExistsException::class, + message: "Cannot create PostRepository cause Post Model does not exists. " + . "Create a Post Model by himself or run command 'php artisan make:entity Post --only-model'.", + ); + + app(RepositoryGenerator::class) + ->setModel('Post') + ->generate(); + + $this->assertFileDoesNotExist('repository.php', 'app/Repositories/PostRepository.php'); + } + + public function testCreateRepository() + { + $this->mockFilesystem(); + + app(RepositoryGenerator::class) + ->setModel('Post') + ->generate(); + + $this->assertGeneratedFileEquals('repository.php', 'app/Repositories/PostRepository.php'); + + $this->assertEventPushed( + className: SuccessCreateMessage::class, + message: 'Created a new Repository: PostRepository', + ); + } + + public function testCreateRepositoryStubNotExist() + { + config(['entity-generator.stubs.repository' => 'incorrect_stub']); + + $this->mockFilesystem(); + + app(RepositoryGenerator::class) + ->setModel('Post') + ->generate(); + + $this->assertFileDoesNotExist('repository.php', 'app/Repositories/PostRepository.php'); + + $this->assertEventPushed( + className: WarningEvent::class, + message: 'Generation of repository has been skipped cause the view incorrect_stub from the config entity-generator.stubs.repository is not exists. Please check that config has the correct view name value.', + ); + } +} diff --git a/tests/Support/Repository/RepositoryMockTrait.php b/tests/Support/Repository/RepositoryMockTrait.php new file mode 100644 index 00000000..9438cfd4 --- /dev/null +++ b/tests/Support/Repository/RepositoryMockTrait.php @@ -0,0 +1,22 @@ +models = [ + 'Post.php' => $this->mockPhpFileContent(), + ]; + + $fileSystemMock->setStructure(); + } +} diff --git a/tests/fixtures/RepositoryGeneratorTest/repository.php b/tests/fixtures/RepositoryGeneratorTest/repository.php new file mode 100644 index 00000000..1e635191 --- /dev/null +++ b/tests/fixtures/RepositoryGeneratorTest/repository.php @@ -0,0 +1,17 @@ +setModel(Post::class); + } +}