|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MaksimM\CompositePrimaryKeys\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
6
|
|
|
use MaksimM\CompositePrimaryKeys\Tests\Stubs\TestOrganization; |
|
7
|
|
|
use MaksimM\CompositePrimaryKeys\Tests\Stubs\TestUser; |
|
8
|
|
|
|
|
9
|
|
|
class CompositeToNonCompositeRelationTest extends CompositeKeyBaseUnit |
|
10
|
|
|
{ |
|
11
|
|
|
/** @test */ |
|
12
|
|
|
public function validateBelongsToRelation() |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var TestUser |
|
16
|
|
|
*/ |
|
17
|
|
|
$user = TestUser::find([ |
|
18
|
|
|
'user_id' => 1, |
|
19
|
|
|
'organization_id' => 101, |
|
20
|
|
|
]); |
|
21
|
|
|
|
|
22
|
|
|
$organization = $user->organization()->first(); |
|
23
|
|
|
|
|
24
|
|
|
$this->assertNotNull($organization); |
|
25
|
|
|
$this->assertInstanceOf(TestOrganization::class, $organization); |
|
26
|
|
|
|
|
27
|
|
|
return $organization; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** @test |
|
31
|
|
|
* @depends validateBelongsToRelation |
|
32
|
|
|
*/ |
|
33
|
|
|
public function validateHasManyRelation(TestOrganization $organization) |
|
34
|
|
|
{ |
|
35
|
|
|
$users = $organization->users()->get(); |
|
36
|
|
|
$this->assertInstanceOf(Collection::class, $users); |
|
37
|
|
|
$this->assertEquals(2, $users->count()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** @test */ |
|
41
|
|
|
public function validateEagerBelongsToRelation() |
|
42
|
|
|
{ |
|
43
|
|
|
/** |
|
44
|
|
|
* @var TestUser |
|
45
|
|
|
*/ |
|
46
|
|
|
$user = TestUser::find([ |
|
47
|
|
|
'user_id' => 2, |
|
48
|
|
|
'organization_id' => 101, |
|
49
|
|
|
]); |
|
50
|
|
|
|
|
51
|
|
|
$referrer_user = $user->organization; |
|
52
|
|
|
|
|
53
|
|
|
$this->assertNotNull($referrer_user); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertInstanceOf(TestOrganization::class, $referrer_user); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @test */ |
|
59
|
|
|
public function validateEagerWithBelongsToRelation() |
|
60
|
|
|
{ |
|
61
|
|
|
/** |
|
62
|
|
|
* @var TestUser |
|
63
|
|
|
*/ |
|
64
|
|
|
$user = TestUser::with(['organization'])->find([ |
|
65
|
|
|
'user_id' => 2, |
|
66
|
|
|
'organization_id' => 101, |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
|
|
$referrer_user = $user->organization; |
|
70
|
|
|
|
|
71
|
|
|
$this->assertNotNull($referrer_user); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertInstanceOf(TestOrganization::class, $referrer_user); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|