|
14 | 14 | namespace CodeIgniter\Models; |
15 | 15 |
|
16 | 16 | use PHPUnit\Framework\Attributes\Group; |
| 17 | +use Tests\Support\Models\EventModel; |
17 | 18 | use Tests\Support\Models\UserModel; |
18 | 19 |
|
19 | 20 | /** |
|
22 | 23 | #[Group('DatabaseLive')] |
23 | 24 | final class ExistsModelTest extends LiveModelTestCase |
24 | 25 | { |
| 26 | + public function testExistsByIdReturnsTrueForExistingPrimaryKey(): void |
| 27 | + { |
| 28 | + $this->createModel(UserModel::class); |
| 29 | + |
| 30 | + $this->assertTrue($this->model->existsById(1)); |
| 31 | + } |
| 32 | + |
| 33 | + public function testExistsByIdReturnsFalseForMissingPrimaryKey(): void |
| 34 | + { |
| 35 | + $this->createModel(UserModel::class); |
| 36 | + |
| 37 | + $this->assertFalse($this->model->existsById(999)); |
| 38 | + } |
| 39 | + |
| 40 | + public function testExistsByIdRespectsSoftDeletes(): void |
| 41 | + { |
| 42 | + $this->createModel(UserModel::class); |
| 43 | + $this->model->delete(1); |
| 44 | + |
| 45 | + $this->assertFalse($this->model->existsById(1)); |
| 46 | + $this->assertTrue($this->model->withDeleted()->existsById(1)); |
| 47 | + } |
| 48 | + |
| 49 | + public function testExistsByIdWorksWithCurrentModelQuery(): void |
| 50 | + { |
| 51 | + $this->createModel(UserModel::class); |
| 52 | + |
| 53 | + $this->assertTrue($this->model->where('country', 'US')->existsById(1)); |
| 54 | + $this->assertFalse($this->model->where('country', 'UK')->existsById(1)); |
| 55 | + } |
| 56 | + |
| 57 | + public function testExistsByIdDoesNotTriggerFindCallbacks(): void |
| 58 | + { |
| 59 | + $model = $this->createModel(EventModel::class); |
| 60 | + |
| 61 | + $this->assertTrue($model->existsById(1)); |
| 62 | + $this->assertFalse($model->hasToken('beforeFind')); |
| 63 | + $this->assertFalse($model->hasToken('afterFind')); |
| 64 | + } |
| 65 | + |
25 | 66 | public function testExistsRespectsSoftDeletes(): void |
26 | 67 | { |
27 | 68 | $this->createModel(UserModel::class); |
|
0 commit comments