|
| 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 | +} |
0 commit comments