|
6 | 6 | use Illuminate\Database\Eloquent\Model as Eloquent;
|
7 | 7 | use Illuminate\Support\Carbon;
|
8 | 8 | use PHPUnit\Framework\TestCase;
|
| 9 | +use RuntimeException; |
9 | 10 |
|
10 | 11 | class DatabaseEloquentTimestampsTest extends TestCase
|
11 | 12 | {
|
@@ -102,6 +103,178 @@ public function testUserWithUpdatedAt()
|
102 | 103 | $this->assertEquals($now->toDateTimeString(), $user->updated_at->toDateTimeString());
|
103 | 104 | }
|
104 | 105 |
|
| 106 | + public function testWithoutTimestamp() |
| 107 | + { |
| 108 | + Carbon::setTestNow($now = Carbon::now()->setYear(1995)->startOfYear()); |
| 109 | + $user = UserWithCreatedAndUpdated:: create([ 'email' => '[email protected]']); |
| 110 | + Carbon::setTestNow(Carbon::now()->addHour()); |
| 111 | + |
| 112 | + $this->assertTrue($user->usesTimestamps()); |
| 113 | + |
| 114 | + $user->withoutTimestamps(function () use ($user) { |
| 115 | + $this->assertFalse($user->usesTimestamps()); |
| 116 | + $user->update([ |
| 117 | + |
| 118 | + ]); |
| 119 | + }); |
| 120 | + |
| 121 | + $this->assertTrue($user->usesTimestamps()); |
| 122 | + $this->assertTrue($now->equalTo($user->updated_at)); |
| 123 | + $this-> assertSame( '[email protected]', $user-> email); |
| 124 | + } |
| 125 | + |
| 126 | + public function testWithoutTimestampWhenAlreadyIgnoringTimestamps() |
| 127 | + { |
| 128 | + Carbon::setTestNow($now = Carbon::now()->setYear(1995)->startOfYear()); |
| 129 | + $user = UserWithCreatedAndUpdated:: create([ 'email' => '[email protected]']); |
| 130 | + Carbon::setTestNow(Carbon::now()->addHour()); |
| 131 | + |
| 132 | + $user->timestamps = false; |
| 133 | + |
| 134 | + $this->assertFalse($user->usesTimestamps()); |
| 135 | + |
| 136 | + $user->withoutTimestamps(function () use ($user) { |
| 137 | + $this->assertFalse($user->usesTimestamps()); |
| 138 | + $user->update([ |
| 139 | + |
| 140 | + ]); |
| 141 | + }); |
| 142 | + |
| 143 | + $this->assertFalse($user->usesTimestamps()); |
| 144 | + $this->assertTrue($now->equalTo($user->updated_at)); |
| 145 | + $this-> assertSame( '[email protected]', $user-> email); |
| 146 | + } |
| 147 | + |
| 148 | + public function testWithoutTimestampRestoresWhenClosureThrowsException() |
| 149 | + { |
| 150 | + $user = UserWithCreatedAndUpdated:: create([ 'email' => '[email protected]']); |
| 151 | + |
| 152 | + $user->timestamps = true; |
| 153 | + |
| 154 | + try { |
| 155 | + $user->withoutTimestamps(function () use ($user) { |
| 156 | + $this->assertFalse($user->usesTimestamps()); |
| 157 | + throw new RuntimeException(); |
| 158 | + }); |
| 159 | + $this->fail(); |
| 160 | + } catch (RuntimeException) { |
| 161 | + // |
| 162 | + } |
| 163 | + |
| 164 | + $this->assertTrue($user->timestamps); |
| 165 | + } |
| 166 | + |
| 167 | + public function testWithoutTimestampsRespectsClasses() |
| 168 | + { |
| 169 | + $a = new UserWithCreatedAndUpdated(); |
| 170 | + $b = new UserWithCreatedAndUpdated(); |
| 171 | + $z = new UserWithUpdated(); |
| 172 | + |
| 173 | + $this->assertTrue($a->usesTimestamps()); |
| 174 | + $this->assertTrue($b->usesTimestamps()); |
| 175 | + $this->assertTrue($z->usesTimestamps()); |
| 176 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 177 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 178 | + |
| 179 | + Eloquent::withoutTimestamps(function () use ($a, $b, $z) { |
| 180 | + $this->assertFalse($a->usesTimestamps()); |
| 181 | + $this->assertFalse($b->usesTimestamps()); |
| 182 | + $this->assertFalse($z->usesTimestamps()); |
| 183 | + $this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 184 | + $this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 185 | + }); |
| 186 | + |
| 187 | + $this->assertTrue($a->usesTimestamps()); |
| 188 | + $this->assertTrue($b->usesTimestamps()); |
| 189 | + $this->assertTrue($z->usesTimestamps()); |
| 190 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 191 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 192 | + |
| 193 | + UserWithCreatedAndUpdated::withoutTimestamps(function () use ($a, $b, $z) { |
| 194 | + $this->assertFalse($a->usesTimestamps()); |
| 195 | + $this->assertFalse($b->usesTimestamps()); |
| 196 | + $this->assertTrue($z->usesTimestamps()); |
| 197 | + $this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 198 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 199 | + }); |
| 200 | + |
| 201 | + $this->assertTrue($a->usesTimestamps()); |
| 202 | + $this->assertTrue($b->usesTimestamps()); |
| 203 | + $this->assertTrue($z->usesTimestamps()); |
| 204 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 205 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 206 | + |
| 207 | + UserWithUpdated::withoutTimestamps(function () use ($a, $b, $z) { |
| 208 | + $this->assertTrue($a->usesTimestamps()); |
| 209 | + $this->assertTrue($b->usesTimestamps()); |
| 210 | + $this->assertFalse($z->usesTimestamps()); |
| 211 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 212 | + $this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 213 | + }); |
| 214 | + |
| 215 | + $this->assertTrue($a->usesTimestamps()); |
| 216 | + $this->assertTrue($b->usesTimestamps()); |
| 217 | + $this->assertTrue($z->usesTimestamps()); |
| 218 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 219 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 220 | + |
| 221 | + Eloquent::withoutTimestampsOn([], function () use ($a, $b, $z) { |
| 222 | + $this->assertTrue($a->usesTimestamps()); |
| 223 | + $this->assertTrue($b->usesTimestamps()); |
| 224 | + $this->assertTrue($z->usesTimestamps()); |
| 225 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 226 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 227 | + }); |
| 228 | + |
| 229 | + $this->assertTrue($a->usesTimestamps()); |
| 230 | + $this->assertTrue($b->usesTimestamps()); |
| 231 | + $this->assertTrue($z->usesTimestamps()); |
| 232 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 233 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 234 | + |
| 235 | + Eloquent::withoutTimestampsOn([UserWithCreatedAndUpdated::class], function () use ($a, $b, $z) { |
| 236 | + $this->assertFalse($a->usesTimestamps()); |
| 237 | + $this->assertFalse($b->usesTimestamps()); |
| 238 | + $this->assertTrue($z->usesTimestamps()); |
| 239 | + $this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 240 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 241 | + }); |
| 242 | + |
| 243 | + $this->assertTrue($a->usesTimestamps()); |
| 244 | + $this->assertTrue($b->usesTimestamps()); |
| 245 | + $this->assertTrue($z->usesTimestamps()); |
| 246 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 247 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 248 | + |
| 249 | + Eloquent::withoutTimestampsOn([UserWithUpdated::class], function () use ($a, $b, $z) { |
| 250 | + $this->assertTrue($a->usesTimestamps()); |
| 251 | + $this->assertTrue($b->usesTimestamps()); |
| 252 | + $this->assertFalse($z->usesTimestamps()); |
| 253 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 254 | + $this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 255 | + }); |
| 256 | + |
| 257 | + $this->assertTrue($a->usesTimestamps()); |
| 258 | + $this->assertTrue($b->usesTimestamps()); |
| 259 | + $this->assertTrue($z->usesTimestamps()); |
| 260 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 261 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 262 | + |
| 263 | + Eloquent::withoutTimestampsOn([UserWithCreatedAndUpdated::class, UserWithUpdated::class], function () use ($a, $b, $z) { |
| 264 | + $this->assertFalse($a->usesTimestamps()); |
| 265 | + $this->assertFalse($b->usesTimestamps()); |
| 266 | + $this->assertFalse($z->usesTimestamps()); |
| 267 | + $this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 268 | + $this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 269 | + }); |
| 270 | + |
| 271 | + $this->assertTrue($a->usesTimestamps()); |
| 272 | + $this->assertTrue($b->usesTimestamps()); |
| 273 | + $this->assertTrue($z->usesTimestamps()); |
| 274 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); |
| 275 | + $this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); |
| 276 | + } |
| 277 | + |
105 | 278 | /**
|
106 | 279 | * Get a database connection instance.
|
107 | 280 | *
|
|
0 commit comments