|
15 | 15 |
|
16 | 16 | use PHPUnit\Framework\Attributes\Group; |
17 | 17 | use Tests\Support\Models\UserModel; |
| 18 | +use Tests\Support\Models\UserWithEventsModel; |
18 | 19 | use Tests\Support\Models\ValidModel; |
19 | 20 |
|
20 | 21 | /** |
@@ -109,4 +110,54 @@ public function testMultiplePager(): void |
109 | 110 | $this->assertStringContainsString('?page_user=3"', $pager->links('user')); |
110 | 111 | $this->assertStringContainsString('?page_user=4"', $pager->links('user')); |
111 | 112 | } |
| 113 | + |
| 114 | + public function testPaginateWithBeforeFindEvents(): void |
| 115 | + { |
| 116 | + $this->createModel(UserWithEventsModel::class); |
| 117 | + |
| 118 | + $this->seedPaginateEventModel(); |
| 119 | + |
| 120 | + // Test pagination - beforeFind event should filter to only US users |
| 121 | + $data = $this->model->paginate(2); |
| 122 | + |
| 123 | + // Should only get US users in results |
| 124 | + $this->assertCount(2, $data); |
| 125 | + $this->assertSame(3, $this->model->pager->getDetails()['total']); |
| 126 | + $this->assertSame(2, $this->model->pager->getPageCount()); |
| 127 | + |
| 128 | + // Verify all returned users are from US |
| 129 | + foreach ($data as $user) { |
| 130 | + $this->assertSame('US', $user->country); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public function testPaginateWithBeforeFindEventsAndDisabledCallbacks(): void |
| 135 | + { |
| 136 | + $this->createModel(UserWithEventsModel::class); |
| 137 | + |
| 138 | + $this->seedPaginateEventModel(); |
| 139 | + |
| 140 | + $data = $this->model->allowCallbacks(false)->paginate(2); |
| 141 | + |
| 142 | + // Should get all users |
| 143 | + $this->assertCount(2, $data); |
| 144 | + $this->assertSame(9, $this->model->pager->getDetails()['total']); |
| 145 | + |
| 146 | + // Should have users from different countries |
| 147 | + $countries = array_unique(array_column($data, 'country')); |
| 148 | + $this->assertGreaterThan(1, count($countries)); |
| 149 | + } |
| 150 | + |
| 151 | + private function seedPaginateEventModel(): void |
| 152 | + { |
| 153 | + $testData = [ |
| 154 | + [ 'name' => 'Jean', 'email' => '[email protected]', 'country' => 'France'], |
| 155 | + [ 'name' => 'Marie', 'email' => '[email protected]', 'country' => 'France'], |
| 156 | + [ 'name' => 'John', 'email' => '[email protected]', 'country' => 'US'], |
| 157 | + [ 'name' => 'Hans', 'email' => '[email protected]', 'country' => 'Germany'], |
| 158 | + [ 'name' => 'Luigi', 'email' => '[email protected]', 'country' => 'Italy'], |
| 159 | + ]; |
| 160 | + |
| 161 | + $this->model->insertBatch($testData); |
| 162 | + } |
112 | 163 | } |
0 commit comments