Skip to content

Commit 7230314

Browse files
authored
Merge pull request #73 from RonasIT/49-add-repository-generator-tests
Add repository generator tests
2 parents 3b523a6 + 40fdcb2 commit 7230314

File tree

5 files changed

+107
-2
lines changed

5 files changed

+107
-2
lines changed

src/Generators/RepositoryGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function generate(): void
1212
if (!$this->classExists('models', $this->model)) {
1313
$this->throwFailureException(
1414
ClassNotExistsException::class,
15-
"Cannot create {$this->model} Model cause {$this->model} Model does not exists.",
15+
"Cannot create {$this->model}Repository cause {$this->model} Model does not exists.",
1616
"Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'."
1717
);
1818
}

tests/FactoryGeneratorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ className: ViewException::class,
6969
])
7070
->setModel('Post')
7171
->generate();
72-
7372
}
7473

7574
public function testCreateSuccess()

tests/RepositoryGeneratorTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests\Support\Repository;
4+
5+
use RonasIT\Support\Tests\Support\FileSystemMock;
6+
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
7+
8+
trait RepositoryMockTrait
9+
{
10+
use GeneratorMockTrait;
11+
12+
public function mockFilesystem(): void
13+
{
14+
$fileSystemMock = new FileSystemMock;
15+
16+
$fileSystemMock->models = [
17+
'Post.php' => $this->mockPhpFileContent(),
18+
];
19+
20+
$fileSystemMock->setStructure();
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Repositories;
4+
5+
use App\Models\Post;
6+
use RonasIT\Support\Repositories\BaseRepository;
7+
8+
/**
9+
* @property Post $model
10+
*/
11+
class PostRepository extends BaseRepository
12+
{
13+
public function __construct()
14+
{
15+
$this->setModel(Post::class);
16+
}
17+
}

0 commit comments

Comments
 (0)