|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RonasIT\Support\Tests; |
| 4 | + |
| 5 | +use RonasIT\Support\Events\SuccessCreateMessage; |
| 6 | +use RonasIT\Support\Events\WarningEvent; |
| 7 | +use RonasIT\Support\Exceptions\ClassNotExistsException; |
| 8 | +use RonasIT\Support\Generators\RepositoryGenerator; |
| 9 | +use RonasIT\Support\Tests\Support\Repository\RepositoryMockTrait; |
| 10 | + |
| 11 | +class RepositoryGeneratorTest extends TestCase |
| 12 | +{ |
| 13 | + use RepositoryMockTrait; |
| 14 | + |
| 15 | + public function testModelNotExist() |
| 16 | + { |
| 17 | + $this->mockClass(RepositoryGenerator::class, [ |
| 18 | + $this->classExistsMethodCall(['models', 'Post'], false), |
| 19 | + ]); |
| 20 | + |
| 21 | + $this->assertExceptionThrew( |
| 22 | + className: ClassNotExistsException::class, |
| 23 | + message: "Cannot create PostRepository cause Post Model does not exists. " |
| 24 | + . "Create a Post Model by himself or run command 'php artisan make:entity Post --only-model'.", |
| 25 | + ); |
| 26 | + |
| 27 | + app(RepositoryGenerator::class) |
| 28 | + ->setModel('Post') |
| 29 | + ->generate(); |
| 30 | + |
| 31 | + $this->assertFileDoesNotExist('repository.php', 'app/Repositories/PostRepository.php'); |
| 32 | + } |
| 33 | + |
| 34 | + public function testCreateRepository() |
| 35 | + { |
| 36 | + $this->mockFilesystem(); |
| 37 | + |
| 38 | + app(RepositoryGenerator::class) |
| 39 | + ->setModel('Post') |
| 40 | + ->generate(); |
| 41 | + |
| 42 | + $this->assertGeneratedFileEquals('repository.php', 'app/Repositories/PostRepository.php'); |
| 43 | + |
| 44 | + $this->assertEventPushed( |
| 45 | + className: SuccessCreateMessage::class, |
| 46 | + message: 'Created a new Repository: PostRepository', |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + public function testCreateRepositoryStubNotExist() |
| 51 | + { |
| 52 | + config(['entity-generator.stubs.repository' => 'incorrect_stub']); |
| 53 | + |
| 54 | + $this->mockFilesystem(); |
| 55 | + |
| 56 | + app(RepositoryGenerator::class) |
| 57 | + ->setModel('Post') |
| 58 | + ->generate(); |
| 59 | + |
| 60 | + $this->assertFileDoesNotExist('repository.php', 'app/Repositories/PostRepository.php'); |
| 61 | + |
| 62 | + $this->assertEventPushed( |
| 63 | + className: WarningEvent::class, |
| 64 | + message: 'Generation of repository has been skipped cause the view incorrect_stub from the config entity-generator.stubs.repository is not exists. Please check that config has the correct view name value.', |
| 65 | + ); |
| 66 | + } |
| 67 | +} |
0 commit comments