-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEntityTest.php
More file actions
147 lines (114 loc) · 3.87 KB
/
EntityTest.php
File metadata and controls
147 lines (114 loc) · 3.87 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
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
142
143
144
145
146
147
<?php
declare(strict_types=1);
namespace Tests;
use CodeIgniter\Events\Events;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Tests\Support\Database\Seeds\SeedTests;
use Tests\Support\Entities\Post;
use Tests\Support\Entities\Profile;
use Tests\Support\Entities\User;
use Tests\Support\Models\UserModel;
/**
* @internal
*/
final class EntityTest extends CIUnitTestCase
{
use DatabaseTestTrait;
protected $refresh = true;
protected $namespace;
protected $seed = SeedTests::class;
private int $queryCount = 0;
protected function setUp(): void
{
parent::setUp();
$this->queryCount = 0;
Events::on('DBQuery', function () {
$this->queryCount++;
});
}
public function testHasOne()
{
$user = model(UserModel::class)->find(1);
$this->assertInstanceOf(User::class, $user);
$isset = isset($user->profile);
$this->assertFalse($isset);
$this->assertSame('1', $user->profile->user_id);
$this->assertSame('United States', $user->profile->country);
$this->assertInstanceOf(Profile::class, $user->profile);
}
public function testHasMany()
{
$user = model(UserModel::class)->find(1);
$this->assertInstanceOf(User::class, $user);
$isset = isset($user->posts);
$this->assertFalse($isset);
$posts = $user->posts;
$this->assertIsArray($posts);
$this->assertInstanceOf(Post::class, $posts[0]);
$this->assertSame('1', $posts[0]->user_id);
$this->assertSame('Title 1', $posts[0]->title);
}
public function testRelationNotExists()
{
$user = model(UserModel::class)->find(1);
$this->assertInstanceOf(User::class, $user);
$isset = isset($user->notExists);
$this->assertFalse($isset);
$notExists = $user->notExists;
$this->assertNull($notExists);
}
public function testRelationQueriedOnlyOnce()
{
$model = model(UserModel::class);
// Single user (without some relations)
$data = [
'username' => 'Test Single User',
'company_id' => '2',
'country_id' => '1',
];
$id = $model->insert($data);
$user = $model->find($id);
$queryCount = $this->queryCount;
$profile = $user->profile;
$this->assertNull($profile);
$this->assertSame($queryCount + 1, $this->queryCount);
$profile = $user->profile;
$this->assertNull($profile);
$this->assertSame($queryCount + 1, $this->queryCount);
}
public function testSaveNullRelationOneToOneSkipsInsert()
{
$model = model(UserModel::class);
$data = [
'username' => 'Test User',
'company_id' => '2',
'country_id' => '1',
'profile' => null,
];
$id = $model->with('profile')->insert($data);
$this->assertIsNumeric($id);
// Verify user was created but profile was not
$user = $model->with('profile')->find($id);
$this->assertInstanceOf(User::class, $user);
$this->assertSame('Test User', $user->username);
$this->assertNull($user->profile);
}
public function testSaveEmptyArrayRelationOneToManySkipsInsert()
{
$model = model(UserModel::class);
$data = [
'username' => 'Test User',
'company_id' => '2',
'country_id' => '1',
'posts' => [],
];
$id = $model->with('posts')->insert($data);
$this->assertIsNumeric($id);
// Verify user was created but no posts were created
$user = $model->with('posts')->find($id);
$this->assertInstanceOf(User::class, $user);
$this->assertSame('Test User', $user->username);
$this->assertSame([], $user->posts);
}
}