Skip to content

Commit a6a2747

Browse files
committed
refactor:use common resources
1 parent fd12be4 commit a6a2747

22 files changed

+533
-62
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"psr-4": {
3636
"RonasIT\\Support\\Tests\\": "tests/",
3737
"RonasIT\\Support\\Tests\\Support\\": "tests/Support/",
38-
"App\\Nova\\": "tests/Support/Nova/"
38+
"App\\Nova\\": "tests/Support/Nova/",
39+
"Laravel\\Nova\\": "tests/Support/NovaResource"
3940
},
4041
"files": [
4142
"tests/TestCase.php"

src/Generators/NovaTestGenerator.php

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
class NovaTestGenerator extends AbstractTestsGenerator
1717
{
18-
protected string $resourceName;
18+
protected ?string $resourceName;
1919

2020
protected ?string $fullNovaResourcePath = null;
2121

22-
protected string $shortNovaResourceName;
22+
protected ?string $shortNovaResourceName;
2323

2424
protected string $novaPath;
2525

@@ -36,7 +36,7 @@ public function generate(): void
3636
if (!$this->classExists('models', $this->model)) {
3737
$this->throwFailureException(
3838
ClassNotExistsException::class,
39-
"Cannot create Nova{$this->shortNovaResourceName}Test cause {$this->model} does not exist.",
39+
"Cannot create Nova{$this->model}Resource Test cause {$this->model} does not exist.",
4040
"Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'."
4141
);
4242
}
@@ -59,11 +59,7 @@ public function generate(): void
5959

6060
public function setMetaData(array $data): self
6161
{
62-
$resourceName = empty($data['resource_name']) ? "{$this->model}Resource" : $data['resource_name'];
63-
64-
$this->resourceName = Str::studly($resourceName);
65-
66-
$this->shortNovaResourceName = Str::afterLast($this->resourceName, '\\');
62+
$this->resourceName = !empty($data['resource_name']) ? Str::studly($data['resource_name']) : null;
6763

6864
return $this;
6965
}
@@ -136,54 +132,80 @@ protected function isFixtureNeeded($type): bool
136132
return true;
137133
}
138134

139-
protected function isNovaResourceExists(): true
135+
protected function isNovaResourceExists(): void
140136
{
141-
$allNovaClasses = $this->getAllNovaClasses();
137+
$resource = $this->getNovaResource();
142138

143-
$resources = [];
139+
$this->shortNovaResourceName = Str::afterLast($resource, '\\');
140+
$this->fullNovaResourcePath = "App\\Nova\\{$resource}";
144141

145-
foreach ($allNovaClasses as $class) {
146-
if ($class === $this->resourceName) {
147-
$this->fullNovaResourcePath = "App\\Nova\\{$this->resourceName}";
142+
if (!class_exists($this->fullNovaResourcePath)) {
143+
$this->throwFailureException(
144+
ClassNotExistsException::class,
145+
"Cannot create Nova{$this->shortNovaResourceName}Test cause {$this->resourceName} Nova resource does not exist.",
146+
"Create {$this->resourceName} Nova resource."
147+
);
148+
}
149+
}
148150

149-
return true;
150-
}
151+
protected function getNovaResource(): ?string
152+
{
153+
if (!empty($this->resourceName)) {
154+
return $this->resourceName;
155+
} else {
156+
$commonResources = $this->getCommonNovaResources();
151157

152-
if (Str::contains($class, $this->model) && is_subclass_of("App\\Nova\\{$class}", "App\\Nova\\Resource")) {
153-
$resources[] = $class;
154-
}
155-
}
158+
if (count($commonResources) > 1) {
159+
$commonResources = implode(', ', $commonResources);
156160

157-
if (!empty($resources)) {
158-
$resources = implode(', ', $resources);
161+
$this->throwFailureException(
162+
EntityCreateException::class,
163+
"Cannot create Nova{$this->model}Resource Test cause was found a lot of suitable resources: $commonResources",
164+
"Please, use --resource-name option"
165+
);
166+
}
159167

160-
$this->throwFailureException(
161-
EntityCreateException::class,
162-
"Cannot create Nova{$this->shortNovaResourceName}Test cause I am found a lot of suitable resources: $resources",
163-
"Please, use --resource-name option"
164-
);
168+
return $commonResources[0];
165169
}
166-
167-
$this->throwFailureException(
168-
ClassNotExistsException::class,
169-
"Cannot create Nova{$this->shortNovaResourceName}Test cause {$this->resourceName} Nova resource does not exist.",
170-
"Create {$this->resourceName} Nova resource."
171-
);
172170
}
173171

174-
protected function getAllNovaClasses(): Generator
172+
protected function allCommonNovaResources(): Generator
175173
{
176174
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->novaPath));
177175

178176
foreach ($iterator as $file) {
179177
if ($file->isFile() && $file->getExtension() === 'php') {
180178
$relativePath = Str::after($file->getPathname(), $this->novaPath . DIRECTORY_SEPARATOR);
181179

182-
yield str_replace(['/', '.php'], ['\\', ''], $relativePath);
180+
$class = str_replace(['/', '.php'], ['\\', ''], $relativePath);
181+
182+
if($this->isValidCommonResourceCheck($class)){
183+
yield $class;
184+
}
183185
}
184186
}
185187
}
186188

189+
protected function getCommonNovaResources(): array
190+
{
191+
$commonNovaResources = $this->allCommonNovaResources();
192+
193+
$resources = [];
194+
195+
foreach ($commonNovaResources as $resource) {
196+
$resources[] = $resource;
197+
}
198+
199+
return $resources;
200+
}
201+
202+
protected function isValidCommonResourceCheck(string $resource)
203+
{
204+
$isContainModel = Str::afterLast(str_replace('Resource', '', $resource), '\\') === $this->model;
205+
206+
return is_subclass_of("App\\Nova\\{$resource}", "Laravel\\Nova\\Resource") && $isContainModel;
207+
}
208+
187209
protected function collectFilters(): array
188210
{
189211
$filtersFromFields = $this->getFiltersFromFields();

stubs/nova_resource.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
@foreach($types as $fieldType)
77
use Laravel\Nova\Fields\{{$fieldType}};
88
@endforeach
9+
use Laravel\Nova\Resource;
910

1011
class {{$model}}Resource extends Resource
1112
{

tests/CommandTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use RonasIT\Support\Tests\Support\Command\CommandMockTrait;
99
use RonasIT\Support\Tests\Support\Command\Models\Post;
1010
use UnexpectedValueException;
11+
use App\Nova\PostResource;
1112

1213
class CommandTest extends TestCase
1314
{
@@ -51,6 +52,7 @@ public function testCallCommand()
5152

5253
$this->mockGenerator();
5354
$this->mockGettingModelInstance(new Post());
55+
$this->app->instance(PostResource::class, new PostResource());
5456
$this->mockDBTransactionStartRollback(2);
5557

5658
$this

tests/NovaTestGeneratorTest.php

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Laravel\Nova\NovaServiceProvider;
1212
use RonasIT\Support\Tests\Support\Models\WelcomeBonus;
1313
use RonasIT\Support\Exceptions\EntityCreateException;
14+
use RonasIT\Support\Tests\Support\Models\Post;
1415

1516
class NovaTestGeneratorTest extends TestCase
1617
{
@@ -25,12 +26,15 @@ public function setUp(): void
2526

2627
public function testGenerateResourceNotExists()
2728
{
28-
$this->mockNovaServiceProviderExists();
29-
3029
$this->mockClass(NovaTestGenerator::class, [
3130
$this->classExistsMethodCall(['models', 'News']),
3231
]);
3332

33+
$this->mockNativeGeneratorFunctions(
34+
$this->nativeClassExistsMethodCall([NovaServiceProvider::class, true]),
35+
$this->nativeClassExistsMethodCall(["App\Nova\NewsResource"], false),
36+
);
37+
3438
$this->assertExceptionThrew(
3539
className: ClassNotExistsException::class,
3640
message: 'Cannot create NovaNewsResourceTest cause NewsResource Nova resource does not exist. Create NewsResource Nova resource.',
@@ -44,13 +48,16 @@ className: ClassNotExistsException::class,
4448

4549
public function testGenerateNovaTestAlreadyExists()
4650
{
47-
$this->mockNovaServiceProviderExists();
48-
4951
$this->mockClass(NovaTestGenerator::class, [
5052
$this->classExistsMethodCall(['models', 'Post']),
5153
$this->classExistsMethodCall(['nova', 'NovaPostResourceTest']),
5254
]);
5355

56+
$this->mockNativeGeneratorFunctions(
57+
$this->nativeClassExistsMethodCall([NovaServiceProvider::class, true]),
58+
$this->nativeClassExistsMethodCall(["App\Nova\Resources\PostResource"]),
59+
);
60+
5461
$this->assertExceptionThrew(
5562
className: ClassAlreadyExistsException::class,
5663
message: "Cannot create NovaPostResourceTest cause it's already exist. Remove NovaPostResourceTest.",
@@ -71,6 +78,7 @@ public function testNovaTestStubNotExist()
7178

7279
$this->mockNativeGeneratorFunctions(
7380
$this->nativeClassExistsMethodCall([NovaServiceProvider::class, true]),
81+
$this->nativeClassExistsMethodCall(["App\Nova\WelcomeBonusResource"]),
7482
$this->nativeClassExistsMethodCall([WelcomeBonus::class, true]),
7583
);
7684

@@ -105,8 +113,6 @@ className: WarningEvent::class,
105113

106114
public function testDumpStubNotExist()
107115
{
108-
$this->mockNovaServiceProviderExists();
109-
110116
$this->mockNovaRequestClassCall();
111117

112118
config([
@@ -121,6 +127,11 @@ public function testDumpStubNotExist()
121127
$this->classExistsMethodCall(['factories', 'WelcomeBonusFactory'], false),
122128
]);
123129

130+
$this->mockNativeGeneratorFunctions(
131+
$this->nativeClassExistsMethodCall([NovaServiceProvider::class, true]),
132+
$this->nativeClassExistsMethodCall(["App\Nova\WelcomeBonusResource"]),
133+
);
134+
124135
app(NovaTestGenerator::class)
125136
->setModel('WelcomeBonus')
126137
->setMetaData(['resource_name' => 'WelcomeBonusResource'])
@@ -156,6 +167,7 @@ public function testSuccess()
156167

157168
$this->mockNativeGeneratorFunctions(
158169
$this->nativeClassExistsMethodCall([NovaServiceProvider::class, true]),
170+
$this->nativeClassExistsMethodCall(["App\Nova\WelcomeBonusResource"]),
159171
$this->nativeClassExistsMethodCall([WelcomeBonus::class, true]),
160172
);
161173

@@ -175,17 +187,21 @@ public function testSuccess()
175187

176188
public function testWithManySameResources()
177189
{
178-
$this->mockNativeGeneratorFunctions(
179-
$this->nativeClassExistsMethodCall([NovaServiceProvider::class, true]),
180-
);
190+
$this->mockNovaServiceProviderExists();
191+
192+
$this->mockNovaRequestClassCall();
193+
194+
$this->mockClass(NovaTestGenerator::class, [
195+
$this->classExistsMethodCall(['models', 'WelcomeBonus']),
196+
]);
181197

182198
$this->assertExceptionThrew(
183199
className: EntityCreateException::class,
184-
message: 'Cannot create NovaPostResourceTest cause I am found a lot of suitable resources: Resources\PostResource Please, use --resource-name option',
200+
message: 'Cannot create NovaWelcomeBonusResource Test cause was found a lot of suitable resources: WelcomeBonusResource, Resources\WelcomeBonus Please, use --resource-name option',
185201
);
186202

187203
app(NovaTestGenerator::class)
188-
->setModel('Post')
204+
->setModel('WelcomeBonus')
189205
->setMetaData(['resource_name' => null])
190206
->generate();
191207
}
@@ -197,32 +213,33 @@ public function testSuccessWithoutSetMetaData()
197213
]);
198214

199215
$this->mockClass(NovaTestGenerator::class, [
200-
$this->classExistsMethodCall(['models', 'WelcomeBonus']),
201-
$this->classExistsMethodCall(['nova', 'NovaWelcomeBonusResourceTest'], false),
216+
$this->classExistsMethodCall(['models', 'Post']),
217+
$this->classExistsMethodCall(['nova', 'NovaPostResourceTest'], false),
202218
$this->classExistsMethodCall(['models', 'User'], false),
203-
$this->classExistsMethodCall(['factories', 'WelcomeBonusFactory'], false),
204-
$this->classExistsMethodCall(['factories', 'WelcomeBonusFactory'], false),
219+
$this->classExistsMethodCall(['factories', 'PostFactory'], false),
220+
$this->classExistsMethodCall(['factories', 'PostFactory'], false),
205221
]);
206222

207223
$this->mockDBTransactionStartRollback();
208224

209225
$this->mockNativeGeneratorFunctions(
210226
$this->nativeClassExistsMethodCall([NovaServiceProvider::class, true]),
211-
$this->nativeClassExistsMethodCall([WelcomeBonus::class, true]),
227+
$this->nativeClassExistsMethodCall(["App\Nova\Resources\PostResource"]),
228+
$this->nativeClassExistsMethodCall([Post::class, true]),
212229
);
213230

214231
$this->mockNovaRequestClassCall();
215232

216233
app(NovaTestGenerator::class)
217-
->setModel('WelcomeBonus')
234+
->setModel('Post')
218235
->setMetaData(['resource_name' => null])
219236
->generate();
220237

221-
$this->assertGeneratedFileEquals('created_resource_test.php', 'tests/NovaWelcomeBonusResourceTest.php');
222-
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/NovaWelcomeBonusResourceTest/nova_welcome_bonus_dump.sql');
223-
$this->assertGeneratedFileEquals('create_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusResourceTest/create_welcome_bonus_request.json');
224-
$this->assertGeneratedFileEquals('create_welcome_bonus_response.json', 'tests/fixtures/NovaWelcomeBonusResourceTest/create_welcome_bonus_response.json');
225-
$this->assertGeneratedFileEquals('update_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusResourceTest/update_welcome_bonus_request.json');
238+
$this->assertGeneratedFileEquals('created_post_resource_test.php', 'tests/NovaPostResourceTest.php');
239+
$this->assertGeneratedFileEquals('post_dump.sql', 'tests/fixtures/NovaPostResourceTest/nova_post_dump.sql');
240+
$this->assertGeneratedFileEquals('create_post_request.json', 'tests/fixtures/NovaPostResourceTest/create_post_request.json');
241+
$this->assertGeneratedFileEquals('create_post_response.json', 'tests/fixtures/NovaPostResourceTest/create_post_response.json');
242+
$this->assertGeneratedFileEquals('update_post_request.json', 'tests/fixtures/NovaPostResourceTest/update_post_request.json');
226243
}
227244

228245
public function testSuccessWithNestedFile(): void
@@ -243,6 +260,7 @@ public function testSuccessWithNestedFile(): void
243260

244261
$this->mockNativeGeneratorFunctions(
245262
$this->nativeClassExistsMethodCall([NovaServiceProvider::class, true]),
263+
$this->nativeClassExistsMethodCall(['App\Nova\Resources\WelcomeBonusDraftResource']),
246264
$this->nativeClassExistsMethodCall([WelcomeBonus::class, true]),
247265
);
248266

@@ -266,7 +284,7 @@ public function testSetIncorrectModel(): void
266284

267285
$this->assertExceptionThrew(
268286
className: ClassNotExistsException::class,
269-
message: "Cannot create NovaSomeUndefinedModelResourceTest cause SomeUndefinedModel does not exist. "
287+
message: "Cannot create NovaSomeUndefinedModelResource Test cause SomeUndefinedModel does not exist. "
270288
. "Create a SomeUndefinedModel Model by himself or run command 'php artisan make:entity SomeUndefinedModel --only-model'.",
271289
);
272290

tests/Support/Command/CommandMockTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function mockGenerator(): void
6363
$this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Command\Models\Post', true]),
6464
$this->nativeClassExistsMethodCall(['Laravel\Nova\NovaServiceProvider', true]),
6565
$this->nativeClassExistsMethodCall(['Laravel\Nova\NovaServiceProvider', true]),
66+
$this->nativeClassExistsMethodCall(['App\Nova\PostResource']),
6667
$this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Command\Models\Post', true]),
6768
);
6869
}
@@ -93,6 +94,7 @@ public function mockGeneratorOnlyNovaTests(): void
9394

9495
$this->mockNativeGeneratorFunctions(
9596
$this->nativeClassExistsMethodCall(['Laravel\Nova\NovaServiceProvider', true]),
97+
$this->nativeClassExistsMethodCall(['App\Nova\PostResource']),
9698
$this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Command\Models\Post', true]),
9799
);
98100
}

tests/Support/Models/News.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests\Support\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use RonasIT\Support\Traits\ModelTrait;
7+
8+
class News extends Model
9+
{
10+
use ModelTrait;
11+
12+
public function getConnectionName(): string
13+
{
14+
return 'pgsql';
15+
}
16+
17+
protected $fillable = [
18+
'title',
19+
'name',
20+
];
21+
22+
public function some_relation()
23+
{
24+
}
25+
}

0 commit comments

Comments
 (0)