forked from mongodb/laravel-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmbedsOneTest.php
141 lines (113 loc) · 4.93 KB
/
EmbedsOneTest.php
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
class EmbedsOneTest extends TestCase {
public function tearDown()
{
User::truncate();
Mockery::close();
}
public function testEmpty()
{
$user = User::create(['name' => 'John Doe']);
$this->assertNull($user->father);
}
public function testSaveNewModel()
{
$user = User::create(['name' => 'John Doe']);
$father = new User(['name' => 'Mark Doe']);
$result = $user->father()->save($father);
$this->assertArrayHasKey('father', $user->getRelations());
$this->assertTrue(array_key_exists('father', $user->getAttributes()));
$this->assertInstanceOf('User', $result);
$this->assertInstanceOf('User', $user->father);
$this->assertEquals($user->father->name, 'Mark Doe');
$user = User::find($user->_id);
$this->assertArrayNotHasKey('father', $user->getRelations());
$this->assertTrue(array_key_exists('father', $user->getAttributes()));
$this->assertInstanceOf('User', $user->father);
$this->assertEquals($user->father->name, 'Mark Doe');
$this->assertArrayHasKey('father', $user->getRelations());
}
public function testCreateNewModel()
{
$user = User::create(['name' => 'John Doe']);
$user->father()->create(['name' => 'Mark Doe']);
$this->assertArrayHasKey('father', $user->getRelations());
$this->assertTrue(array_key_exists('father', $user->getAttributes()));
$this->assertInstanceOf('User', $user->father);
$this->assertEquals($user->father->name, 'Mark Doe');
$user = User::find($user->_id);
$this->assertArrayNotHasKey('father', $user->getRelations());
$this->assertTrue(array_key_exists('father', $user->getAttributes()));
$this->assertInstanceOf('User', $user->father);
$this->assertEquals($user->father->name, 'Mark Doe');
$this->assertArrayHasKey('father', $user->getRelations());
}
public function testEagerLoading()
{
User::create(['name' => 'John Doe'])->father()->create(['name' => 'Mark Doe']);
User::create(['name' => 'Tom Doe'])->father()->create(['name' => 'Mark Doe']);
User::create(['name' => 'Dennis Doe'])->father()->create(['name' => 'Mark Doe']);
$users = User::get();
$this->assertCount(3, $users);
foreach ($users as $user)
{
$this->assertArrayNotHasKey('father', $user->getRelations());
}
$users = User::with('father')->get();
$this->assertCount(3, $users);
foreach ($users as $user)
{
$this->assertArrayHasKey('father', $user->getRelations());
}
}
public function testWhereHas()
{
// User::create(['name' => 'John Doe'])->father()->create(['name' => 'Mark Doe']);
// User::create(['name' => 'Tom Doe']);
// User::create(['name' => 'Dennis Doe'])->father()->create(['name' => 'Mark Doe']);
// $users = User::has('father')->get();
// $this->assertEquals($users[0]->name, 'John Doe');
// $this->assertEquals($users[1]->name, 'Dennis Doe');
}
public function testAssociate()
{
$user = User::create(['name' => 'John Doe']);
$father = new User(['name' => 'Mark Doe']);
$user->father()->associate($father);
$this->assertArrayHasKey('father', $user->getRelations());
$this->assertInstanceOf('User', $user->father);
$this->assertEquals($user->father->name, 'Mark Doe');
$user = User::find($user->_id);
$this->assertArrayNotHasKey('father', $user->getRelations());
$this->assertNull($user->father);
}
public function testUpdate()
{
$user = User::create(['name' => 'John Doe']);
$father = $user->father()->create(['name' => 'Mark Doe']);
$user->father()->update(['age' => 60]);
$user = User::find($user->_id);
$this->assertEquals($user->father->name, 'Mark Doe');
$this->assertEquals($user->father->age, 60);
}
public function testSaveOnEmbeddedModel()
{
// $user = User::create(['name' => 'John Doe']);
// $father = $user->father()->save(new User(['name' => 'Mark Doe']));
// $father->name = 'Steve Doe';
// $father->save();
// $user = User::find($user->_id);
// $this->assertEquals($user->father->name, 'Steve Doe');
}
public function testNestedModels()
{
// $user = User::create(['name' => 'John Doe']);
// $father = $user->father()->save(new User(['name' => 'Mark Doe']));
// $grandFather = $father->father()->save(new User(['name' => 'Tim Doe']));
// $this->assertEquals($user->father->name, 'Steve Doe');
// $this->assertEquals($user->father->father->name, 'Tim Doe');
// $user = User::find($user->_id);
// $this->assertEquals($user->father->name, 'Steve Doe');
// $this->assertEquals($user->father->father->name, 'Tim Doe');
}
}