diff --git a/src/Generators/ServiceGenerator.php b/src/Generators/ServiceGenerator.php index df126329..6da60c96 100644 --- a/src/Generators/ServiceGenerator.php +++ b/src/Generators/ServiceGenerator.php @@ -29,9 +29,9 @@ 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.", - "Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'." + exceptionClass: ClassNotExistsException::class, + failureMessage: "Cannot create {$this->model}Service cause {$this->model} Model does not exists.", + recommendedMessage: "Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'.", ); } } diff --git a/stubs/service_with_trait.blade.php b/stubs/service_with_trait.blade.php index 0cc37d96..2503902f 100644 --- a/stubs/service_with_trait.blade.php +++ b/stubs/service_with_trait.blade.php @@ -1,6 +1,7 @@ namespace {{$namespace}}; use {{$modelsNamespace}}\{{$entity}}; +use Illuminate\Pagination\LengthAwarePaginator; use RonasIT\Support\Traits\EntityControlTrait; {{-- Laravel inserts two spaces between @property and type, so we are forced @@ -23,15 +24,16 @@ public function __construct() $this->setModel({{$entity}}::class); } - public function search($filters) + public function search(array $filters = []): LengthAwarePaginator { - return $this->searchQuery($filters) + return $this + ->searchQuery($filters) @foreach($fields['simple_search'] as $field) - ->filterBy('{{$field}}') + ->filterBy('{{$field}}') @endforeach @if(!empty($fields['search_by_query'])) - ->filterByQuery(['{!! implode('\', \'', $fields['search_by_query']) !!}']) + ->filterByQuery(['{!! implode('\', \'', $fields['search_by_query']) !!}']) @endif - ->getSearchResults(); + ->getSearchResults(); } } diff --git a/tests/ServiceGeneratorTest.php b/tests/ServiceGeneratorTest.php new file mode 100644 index 00000000..02091594 --- /dev/null +++ b/tests/ServiceGeneratorTest.php @@ -0,0 +1,137 @@ +mockClass(ServiceGenerator::class, [ + $this->classExistsMethodCall(['repositories', 'PostRepository'], false), + $this->classExistsMethodCall(['models', 'Post'], false), + ]); + + $this->assertExceptionThrew( + className: ClassNotExistsException::class, + message: 'Cannot create PostService cause Post Model does not exists. ' + . "Create a Post Model by himself or run command 'php artisan make:entity Post --only-model'", + ); + + app(ServiceGenerator::class) + ->setModel('Post') + ->generate(); + + Event::assertNothingDispatched(); + } + + public function testCreateWithTrait() + { + $this->mockClass(ServiceGenerator::class, [ + $this->classExistsMethodCall(['repositories', 'PostRepository'], false), + $this->classExistsMethodCall(['models', 'Post']), + ]); + + app(ServiceGenerator::class) + ->setRelations([ + 'hasOne' => [], + 'belongsTo' => ['User'], + 'hasMany' => ['Comment'], + 'belongsToMany' => [] + ]) + ->setFields([ + 'integer-required' => ['media_id'], + 'string-required' => ['body'], + 'string' => ['title'] + ]) + ->setModel('Post') + ->generate(); + + $this->assertGeneratedFileEquals('service_with_trait.php', 'app/Services/PostService.php'); + + $this->assertEventPushed( + className: SuccessCreateMessage::class, + message: 'Created a new Service: PostService', + ); + } + + public function testCreateWithTraitStubNotExist() + { + config(['entity-generator.stubs.service_with_trait' => 'incorrect_stub']); + + $this->mockClass(ServiceGenerator::class, [ + $this->classExistsMethodCall(['repositories', 'PostRepository'], false), + $this->classExistsMethodCall(['models', 'Post']), + ]); + + app(ServiceGenerator::class) + ->setFields([]) + ->setModel('Post') + ->generate(); + + $this->assertFileDoesNotExist('app/Services/PostService.php'); + + $this->assertEventPushed( + className: WarningEvent::class, + message: 'Generation of service with trait has been skipped cause the view incorrect_stub from the config entity-generator.stubs.service_with_trait is not exists. Please check that config has the correct view name value.', + ); + } + + public function testCreateWithoutTrait() + { + $this->mockClass(ServiceGenerator::class, [ + $this->classExistsMethodCall(['repositories', 'PostRepository']), + ]); + + app(ServiceGenerator::class) + ->setRelations([ + 'hasOne' => [], + 'belongsTo' => ['User'], + 'hasMany' => ['Comment'], + 'belongsToMany' => [] + ]) + ->setFields([ + 'integer-required' => ['media_id'], + 'string-required' => ['body'], + 'string' => ['title'] + ]) + ->setModel('Post') + ->generate(); + + $this->assertGeneratedFileEquals('service_without_trait.php', 'app/Services/PostService.php'); + + $this->assertEventPushed( + className: SuccessCreateMessage::class, + message: 'Created a new Service: PostService', + ); + } + + public function testCreateWithoutTraitStubNotExist() + { + config(['entity-generator.stubs.service' => 'incorrect_stub']); + + $this->mockClass(ServiceGenerator::class, [ + $this->classExistsMethodCall(['repositories', 'PostRepository']), + ]); + + app(ServiceGenerator::class) + ->setFields([]) + ->setModel('Post') + ->generate(); + + $this->assertFileDoesNotExist('app/Services/PostService.php'); + + $this->assertEventPushed( + className: WarningEvent::class, + message: 'Generation of service has been skipped cause the view incorrect_stub from the config entity-generator.stubs.service is not exists. Please check that config has the correct view name value.', + ); + } +} diff --git a/tests/fixtures/ServiceGeneratorTest/service_with_trait.php b/tests/fixtures/ServiceGeneratorTest/service_with_trait.php new file mode 100644 index 00000000..3fc2aee5 --- /dev/null +++ b/tests/fixtures/ServiceGeneratorTest/service_with_trait.php @@ -0,0 +1,29 @@ +setModel(Post::class); + } + + public function search(array $filters = []): LengthAwarePaginator + { + return $this + ->searchQuery($filters) + ->filterBy('media_id') + ->filterByQuery(['title', 'body']) + ->getSearchResults(); + } +} diff --git a/tests/fixtures/ServiceGeneratorTest/service_without_trait.php b/tests/fixtures/ServiceGeneratorTest/service_without_trait.php new file mode 100644 index 00000000..6a829400 --- /dev/null +++ b/tests/fixtures/ServiceGeneratorTest/service_without_trait.php @@ -0,0 +1,31 @@ +setRepository(PostRepository::class); + } + + public function search(array $filters = []): LengthAwarePaginator + { + return $this + ->with(Arr::get($filters, 'with', [])) + ->withCount(Arr::get($filters, 'with_count', [])) + ->searchQuery($filters) + ->filterBy('media_id') + ->filterByQuery(['title', 'body']) + ->getSearchResults(); + } +}