forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPaginateModelTest.php
More file actions
163 lines (129 loc) · 5.39 KB
/
PaginateModelTest.php
File metadata and controls
163 lines (129 loc) · 5.39 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Models;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Models\UserModel;
use Tests\Support\Models\UserWithEventsModel;
use Tests\Support\Models\ValidModel;
/**
* @internal
*/
#[Group('DatabaseLive')]
final class PaginateModelTest extends LiveModelTestCase
{
public function testPaginate(): void
{
$data = $this->createModel(ValidModel::class)->paginate();
$this->assertCount(4, $data);
}
public function testPaginateChangeConfigPager(): void
{
$perPage = config('Pager')->perPage;
config('Pager')->perPage = 1;
$data = $this->createModel(ValidModel::class)->paginate();
$this->assertCount(1, $data);
config('Pager')->perPage = $perPage;
}
public function testPaginatePassPerPageParameter(): void
{
$data = $this->createModel(ValidModel::class)->paginate(2);
$this->assertCount(2, $data);
}
public function testPaginateForQueryWithGroupBy(): void
{
$this->createModel(ValidModel::class);
$this->model->select('id')->groupBy('id');
$this->model->paginate();
$this->assertSame(4, $this->model->pager->getDetails()['total']);
}
public function testPaginateWithDeleted(): void
{
$this->createModel(UserModel::class);
$this->model->delete(1);
$data = $this->model->withDeleted()->paginate();
$this->assertCount(4, $data);
$this->assertSame(4, $this->model->pager->getDetails()['total']);
}
public function testPaginateWithoutDeleted(): void
{
$this->createModel(UserModel::class);
$this->model->delete(1);
$data = $this->model->withDeleted(false)->paginate();
$this->assertCount(3, $data);
$this->assertSame(3, $this->model->pager->getDetails()['total']);
}
public function testPaginatePageOutOfRange(): void
{
$this->createModel(ValidModel::class);
$this->model->paginate(1, 'default', -500);
$this->assertSame(1, $this->model->pager->getCurrentPage());
$this->model->paginate(1, 'default', 500);
$this->assertSame($this->model->pager->getPageCount(), $this->model->pager->getCurrentPage());
}
public function testMultiplePager(): void
{
$_GET = [];
$validModel = $this->createModel(ValidModel::class);
$userModel = $this->createModel(UserModel::class);
$validModel->paginate(1, 'valid');
$userModel->paginate(1, 'user');
$pager = $this->model->pager;
$this->assertSame($userModel->pager, $validModel->pager);
$this->assertSame(4, $validModel->countAllResults());
$this->assertSame(4, $userModel->countAllResults());
$this->assertStringContainsString('?page_valid=1"', $pager->links('valid'));
$this->assertStringContainsString('?page_valid=2"', $pager->links('valid'));
$this->assertStringContainsString('?page_valid=3"', $pager->links('valid'));
$this->assertStringContainsString('?page_valid=4"', $pager->links('valid'));
$this->assertStringContainsString('?page_user=1"', $pager->links('user'));
$this->assertStringContainsString('?page_user=2"', $pager->links('user'));
$this->assertStringContainsString('?page_user=3"', $pager->links('user'));
$this->assertStringContainsString('?page_user=4"', $pager->links('user'));
}
public function testPaginateWithBeforeFindEvents(): void
{
$this->createModel(UserWithEventsModel::class);
$this->seedPaginateEventModel();
// Test pagination - beforeFind event should filter to only US users
$data = $this->model->paginate(2);
// Should only get US users in results
$this->assertCount(2, $data);
$this->assertSame(3, $this->model->pager->getDetails()['total']);
$this->assertSame(2, $this->model->pager->getPageCount());
// Verify all returned users are from US
foreach ($data as $user) {
$this->assertSame('US', $user->country);
}
}
public function testPaginateWithBeforeFindEventsAndDisabledCallbacks(): void
{
$this->createModel(UserWithEventsModel::class);
$this->seedPaginateEventModel();
$data = $this->model->allowCallbacks(false)->paginate(2);
// Should get all users
$this->assertCount(2, $data);
$this->assertSame(9, $this->model->pager->getDetails()['total']);
// Should have users from different countries
$countries = array_unique(array_column($data, 'country'));
$this->assertGreaterThan(1, count($countries));
}
private function seedPaginateEventModel(): void
{
$testData = [
['name' => 'Jean', 'email' => '[email protected]', 'country' => 'France'],
['name' => 'Marie', 'email' => '[email protected]', 'country' => 'France'],
['name' => 'John', 'email' => '[email protected]', 'country' => 'US'],
['name' => 'Hans', 'email' => '[email protected]', 'country' => 'Germany'],
['name' => 'Luigi', 'email' => '[email protected]', 'country' => 'Italy'],
];
$this->model->insertBatch($testData);
}
}