Skip to content

Commit 74c273d

Browse files
authored
Merge pull request #77 from RonasIT/49-add-service-generator-tests
Add service generator tests
2 parents 926bbfb + 20d923d commit 74c273d

File tree

5 files changed

+207
-8
lines changed

5 files changed

+207
-8
lines changed

src/Generators/ServiceGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public function generate(): void
2929

3030
if (!$this->classExists('models', $this->model)) {
3131
$this->throwFailureException(
32-
ClassNotExistsException::class,
33-
"Cannot create {$this->model} Model cause {$this->model} Model does not exists.",
34-
"Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'."
32+
exceptionClass: ClassNotExistsException::class,
33+
failureMessage: "Cannot create {$this->model}Service cause {$this->model} Model does not exists.",
34+
recommendedMessage: "Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'.",
3535
);
3636
}
3737
}

stubs/service_with_trait.blade.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace {{$namespace}};
22

33
use {{$modelsNamespace}}\{{$entity}};
4+
use Illuminate\Pagination\LengthAwarePaginator;
45
use RonasIT\Support\Traits\EntityControlTrait;
56
{{--
67
Laravel inserts two spaces between @property and type, so we are forced
@@ -23,15 +24,16 @@ public function __construct()
2324
$this->setModel({{$entity}}::class);
2425
}
2526

26-
public function search($filters)
27+
public function search(array $filters = []): LengthAwarePaginator
2728
{
28-
return $this->searchQuery($filters)
29+
return $this
30+
->searchQuery($filters)
2931
@foreach($fields['simple_search'] as $field)
30-
->filterBy('{{$field}}')
32+
->filterBy('{{$field}}')
3133
@endforeach
3234
@if(!empty($fields['search_by_query']))
33-
->filterByQuery(['{!! implode('\', \'', $fields['search_by_query']) !!}'])
35+
->filterByQuery(['{!! implode('\', \'', $fields['search_by_query']) !!}'])
3436
@endif
35-
->getSearchResults();
37+
->getSearchResults();
3638
}
3739
}

tests/ServiceGeneratorTest.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests;
4+
5+
use Illuminate\Support\Facades\Event;
6+
use RonasIT\Support\Events\SuccessCreateMessage;
7+
use RonasIT\Support\Events\WarningEvent;
8+
use RonasIT\Support\Exceptions\ClassNotExistsException;
9+
use RonasIT\Support\Generators\ServiceGenerator;
10+
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
11+
12+
class ServiceGeneratorTest extends TestCase
13+
{
14+
use GeneratorMockTrait;
15+
16+
public function testMissingModel()
17+
{
18+
$this->mockClass(ServiceGenerator::class, [
19+
$this->classExistsMethodCall(['repositories', 'PostRepository'], false),
20+
$this->classExistsMethodCall(['models', 'Post'], false),
21+
]);
22+
23+
$this->assertExceptionThrew(
24+
className: ClassNotExistsException::class,
25+
message: 'Cannot create PostService cause Post Model does not exists. '
26+
. "Create a Post Model by himself or run command 'php artisan make:entity Post --only-model'",
27+
);
28+
29+
app(ServiceGenerator::class)
30+
->setModel('Post')
31+
->generate();
32+
33+
Event::assertNothingDispatched();
34+
}
35+
36+
public function testCreateWithTrait()
37+
{
38+
$this->mockClass(ServiceGenerator::class, [
39+
$this->classExistsMethodCall(['repositories', 'PostRepository'], false),
40+
$this->classExistsMethodCall(['models', 'Post']),
41+
]);
42+
43+
app(ServiceGenerator::class)
44+
->setRelations([
45+
'hasOne' => [],
46+
'belongsTo' => ['User'],
47+
'hasMany' => ['Comment'],
48+
'belongsToMany' => []
49+
])
50+
->setFields([
51+
'integer-required' => ['media_id'],
52+
'string-required' => ['body'],
53+
'string' => ['title']
54+
])
55+
->setModel('Post')
56+
->generate();
57+
58+
$this->assertGeneratedFileEquals('service_with_trait.php', 'app/Services/PostService.php');
59+
60+
$this->assertEventPushed(
61+
className: SuccessCreateMessage::class,
62+
message: 'Created a new Service: PostService',
63+
);
64+
}
65+
66+
public function testCreateWithTraitStubNotExist()
67+
{
68+
config(['entity-generator.stubs.service_with_trait' => 'incorrect_stub']);
69+
70+
$this->mockClass(ServiceGenerator::class, [
71+
$this->classExistsMethodCall(['repositories', 'PostRepository'], false),
72+
$this->classExistsMethodCall(['models', 'Post']),
73+
]);
74+
75+
app(ServiceGenerator::class)
76+
->setFields([])
77+
->setModel('Post')
78+
->generate();
79+
80+
$this->assertFileDoesNotExist('app/Services/PostService.php');
81+
82+
$this->assertEventPushed(
83+
className: WarningEvent::class,
84+
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.',
85+
);
86+
}
87+
88+
public function testCreateWithoutTrait()
89+
{
90+
$this->mockClass(ServiceGenerator::class, [
91+
$this->classExistsMethodCall(['repositories', 'PostRepository']),
92+
]);
93+
94+
app(ServiceGenerator::class)
95+
->setRelations([
96+
'hasOne' => [],
97+
'belongsTo' => ['User'],
98+
'hasMany' => ['Comment'],
99+
'belongsToMany' => []
100+
])
101+
->setFields([
102+
'integer-required' => ['media_id'],
103+
'string-required' => ['body'],
104+
'string' => ['title']
105+
])
106+
->setModel('Post')
107+
->generate();
108+
109+
$this->assertGeneratedFileEquals('service_without_trait.php', 'app/Services/PostService.php');
110+
111+
$this->assertEventPushed(
112+
className: SuccessCreateMessage::class,
113+
message: 'Created a new Service: PostService',
114+
);
115+
}
116+
117+
public function testCreateWithoutTraitStubNotExist()
118+
{
119+
config(['entity-generator.stubs.service' => 'incorrect_stub']);
120+
121+
$this->mockClass(ServiceGenerator::class, [
122+
$this->classExistsMethodCall(['repositories', 'PostRepository']),
123+
]);
124+
125+
app(ServiceGenerator::class)
126+
->setFields([])
127+
->setModel('Post')
128+
->generate();
129+
130+
$this->assertFileDoesNotExist('app/Services/PostService.php');
131+
132+
$this->assertEventPushed(
133+
className: WarningEvent::class,
134+
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.',
135+
);
136+
}
137+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use App\Models\Post;
6+
use Illuminate\Pagination\LengthAwarePaginator;
7+
use RonasIT\Support\Traits\EntityControlTrait;
8+
9+
/**
10+
* @property Post $model
11+
*/
12+
class PostService
13+
{
14+
use EntityControlTrait;
15+
16+
public function __construct()
17+
{
18+
$this->setModel(Post::class);
19+
}
20+
21+
public function search(array $filters = []): LengthAwarePaginator
22+
{
23+
return $this
24+
->searchQuery($filters)
25+
->filterBy('media_id')
26+
->filterByQuery(['title', 'body'])
27+
->getSearchResults();
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use App\Repositories\PostRepository;
6+
use Illuminate\Support\Arr;
7+
use RonasIT\Support\Services\EntityService;
8+
use Illuminate\Pagination\LengthAwarePaginator;
9+
10+
/**
11+
* @mixin PostRepository
12+
* @property PostRepository $repository
13+
*/
14+
class PostService extends EntityService
15+
{
16+
public function __construct()
17+
{
18+
$this->setRepository(PostRepository::class);
19+
}
20+
21+
public function search(array $filters = []): LengthAwarePaginator
22+
{
23+
return $this
24+
->with(Arr::get($filters, 'with', []))
25+
->withCount(Arr::get($filters, 'with_count', []))
26+
->searchQuery($filters)
27+
->filterBy('media_id')
28+
->filterByQuery(['title', 'body'])
29+
->getSearchResults();
30+
}
31+
}

0 commit comments

Comments
 (0)