|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Models; |
| 15 | + |
| 16 | +use PHPUnit\Framework\Attributes\Group; |
| 17 | +use RuntimeException; |
| 18 | +use Tests\Support\Models\EventModel; |
| 19 | +use Tests\Support\Models\UserModel; |
| 20 | + |
| 21 | +/** |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +#[Group('DatabaseLive')] |
| 25 | +final class WithLockedRowModelTest extends LiveModelTestCase |
| 26 | +{ |
| 27 | + protected function setUp(): void |
| 28 | + { |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + if ($this->db->DBDriver === 'SQLite3') { |
| 32 | + $this->markTestSkipped('SQLite3 does not support lockForUpdate().'); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public function testWithLockedRowReturnsCallbackResult(): void |
| 37 | + { |
| 38 | + $inTransaction = false; |
| 39 | + $model = $this->createModel(UserModel::class); |
| 40 | + |
| 41 | + $result = $model->withLockedRow(1, static function (object $user, UserModel $model) use (&$inTransaction): string { |
| 42 | + $inTransaction = $model->db->inTransaction(); |
| 43 | + |
| 44 | + return $user->email; |
| 45 | + }); |
| 46 | + |
| 47 | + $this->assertSame('derek@world.com', $result); |
| 48 | + $this->assertTrue($inTransaction); |
| 49 | + $this->assertFalse($this->db->inTransaction()); |
| 50 | + } |
| 51 | + |
| 52 | + public function testWithLockedRowDoesNotRunCallbackWhenRowIsMissing(): void |
| 53 | + { |
| 54 | + $callbackRan = false; |
| 55 | + $model = $this->createModel(UserModel::class); |
| 56 | + |
| 57 | + $result = $model->withLockedRow(999, static function () use (&$callbackRan): void { |
| 58 | + $callbackRan = true; |
| 59 | + }); |
| 60 | + |
| 61 | + $this->assertNull($result); |
| 62 | + $this->assertFalse($callbackRan); |
| 63 | + } |
| 64 | + |
| 65 | + public function testWithLockedRowAppliesExistingQueryConstraints(): void |
| 66 | + { |
| 67 | + $model = $this->createModel(UserModel::class); |
| 68 | + |
| 69 | + $result = $model->where('country', 'CA')->withLockedRow(1, static fn (): string => 'locked'); |
| 70 | + |
| 71 | + $this->assertNull($result); |
| 72 | + } |
| 73 | + |
| 74 | + public function testWithLockedRowRespectsSoftDeletes(): void |
| 75 | + { |
| 76 | + $model = $this->createModel(UserModel::class); |
| 77 | + $model->delete(1); |
| 78 | + |
| 79 | + $result = $model->withLockedRow(1, static fn (): string => 'locked'); |
| 80 | + |
| 81 | + $this->assertNull($result); |
| 82 | + } |
| 83 | + |
| 84 | + public function testWithLockedRowCanIncludeSoftDeletedRows(): void |
| 85 | + { |
| 86 | + $model = $this->createModel(UserModel::class); |
| 87 | + $model->delete(1); |
| 88 | + |
| 89 | + $result = $model->withDeleted()->withLockedRow(1, static fn (object $user): string => $user->email); |
| 90 | + |
| 91 | + $this->assertSame('derek@world.com', $result); |
| 92 | + } |
| 93 | + |
| 94 | + public function testWithLockedRowRollsBackWhenCallbackThrows(): void |
| 95 | + { |
| 96 | + $model = $this->createModel(UserModel::class); |
| 97 | + |
| 98 | + try { |
| 99 | + $model->withLockedRow(1, static function (object $user, UserModel $model): void { |
| 100 | + $model->update($user->id, ['name' => 'Rolled Back']); |
| 101 | + |
| 102 | + throw new RuntimeException('Stop transaction.'); |
| 103 | + }); |
| 104 | + } catch (RuntimeException $e) { |
| 105 | + $this->assertSame('Stop transaction.', $e->getMessage()); |
| 106 | + } |
| 107 | + |
| 108 | + $this->seeInDatabase('user', [ |
| 109 | + 'id' => 1, |
| 110 | + 'name' => 'Derek Jones', |
| 111 | + ]); |
| 112 | + } |
| 113 | + |
| 114 | + public function testWithLockedRowBypassesFindCallbacks(): void |
| 115 | + { |
| 116 | + $model = $this->createModel(EventModel::class); |
| 117 | + $model->beforeFindReturnData = true; |
| 118 | + |
| 119 | + $result = $model->withLockedRow(1, static fn (array $user): string => $user['email']); |
| 120 | + |
| 121 | + $this->assertSame('derek@world.com', $result); |
| 122 | + $this->assertFalse($model->hasToken('beforeFind')); |
| 123 | + $this->assertFalse($model->hasToken('afterFind')); |
| 124 | + } |
| 125 | + |
| 126 | + public function testWithLockedRowRestoresCallbacksBeforeRunningCallback(): void |
| 127 | + { |
| 128 | + $model = $this->createModel(EventModel::class); |
| 129 | + |
| 130 | + $model->withLockedRow(1, static function (array $user, EventModel $model): void { |
| 131 | + $model->update($user['id'], ['name' => 'Locked Update']); |
| 132 | + }); |
| 133 | + |
| 134 | + $this->assertTrue($model->hasToken('beforeUpdate')); |
| 135 | + $this->assertTrue($model->hasToken('afterUpdate')); |
| 136 | + } |
| 137 | + |
| 138 | + public function testWithLockedRowDoesNotAddLimitToLockedLookup(): void |
| 139 | + { |
| 140 | + $model = $this->createModel(UserModel::class); |
| 141 | + |
| 142 | + $model->withLockedRow(1, static fn (): string => 'locked'); |
| 143 | + |
| 144 | + $sql = strtoupper((string) $this->db->getLastQuery()); |
| 145 | + |
| 146 | + $this->assertStringNotContainsString(' LIMIT ', $sql); |
| 147 | + $this->assertStringNotContainsString(' OFFSET ', $sql); |
| 148 | + } |
| 149 | +} |
0 commit comments