Skip to content

Commit caf1c8c

Browse files
author
Ni Nelli
committed
refactor: fix model relation type, separate methods in ModelMockTrait
refs: #203
1 parent c928e1b commit caf1c8c

File tree

8 files changed

+36
-27
lines changed

8 files changed

+36
-27
lines changed

src/Generators/ModelGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ protected function isRequired(string $typeName): bool
261261
protected function getRelationType(string $model, string $relation): string
262262
{
263263
if (in_array($relation, self::PLURAL_NUMBER_REQUIRED)) {
264-
return "Collection<int, $model>";
264+
return "Collection<{$model}>";
265265
}
266266

267267
return "{$model}|null";

tests/ModelGeneratorTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function setUp(): void
1919
{
2020
parent::setUp();
2121

22-
$this->mockFilesystem();
22+
$this->mockDefaultFilesystem();
2323
}
2424

2525
public function testModelAlreadyExists()
@@ -249,19 +249,15 @@ className: WarningEvent::class,
249249

250250
public function testAddPropertyAnnotationToRelatedModel()
251251
{
252-
$this->mockFilesystem([
253-
'Post.php' => $this->getFixture('new_model_without_fields.php'),
254-
]);
255-
256252
app(ModelGenerator::class)
257253
->setModel('Category')
258254
->setFields([])
259255
->setRelations(new RelationsDTO(
260-
belongsToMany: ['Post'],
256+
belongsToMany: ['User'],
261257
))
262258
->generate();
263259

264-
$this->assertGeneratedFileEquals('related_model_with_property.php', 'app/Models/Post.php');
260+
$this->assertGeneratedFileEquals('related_model_with_property.php', 'app/Models/User.php');
265261

266262
$this->assertEventPushed(
267263
className: SuccessCreateMessage::class,

tests/Support/Model/ModelMockTrait.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@ trait ModelMockTrait
99
{
1010
use GeneratorMockTrait;
1111

12+
public function mockDefaultFilesystem(): void
13+
{
14+
$fileSystemMock = new FileSystemMock;
15+
16+
$fileSystemMock->models = [
17+
'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
18+
'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
19+
'Forum/Author.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
20+
];
21+
22+
$fileSystemMock->setStructure();
23+
}
24+
1225
public function mockFilesystem(array $models = []): void
1326
{
1427
$fileSystemMock = new FileSystemMock;
1528

16-
if (!empty($models)) {
17-
$fileSystemMock->models = $models;
18-
} else {
19-
$fileSystemMock->models = [
20-
'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
21-
'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
22-
'Forum/Author.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
23-
];
24-
}
29+
$fileSystemMock->models = $models;
2530

2631
$fileSystemMock->setStructure();
2732
}

tests/fixtures/ModelGeneratorTest/new_model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @property Carbon $published_at
2323
* @property array $meta
2424
* @property Comment|null $comment
25-
* @property Collection<int, User> $users
25+
* @property Collection<User> $users
2626
*/
2727
class Post extends Model
2828
{

tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Illuminate\Database\Eloquent\Collection;
99

1010
/**
11-
* @property Collection<int, User> $users
11+
* @property Collection<User> $users
1212
*/
1313
class Post extends Model
1414
{

tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
/**
1111
* @property string $title
12-
* @property Collection<int, Author> $authors
12+
* @property Collection<Author> $authors
1313
*/
1414
class Post extends Model
1515
{

tests/fixtures/ModelGeneratorTest/new_subfolders_model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @property Carbon $published_at
2525
* @property array $meta
2626
* @property Comment|null $comment
27-
* @property Collection<int, User> $users
27+
* @property Collection<User> $users
2828
*/
2929
class Post extends Model
3030
{
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
<?php
22

3-
namespace App\Models;
3+
namespace RonasIT\Support\Tests\Support\Models;
44

55
use Illuminate\Database\Eloquent\Collection;
66
use Illuminate\Database\Eloquent\Model;
77
use RonasIT\Support\Traits\ModelTrait;
88

9-
//TODO: add @property annotation for each model's field
109
/**
11-
* @property Collection<int, Category> $categories
10+
* @property Collection<Category> $categories
1211
*/
13-
class Post extends Model
12+
class WelcomeBonus extends Model
1413
{
1514
use ModelTrait;
1615

16+
public function getConnectionName(): string
17+
{
18+
return 'pgsql';
19+
}
20+
1721
protected $fillable = [
22+
'title',
23+
'name',
1824
];
1925

20-
protected $hidden = ['pivot'];
26+
public function some_relation()
27+
{
28+
}
2129

2230
public function categories()
2331
{
2432
return $this->belongsToMany(Category::class);
2533
}
26-
}
34+
}

0 commit comments

Comments
 (0)