Skip to content

Commit 5e93ac5

Browse files
committed
refactor: tests
1 parent 51653d0 commit 5e93ac5

File tree

8 files changed

+60
-52
lines changed

8 files changed

+60
-52
lines changed

tests/ControllerGeneratorTest.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Contracts\Filesystem\FileNotFoundException;
66
use Illuminate\Support\Facades\Event;
7-
use org\bovigo\vfs\vfsStream;
87
use RonasIT\Support\Events\SuccessCreateMessage;
98
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
109
use RonasIT\Support\Exceptions\ClassNotExistsException;
@@ -19,57 +18,45 @@ public function setUp(): void
1918
{
2019
parent::setUp();
2120

22-
vfsStream::setup();
23-
24-
$this->generatedFileBasePath = vfsStream::url('root');
25-
26-
$this->app->setBasePath($this->generatedFileBasePath);
21+
Event::fake();
2722
}
2823

2924
public function testControllerAlreadyExists()
3025
{
31-
Event::fake();
32-
33-
$this->expectException(ClassAlreadyExistsException::class);
34-
$this->expectExceptionMessage('Cannot create PostController cause PostController already exists. Remove PostController.');
35-
3626
$this->mockClass(ControllerGenerator::class, [
3727
$this->classExistsMethodCall(['controllers', 'PostController'])
3828
]);
3929

30+
$this->expectException(ClassAlreadyExistsException::class);
31+
$this->expectExceptionMessage('Cannot create PostController cause PostController already exists. Remove PostController.');
32+
4033
app(ControllerGenerator::class)
4134
->setModel('Post')
4235
->generate();
43-
44-
Event::assertDispatched(SuccessCreateMessage::class);
4536
}
4637

4738
public function testModelServiceNotExists()
4839
{
49-
Event::fake();
50-
51-
$this->expectException(ClassNotExistsException::class);
52-
$this->expectExceptionMessage('Cannot create PostService cause PostService does not exists. Create a PostService by himself.');
53-
5440
$this->mockClass(ControllerGenerator::class, [
5541
$this->classExistsMethodCall(['controllers', 'PostController'], false),
5642
$this->classExistsMethodCall(['services', 'PostService'], false)
5743
]);
5844

45+
$this->expectException(ClassNotExistsException::class);
46+
$this->expectExceptionMessage('Cannot create PostService cause PostService does not exists. Create a PostService by himself.');
47+
5948
app(ControllerGenerator::class)
6049
->setModel('Post')
6150
->generate();
62-
63-
Event::assertDispatched(SuccessCreateMessage::class);
6451
}
6552

6653
public function testRouteFileNotExists()
6754
{
55+
$this->mockFilesystemWithoutRoutesFile();
56+
6857
$this->expectException(FileNotFoundException::class);
6958
$this->expectExceptionMessage("Not found file with routes. Create a routes file on path: 'vfs://root/routes/api.php'");
7059

71-
$this->mockFilesystemWithoutRoutesFile();
72-
7360
app(ControllerGenerator::class)
7461
->setModel('Post')
7562
->setCrudOptions(['C', 'R', 'U', 'D'])
@@ -78,8 +65,6 @@ public function testRouteFileNotExists()
7865

7966
public function testCreate()
8067
{
81-
Event::fake();
82-
8368
$this->mockFilesystem();
8469

8570
app(ControllerGenerator::class)
@@ -92,6 +77,16 @@ public function testCreate()
9277
$this->assertGeneratedFileEquals('created_controller.php', 'app/Http/Controllers/PostController.php');
9378
$this->assertGeneratedFileEquals('api.php', 'routes/api.php');
9479

95-
Event::assertDispatched(SuccessCreateMessage::class);
80+
Event::assertDispatchedTimes(SuccessCreateMessage::class, 6);
81+
Event::assertDispatched(SuccessCreateMessage::class, function ($event) {
82+
return in_array($event->message, [
83+
"Created a new Route: Route::post('posts', [PostController::class, 'create']);",
84+
"Created a new Route: Route::put('posts/{id}', [PostController::class, 'update']);",
85+
"Created a new Route: Route::delete('posts/{id}', [PostController::class, 'delete']);",
86+
"Created a new Route: Route::get('posts/{id}', [PostController::class, 'get']);",
87+
"Created a new Route: Route::get('posts', [PostController::class, 'search']);",
88+
"Created a new Controller: PostController",
89+
]);
90+
});
9691
}
9792
}

tests/NovaResourceGeneratorTest.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,20 @@ public function testCreateWithMissingNovaPackage()
2323
{
2424
Event::fake();
2525

26-
$this->mockCheckingNovaPackageExistence();
26+
$this->mockNovaServiceProviderExists(false);
2727

2828
app(NovaResourceGenerator::class)
2929
->setModel('Post')
3030
->generate();
3131

32-
Event::assertDispatched(SuccessCreateMessage::class);
32+
Event::assertDispatched(SuccessCreateMessage::class, function ($event) {
33+
return $event->message === 'Nova is not installed and NovaResource is skipped';
34+
});
3335
}
3436

3537
public function testCreateNovaResourceWithMissingModel()
3638
{
37-
$this->mockClassExistsFunction();
38-
39-
$this->mockClass(NovaResourceGenerator::class, [
40-
$this->classExistsMethodCall([], false)
41-
]);
39+
$this->mockNovaServiceProviderExists();
4240

4341
$this->expectException(ClassNotExistsException::class);
4442
$this->expectExceptionMessage('Cannot create Post Nova resource cause Post Model does not exists. '
@@ -51,7 +49,7 @@ public function testCreateNovaResourceWithMissingModel()
5149

5250
public function testCreateNovaTestAlreadyExists()
5351
{
54-
$this->mockClassExistsFunction();
52+
$this->mockNovaServiceProviderExists();
5553

5654
$this->mockClass(NovaResourceGenerator::class, [
5755
$this->classExistsMethodCall(['models', 'Post']),
@@ -70,7 +68,7 @@ public function testCreate()
7068
{
7169
Event::fake();
7270

73-
$this->mockClassExistsFunction();
71+
$this->mockNovaServiceProviderExists();
7472

7573
$this->mockFilesystem();
7674

tests/NovaTestGeneratorTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use RonasIT\Support\Events\SuccessCreateMessage;
77
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
88
use RonasIT\Support\Exceptions\ClassNotExistsException;
9+
use RonasIT\Support\Generators\NovaResourceGenerator;
910
use RonasIT\Support\Generators\NovaTestGenerator;
1011
use RonasIT\Support\Tests\Support\NovaTestGeneratorTest\NovaTestGeneratorMockTrait;
1112

@@ -15,7 +16,7 @@ class NovaTestGeneratorTest extends TestCase
1516

1617
public function testCreateNovaTestsResourceNotExists()
1718
{
18-
$this->mockClassExistsFunction();
19+
$this->mockNovaServiceProviderExists();
1920

2021
$this->mockClass(NovaTestGenerator::class, [
2122
$this->classExistsMethodCall(['nova', 'PostNovaResource'], false),
@@ -33,7 +34,7 @@ public function testCreateNovaTestsResourceNotExists()
3334

3435
public function testCreateNovaTestAlreadyExists()
3536
{
36-
$this->mockClassExistsFunction();
37+
$this->mockNovaServiceProviderExists();
3738

3839
$this->expectException(ClassAlreadyExistsException::class);
3940
$this->expectExceptionMessage("Cannot create NovaPostTest cause it's already exist. Remove NovaPostTest.");
@@ -50,7 +51,7 @@ public function testCreateNovaTestAlreadyExists()
5051

5152
public function testCreate()
5253
{
53-
$this->mockClassExistsFunction();
54+
$this->mockNovaServiceProviderExists();
5455

5556
$this->mockFilesystem();
5657
$this->mockNovaResourceTestGenerator();
@@ -72,12 +73,14 @@ public function testCreateWithMissingNovaPackage()
7273
{
7374
Event::fake();
7475

75-
$this->mockCheckingNovaPackageExistence();
76+
$this->mockNovaServiceProviderExists(false);
7677

7778
app(NovaTestGenerator::class)
7879
->setModel('Post')
7980
->generate();
8081

81-
Event::assertDispatched(SuccessCreateMessage::class);
82+
Event::assertDispatched(SuccessCreateMessage::class, function ($event) {
83+
return $event->message === 'Nova is not installed and NovaTest is skipped';
84+
});
8285
}
8386
}

tests/Support/ControllerGeneratorTest/ControllerGeneratorMockTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function mockFilesystemWithoutRoutesFile(): void
1515
{
1616
$fileSystemMock = new FileSystemMock;
1717
$fileSystemMock->services = [
18-
'PostService.php' => '<?php'
18+
'PostService.php' => $this->mockPhpFileContent(),
1919
];
2020
$fileSystemMock->controllers = [];
2121

@@ -26,11 +26,11 @@ public function mockFilesystem(): void
2626
{
2727
$fileSystemMock = new FileSystemMock;
2828
$fileSystemMock->services = [
29-
'PostService.php' => '<?php'
29+
'PostService.php' => $this->mockPhpFileContent(),
3030
];
3131
$fileSystemMock->controllers = [];
3232
$fileSystemMock->routes = [
33-
'api.php' => '<?php'
33+
'api.php' => $this->mockPhpFileContent(),
3434
];
3535

3636
$fileSystemMock->setStructure();

tests/Support/FileSystemMock.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function setStructure()
2222

2323
if (!is_null($this->novaModels)) {
2424
$structure['app']['Nova'] = [];
25+
2526
foreach ($this->novaModels as $novaModel => $content) {
2627
$structure['app']['Nova'][$novaModel] = $content;
2728
}

tests/Support/GeneratorMockTrait.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@
22

33
namespace RonasIT\Support\Tests\Support;
44

5-
use phpmock\Mock;
5+
use Laravel\Nova\NovaServiceProvider;
66

77
trait GeneratorMockTrait
88
{
9-
public function mockClassExistsFunction(bool $result = true): void
9+
public function mockClassExistsFunction(string $className, bool $result = true, bool $autoloadArg = true): void
1010
{
1111
$this->mockNativeFunction('\RonasIT\Support\Generators', [
1212
$this->functionCall(
1313
name: 'class_exists',
14+
arguments: [$className, $autoloadArg],
1415
result: $result,
1516
),
1617
]);
1718
}
1819

19-
public function mockCheckingNovaPackageExistence(bool $result = false): void
20+
public function mockCheckingNovaPackageExistence(string $className, bool $result = false): void
2021
{
21-
$this->mockClassExistsFunction($result);
22+
$this->mockClassExistsFunction($className, $result);
23+
}
24+
25+
public function mockNovaServiceProviderExists(bool $result = true): void
26+
{
27+
$this->mockClassExistsFunction(NovaServiceProvider::class, $result);
2228
}
2329

2430
public function classExistsMethodCall(array $arguments, bool $result = true): array
@@ -29,4 +35,9 @@ public function classExistsMethodCall(array $arguments, bool $result = true): ar
2935
'result' => $result
3036
];
3137
}
38+
39+
public function mockPhpFileContent(): string
40+
{
41+
return '<?php';
42+
}
3243
}

tests/Support/NovaResourceGeneratorTest/NovaResourceGeneratorMockTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function mockFilesystem(): void
1919
$fileSystemMock = new FileSystemMock;
2020
$fileSystemMock->novaModels = [];
2121
$fileSystemMock->models = [
22-
'Post.php' => '<?php'
22+
'Post.php' => $this->mockPhpFileContent(),
2323
];
2424

2525
$fileSystemMock->setStructure();

tests/Support/NovaTestGeneratorTest/NovaTestGeneratorMockTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,18 @@ public function mockFilesystem(): void
7272
$fileSystemMock = new FileSystemMock;
7373

7474
$fileSystemMock->novaActions = [
75-
'PublishPostAction.php' => '<?php',
76-
'ArchivePostAction.php' => '<?php',
77-
'BlockCommentAction.php' => '<?php',
75+
'PublishPostAction.php' => $this->mockPhpFileContent(),
76+
'ArchivePostAction.php' => $this->mockPhpFileContent(),
77+
'BlockCommentAction.php' => $this->mockPhpFileContent(),
7878
'UnPublishPostAction.txt' => 'text',
7979
];
8080

8181
$fileSystemMock->novaModels = [
82-
'Post.php' => '<?php'
82+
'Post.php' => $this->mockPhpFileContent(),
8383
];
8484

8585
$fileSystemMock->models = [
86-
'Post.php' => '<?php'
86+
'Post.php' => $this->mockPhpFileContent(),
8787
];
8888

8989
$fileSystemMock->testFixtures = [

0 commit comments

Comments
 (0)