-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathGetModelRelationsTest.php
More file actions
92 lines (71 loc) · 2.97 KB
/
Copy pathGetModelRelationsTest.php
File metadata and controls
92 lines (71 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace BeyondCode\ErdGenerator\Tests;
use BeyondCode\ErdGenerator\ModelRelation;
use BeyondCode\ErdGenerator\RelationFinder;
use BeyondCode\ErdGenerator\Tests\Models\Avatar;
use BeyondCode\ErdGenerator\Tests\Models\Comment;
use BeyondCode\ErdGenerator\Tests\Models\Post;
use BeyondCode\ErdGenerator\Tests\Fixtures\ModelWithThrowingMethod;
use BeyondCode\ErdGenerator\Tests\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use PHPUnit\Framework\Attributes\Test;
class GetModelRelationsTest extends TestCase
{
#[Test]
public function it_can_find_model_relations()
{
$finder = new RelationFinder();
$relations = $finder->getModelRelations(User::class);
$this->assertCount(3, $relations);
$posts = $relations['posts'];
$this->assertInstanceOf(ModelRelation::class, $posts);
$this->assertSame('HasMany', $posts->getType());
$this->assertSame('posts', $posts->getName());
$this->assertSame(Post::class, $posts->getModel());
$this->assertSame('id', $posts->getLocalKey());
$this->assertSame('user_id', $posts->getForeignKey());
$avatar = $relations['avatar'];
$this->assertInstanceOf(ModelRelation::class, $avatar);
$this->assertSame('avatar', $avatar->getName());
$this->assertSame('HasOne', $avatar->getType());
$this->assertSame(Avatar::class, $avatar->getModel());
$this->assertSame('id', $avatar->getLocalKey());
$this->assertSame('user_id', $avatar->getForeignKey());
$avatar = $relations['comments'];
$this->assertInstanceOf(ModelRelation::class, $avatar);
$this->assertSame('comments', $avatar->getName());
$this->assertSame('BelongsToMany', $avatar->getType());
$this->assertSame(Comment::class, $avatar->getModel());
$this->assertSame(null, $avatar->getLocalKey());
$this->assertSame(null, $avatar->getForeignKey());
}
#[Test]
public function it_will_ignore_a_relation_if_it_is_excluded_on_config()
{
$this->app['config']->set('erd-generator.ignore', [
User::class => [
'posts'
]
]);
$finder = new RelationFinder();
$relations = $finder->getModelRelations(User::class);
$this->assertCount(2, $relations);
$this->assertNull(Arr::get($relations, 'posts'));
}
#[Test]
public function it_logs_debug_when_method_throws_exception()
{
Log::spy();
$finder = new RelationFinder();
$relations = $finder->getModelRelations(ModelWithThrowingMethod::class);
$this->assertCount(0, $relations);
Log::shouldHaveReceived('debug')
->withArgs(function (string $message) {
return str_contains($message, 'Could not analyze method')
&& str_contains($message, 'throwingMethod')
&& str_contains($message, 'Something went wrong');
})
->once();
}
}