Skip to content

Commit 3f38dbd

Browse files
committed
fix:
- minor fixes - removed unused methods - use WelcomeBonusResource instead mocking NovaTestGenerator
1 parent 8384c61 commit 3f38dbd

13 files changed

+95
-76
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"psr-4": {
3939
"RonasIT\\Support\\Tests\\": "tests/",
4040
"RonasIT\\Support\\Tests\\Support\\": "tests/Support/",
41-
"App\\Models\\": "tests/Support/Models/"
41+
"App\\Models\\": "tests/Support/Models/",
42+
"App\\Nova\\": "tests/Support/Nova/"
4243
},
4344
"files": [
4445
"tests/TestCase.php"

src/Generators/AbstractTestsGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ protected function getMockModel($model): array
184184
$hasFactory = method_exists($modelClass, 'factory') && $this->classExists('factory', "{$model}Factory");
185185
$factory = ($hasFactory) ? $modelClass::factory() : factory($modelClass);
186186

187+
dd($factory);
187188
return $factory
188189
->make()
189190
->toArray();

src/Generators/NovaResourceGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected function prepareNovaFields(): array
112112
return $result;
113113
}
114114

115-
public function getFieldsForCreation(): array
115+
protected function getFieldsForCreation(): array
116116
{
117117
if ($this->commandFieldsExists()) {
118118
return $this->getFieldsFromCommandLineArguments();

tests/NovaResourceGeneratorTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@
22

33
namespace RonasIT\Support\Tests;
44

5-
use Doctrine\DBAL\Schema\Column;
6-
use Doctrine\DBAL\Types\DateTimeType;
7-
use Doctrine\DBAL\Types\IntegerType;
8-
use Doctrine\DBAL\Types\StringType;
95
use Illuminate\Support\Facades\Event;
10-
use ReflectionClass;
116
use RonasIT\Support\Events\SuccessCreateMessage;
127
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
138
use RonasIT\Support\Exceptions\ClassNotExistsException;
149
use RonasIT\Support\Generators\NovaResourceGenerator;
15-
use RonasIT\Support\Support\DatabaseNovaField;
1610
use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\NovaResourceGeneratorMockTrait;
1711

1812
class NovaResourceGeneratorTest extends TestCase
@@ -44,9 +38,11 @@ public function testCreateNovaResourceWithMissingModel()
4438
{
4539
$this->mockNovaServiceProviderExists();
4640

47-
$this->expectException(ClassNotExistsException::class);
48-
$this->expectExceptionMessage('Cannot create Post Nova resource cause Post Model does not exists. '
49-
. "Create a Post Model by himself or run command 'php artisan make:entity Post --only-model'");
41+
$this->assertExceptionThrowed(
42+
className: ClassNotExistsException::class,
43+
message: 'Cannot create Post Nova resource cause Post Model does not exists. '
44+
. "Create a Post Model by himself or run command 'php artisan make:entity Post --only-model'"
45+
);
5046

5147
app(NovaResourceGenerator::class)
5248
->setModel('Post')

tests/NovaTestGeneratorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class NovaTestGeneratorTest extends TestCase
1313
{
1414
use NovaTestGeneratorMockTrait;
1515

16-
public function testCreateNovaTestsResourceNotExists()
16+
public function testGenerateResourceNotExists()
1717
{
1818
$this->mockNovaServiceProviderExists();
1919

@@ -33,7 +33,7 @@ className: ClassNotExistsException::class,
3333
->generate();
3434
}
3535

36-
public function testCreateNovaTestAlreadyExists()
36+
public function testGenerateNovaTestAlreadyExists()
3737
{
3838
$this->mockNovaServiceProviderExists();
3939

@@ -70,7 +70,7 @@ public function testSuccess()
7070
$this->assertGeneratedFileEquals('update_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusTest/update_welcome_bonus_request.json');
7171
}
7272

73-
public function testCreateWithMissingNovaPackage()
73+
public function testGenerateNovaPackageNotInstall()
7474
{
7575
Event::fake();
7676

tests/Support/FileSystemMock.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
use Illuminate\Support\Arr;
66
use org\bovigo\vfs\vfsStream;
77

8-
class FileSystemMock {
9-
public $novaModels = null;
10-
public $novaActions = null;
11-
public $models = null;
12-
public $controllers = null;
13-
public $services = null;
14-
public $repositories = null;
15-
public $testFixtures = null;
16-
public $testClasses = null;
17-
public $routes = null;
18-
19-
public function setStructure()
8+
class FileSystemMock
9+
{
10+
public ?array $novaModels = null;
11+
public ?array $novaActions = null;
12+
public ?array $models = null;
13+
public ?array $controllers = null;
14+
public ?array $services = null;
15+
public ?array $repositories = null;
16+
public ?array $testFixtures = null;
17+
public ?array $testClasses = null;
18+
public ?array $routes = null;
19+
20+
public function setStructure(): void
2021
{
2122
$structure = ['app' => []];
2223

tests/Support/GeneratorMockTrait.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ public function mockClassExistsFunction(string $className, bool $result = true,
1717
]);
1818
}
1919

20-
public function mockCheckingNovaPackageExistence(string $className, bool $result = false): void
21-
{
22-
$this->mockClassExistsFunction($className, $result);
23-
}
24-
2520
public function mockNovaServiceProviderExists(bool $result = true): void
2621
{
2722
$this->mockClassExistsFunction(NovaServiceProvider::class, $result);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\Nova;
4+
5+
use Laravel\Nova\Fields\ID;
6+
use Laravel\Nova\Fields\Text;
7+
use Laravel\Nova\Http\Requests\NovaRequest;
8+
use RonasIT\Support\Tests\Support\NovaTestGeneratorTest\CreatedAtFilter;
9+
use RonasIT\Support\Tests\Support\NovaTestGeneratorTest\DateField;
10+
use RonasIT\Support\Tests\Support\NovaTestGeneratorTest\PublishPostAction;
11+
use RonasIT\Support\Tests\Support\NovaTestGeneratorTest\TextField;
12+
use RonasIT\Support\Tests\Support\NovaTestGeneratorTest\UnPublishPostAction;
13+
14+
class WelcomeBonusResource
15+
{
16+
public static $title = 'name';
17+
18+
public static $search = ['id', 'name'];
19+
20+
public static function label(): string
21+
{
22+
return 'WelcomeBonus';
23+
}
24+
25+
public function fields(NovaRequest $request): array
26+
{
27+
return [
28+
new TextField,
29+
new DateField,
30+
];
31+
}
32+
33+
public function cards(NovaRequest $request): array
34+
{
35+
return [];
36+
}
37+
38+
public function filters(NovaRequest $request): array
39+
{
40+
return [
41+
new CreatedAtFilter,
42+
];
43+
}
44+
45+
public function lenses(NovaRequest $request): array
46+
{
47+
return [];
48+
}
49+
50+
public function actions(NovaRequest $request): array
51+
{
52+
return [
53+
new PublishPostAction,
54+
new UnPublishPostAction,
55+
new UnPublishPostAction,
56+
];
57+
}
58+
}

tests/Support/NovaTestGeneratorTest/NovaTestGeneratorMockTrait.php

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace RonasIT\Support\Tests\Support\NovaTestGeneratorTest;
44

5-
use RonasIT\Support\Generators\NovaTestGenerator;
5+
use Mockery;
66
use RonasIT\Support\Tests\Support\FileSystemMock;
77
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
88
use RonasIT\Support\Traits\MockTrait;
@@ -14,42 +14,9 @@ trait NovaTestGeneratorMockTrait
1414

1515
public function mockNovaResourceTestGenerator(): void
1616
{
17-
$this->mockClass(NovaTestGenerator::class, [
18-
[
19-
'function' => 'getMockModel',
20-
'arguments' => ['WelcomeBonus'],
21-
'result' => ['title' => 'some title', 'name' => 'some name']
22-
],
23-
[
24-
'function' => 'getMockModel',
25-
'arguments' => ['WelcomeBonus'],
26-
'result' => ['title' => 'some title', 'name' => 'some name']
27-
],
28-
[
29-
'function' => 'loadNovaActions',
30-
'arguments' => [],
31-
'result' => [
32-
new PublishPostAction,
33-
new UnPublishPostAction,
34-
new UnPublishPostAction,
35-
]
36-
],
37-
[
38-
'function' => 'loadNovaFields',
39-
'arguments' => [],
40-
'result' => [
41-
new TextField,
42-
new DateField,
43-
]
44-
],
45-
[
46-
'function' => 'loadNovaFilters',
47-
'arguments' => [],
48-
'result' => [
49-
new CreatedAtFilter,
50-
]
51-
],
52-
]);
17+
$mock = Mockery::mock('alias:Laravel\Nova\Http\Requests\NovaRequest');
18+
19+
$this->app->instance('Laravel\Nova\Http\Requests\NovaRequest', $mock);
5320
}
5421

5522
public function mockFilesystem(): void
@@ -64,7 +31,7 @@ public function mockFilesystem(): void
6431
];
6532

6633
$fileSystemMock->novaModels = [
67-
'WelcomeBonus.php' => $this->mockPhpFileContent(),
34+
'WelcomeBonusResource.php' => $this->mockPhpFileContent(),
6835
];
6936

7037
$fileSystemMock->models = [
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"title": "some title",
3-
"name": "some name"
2+
"title": 1,
3+
"name": 1
44
}

0 commit comments

Comments
 (0)