Skip to content
6 changes: 3 additions & 3 deletions src/Generators/ServiceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'.",
);
}
}
Expand Down
12 changes: 7 additions & 5 deletions stubs/service_with_trait.blade.php
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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();
}
}
137 changes: 137 additions & 0 deletions tests/ServiceGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace RonasIT\Support\Tests;

use Illuminate\Support\Facades\Event;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Generators\ServiceGenerator;
use RonasIT\Support\Tests\Support\GeneratorMockTrait;

class ServiceGeneratorTest extends TestCase
{
use GeneratorMockTrait;

public function testMissingModel()
{
$this->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.',
);
}
}
29 changes: 29 additions & 0 deletions tests/fixtures/ServiceGeneratorTest/service_with_trait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Services;

use App\Models\Post;
use Illuminate\Pagination\LengthAwarePaginator;
use RonasIT\Support\Traits\EntityControlTrait;

/**
* @property Post $model
*/
class PostService
{
use EntityControlTrait;

public function __construct()
{
$this->setModel(Post::class);
}

public function search(array $filters = []): LengthAwarePaginator
{
return $this
->searchQuery($filters)
->filterBy('media_id')
->filterByQuery(['title', 'body'])
->getSearchResults();
}
}
31 changes: 31 additions & 0 deletions tests/fixtures/ServiceGeneratorTest/service_without_trait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Services;

use App\Repositories\PostRepository;
use Illuminate\Support\Arr;
use RonasIT\Support\Services\EntityService;
use Illuminate\Pagination\LengthAwarePaginator;

/**
* @mixin PostRepository
* @property PostRepository $repository
*/
class PostService extends EntityService
{
public function __construct()
{
$this->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();
}
}
Loading