Skip to content

Commit 6bdd479

Browse files
authored
Merge pull request #192 from RonasIT/188_Remove-service_with_trait-logic-generation
feat: Remove service_with_trait logic generation
2 parents 59bfe87 + aae7b47 commit 6bdd479

File tree

6 files changed

+15
-142
lines changed

6 files changed

+15
-142
lines changed

src/Commands/MakeEntityCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class MakeEntityCommand extends Command
9090
protected $rules = [
9191
'only' => [
9292
'only-api' => [ResourceGenerator::class, ControllerGenerator::class, RequestsGenerator::class, TestsGenerator::class],
93-
'only-entity' => [MigrationGenerator::class, ModelGenerator::class, ServiceGenerator::class, RepositoryGenerator::class, FactoryGenerator::class, SeederGenerator::class],
93+
'only-entity' => [MigrationGenerator::class, ModelGenerator::class, RepositoryGenerator::class, ServiceGenerator::class, FactoryGenerator::class, SeederGenerator::class],
9494
'only-model' => [ModelGenerator::class],
9595
'only-repository' => [RepositoryGenerator::class],
9696
'only-service' => [ServiceGenerator::class],

src/Generators/ServiceGenerator.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,23 @@ class ServiceGenerator extends EntityGenerator
1010
{
1111
public function generate(): void
1212
{
13-
if ($this->classExists('repositories', "{$this->model}Repository")) {
14-
$stub = 'service';
15-
} else {
16-
$stub = 'service_with_trait';
17-
18-
if (!$this->classExists('models', $this->model)) {
19-
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
20-
$this->throwFailureException(
21-
exceptionClass: ClassNotExistsException::class,
22-
failureMessage: "Cannot create {$this->model}Service cause {$this->model} Model does not exists.",
23-
recommendedMessage: "Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'.",
24-
);
25-
}
13+
if (!$this->classExists('repositories', "{$this->model}Repository")) {
14+
$this->throwFailureException(
15+
exceptionClass: ClassNotExistsException::class,
16+
failureMessage: "Cannot create {$this->model}Service cause {$this->model}Repository does not exists.",
17+
recommendedMessage: "Create a {$this->model}Repository by himself or run command 'php artisan make:entity {$this->model} --only-repository'.",
18+
);
2619
}
2720

28-
if (!$this->isStubExists($stub)) {
21+
if (!$this->isStubExists('service')) {
2922
return;
3023
}
3124

32-
$serviceContent = $this->getStub($stub, [
25+
$serviceContent = $this->getStub('service', [
3326
'entity' => $this->model,
3427
'fields' => $this->getFields(),
3528
'namespace' => $this->getOrCreateNamespace('services'),
3629
'repositoriesNamespace' => $this->getOrCreateNamespace('repositories'),
37-
'modelsNamespace' => $this->getOrCreateNamespace('models')
3830
]);
3931

4032
$this->saveClass('services', "{$this->model}Service", $serviceContent);

stubs/service_with_trait.blade.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

tests/ServiceGeneratorTest.php

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@ class ServiceGeneratorTest extends TestCase
1414
{
1515
use GeneratorMockTrait;
1616

17-
public function testMissingModel()
17+
public function testMissingRepository()
1818
{
1919
$this->mockClass(ServiceGenerator::class, [
2020
$this->classExistsMethodCall(['repositories', 'PostRepository'], false),
21-
$this->classExistsMethodCall(['models', 'Post'], false),
2221
]);
2322

2423
$this->assertExceptionThrew(
2524
className: ClassNotExistsException::class,
26-
message: 'Cannot create PostService cause Post Model does not exists. '
27-
. "Create a Post Model by himself or run command 'php artisan make:entity Post --only-model'",
25+
message: 'Cannot create PostService cause PostRepository does not exists. '
26+
. "Create a PostRepository by himself or run command 'php artisan make:entity Post --only-repository'",
2827
);
2928

3029
app(ServiceGenerator::class)
@@ -34,57 +33,7 @@ className: ClassNotExistsException::class,
3433
Event::assertNothingDispatched();
3534
}
3635

37-
public function testCreateWithTrait()
38-
{
39-
$this->mockClass(ServiceGenerator::class, [
40-
$this->classExistsMethodCall(['repositories', 'PostRepository'], false),
41-
$this->classExistsMethodCall(['models', 'Post']),
42-
]);
43-
44-
app(ServiceGenerator::class)
45-
->setRelations(new RelationsDTO(
46-
hasMany: ['Comment'],
47-
belongsTo: ['User'],
48-
))
49-
->setFields([
50-
'integer-required' => ['media_id'],
51-
'string-required' => ['body'],
52-
'string' => ['title']
53-
])
54-
->setModel('Post')
55-
->generate();
56-
57-
$this->assertGeneratedFileEquals('service_with_trait.php', 'app/Services/PostService.php');
58-
59-
$this->assertEventPushed(
60-
className: SuccessCreateMessage::class,
61-
message: 'Created a new Service: PostService',
62-
);
63-
}
64-
65-
public function testCreateWithTraitStubNotExist()
66-
{
67-
config(['entity-generator.stubs.service_with_trait' => 'incorrect_stub']);
68-
69-
$this->mockClass(ServiceGenerator::class, [
70-
$this->classExistsMethodCall(['repositories', 'PostRepository'], false),
71-
$this->classExistsMethodCall(['models', 'Post']),
72-
]);
73-
74-
app(ServiceGenerator::class)
75-
->setFields([])
76-
->setModel('Post')
77-
->generate();
78-
79-
$this->assertFileDoesNotExist('app/Services/PostService.php');
80-
81-
$this->assertEventPushed(
82-
className: WarningEvent::class,
83-
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.',
84-
);
85-
}
86-
87-
public function testCreateWithoutTrait()
36+
public function testCreate()
8837
{
8938
$this->mockClass(ServiceGenerator::class, [
9039
$this->classExistsMethodCall(['repositories', 'PostRepository']),
@@ -104,15 +53,15 @@ public function testCreateWithoutTrait()
10453
->setModel('Post')
10554
->generate();
10655

107-
$this->assertGeneratedFileEquals('service_without_trait.php', 'app/Services/PostService.php');
56+
$this->assertGeneratedFileEquals('service.php', 'app/Services/PostService.php');
10857

10958
$this->assertEventPushed(
11059
className: SuccessCreateMessage::class,
11160
message: 'Created a new Service: PostService',
11261
);
11362
}
11463

115-
public function testCreateWithoutTraitStubNotExist()
64+
public function testCreateStubNotExist()
11665
{
11766
config(['entity-generator.stubs.service' => 'incorrect_stub']);
11867

tests/fixtures/ServiceGeneratorTest/service_with_trait.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)