-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[9.x] Add Model::withoutTimestamps(...) (#44138)
* add withoutTimestamps * pass through $this * ensure timestamps are restored * refactor to static * fix test * formatting Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
ba37a05
commit 0432012
Showing
4 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
use Illuminate\Database\Eloquent\Model as Eloquent; | ||
use Illuminate\Support\Carbon; | ||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
|
||
class DatabaseEloquentTimestampsTest extends TestCase | ||
{ | ||
|
@@ -102,6 +103,178 @@ public function testUserWithUpdatedAt() | |
$this->assertEquals($now->toDateTimeString(), $user->updated_at->toDateTimeString()); | ||
} | ||
|
||
public function testWithoutTimestamp() | ||
{ | ||
Carbon::setTestNow($now = Carbon::now()->setYear(1995)->startOfYear()); | ||
$user = UserWithCreatedAndUpdated::create(['email' => '[email protected]']); | ||
Carbon::setTestNow(Carbon::now()->addHour()); | ||
|
||
$this->assertTrue($user->usesTimestamps()); | ||
|
||
$user->withoutTimestamps(function () use ($user) { | ||
$this->assertFalse($user->usesTimestamps()); | ||
$user->update([ | ||
'email' => '[email protected]', | ||
]); | ||
}); | ||
|
||
$this->assertTrue($user->usesTimestamps()); | ||
$this->assertTrue($now->equalTo($user->updated_at)); | ||
$this->assertSame('[email protected]', $user->email); | ||
} | ||
|
||
public function testWithoutTimestampWhenAlreadyIgnoringTimestamps() | ||
{ | ||
Carbon::setTestNow($now = Carbon::now()->setYear(1995)->startOfYear()); | ||
$user = UserWithCreatedAndUpdated::create(['email' => '[email protected]']); | ||
Carbon::setTestNow(Carbon::now()->addHour()); | ||
|
||
$user->timestamps = false; | ||
|
||
$this->assertFalse($user->usesTimestamps()); | ||
|
||
$user->withoutTimestamps(function () use ($user) { | ||
$this->assertFalse($user->usesTimestamps()); | ||
$user->update([ | ||
'email' => '[email protected]', | ||
]); | ||
}); | ||
|
||
$this->assertFalse($user->usesTimestamps()); | ||
$this->assertTrue($now->equalTo($user->updated_at)); | ||
$this->assertSame('[email protected]', $user->email); | ||
} | ||
|
||
public function testWithoutTimestampRestoresWhenClosureThrowsException() | ||
{ | ||
$user = UserWithCreatedAndUpdated::create(['email' => '[email protected]']); | ||
|
||
$user->timestamps = true; | ||
|
||
try { | ||
$user->withoutTimestamps(function () use ($user) { | ||
$this->assertFalse($user->usesTimestamps()); | ||
throw new RuntimeException(); | ||
}); | ||
$this->fail(); | ||
} catch (RuntimeException) { | ||
// | ||
} | ||
|
||
$this->assertTrue($user->timestamps); | ||
} | ||
|
||
public function testWithoutTimestampsRespectsClasses() | ||
{ | ||
$a = new UserWithCreatedAndUpdated(); | ||
$b = new UserWithCreatedAndUpdated(); | ||
$z = new UserWithUpdated(); | ||
|
||
$this->assertTrue($a->usesTimestamps()); | ||
$this->assertTrue($b->usesTimestamps()); | ||
$this->assertTrue($z->usesTimestamps()); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
|
||
Eloquent::withoutTimestamps(function () use ($a, $b, $z) { | ||
$this->assertFalse($a->usesTimestamps()); | ||
$this->assertFalse($b->usesTimestamps()); | ||
$this->assertFalse($z->usesTimestamps()); | ||
$this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
}); | ||
|
||
$this->assertTrue($a->usesTimestamps()); | ||
$this->assertTrue($b->usesTimestamps()); | ||
$this->assertTrue($z->usesTimestamps()); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
|
||
UserWithCreatedAndUpdated::withoutTimestamps(function () use ($a, $b, $z) { | ||
$this->assertFalse($a->usesTimestamps()); | ||
$this->assertFalse($b->usesTimestamps()); | ||
$this->assertTrue($z->usesTimestamps()); | ||
$this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
}); | ||
|
||
$this->assertTrue($a->usesTimestamps()); | ||
$this->assertTrue($b->usesTimestamps()); | ||
$this->assertTrue($z->usesTimestamps()); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
|
||
UserWithUpdated::withoutTimestamps(function () use ($a, $b, $z) { | ||
$this->assertTrue($a->usesTimestamps()); | ||
$this->assertTrue($b->usesTimestamps()); | ||
$this->assertFalse($z->usesTimestamps()); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
}); | ||
|
||
$this->assertTrue($a->usesTimestamps()); | ||
$this->assertTrue($b->usesTimestamps()); | ||
$this->assertTrue($z->usesTimestamps()); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
|
||
Eloquent::withoutTimestampsOn([], function () use ($a, $b, $z) { | ||
$this->assertTrue($a->usesTimestamps()); | ||
$this->assertTrue($b->usesTimestamps()); | ||
$this->assertTrue($z->usesTimestamps()); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
}); | ||
|
||
$this->assertTrue($a->usesTimestamps()); | ||
$this->assertTrue($b->usesTimestamps()); | ||
$this->assertTrue($z->usesTimestamps()); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
|
||
Eloquent::withoutTimestampsOn([UserWithCreatedAndUpdated::class], function () use ($a, $b, $z) { | ||
$this->assertFalse($a->usesTimestamps()); | ||
$this->assertFalse($b->usesTimestamps()); | ||
$this->assertTrue($z->usesTimestamps()); | ||
$this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
}); | ||
|
||
$this->assertTrue($a->usesTimestamps()); | ||
$this->assertTrue($b->usesTimestamps()); | ||
$this->assertTrue($z->usesTimestamps()); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
|
||
Eloquent::withoutTimestampsOn([UserWithUpdated::class], function () use ($a, $b, $z) { | ||
$this->assertTrue($a->usesTimestamps()); | ||
$this->assertTrue($b->usesTimestamps()); | ||
$this->assertFalse($z->usesTimestamps()); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
}); | ||
|
||
$this->assertTrue($a->usesTimestamps()); | ||
$this->assertTrue($b->usesTimestamps()); | ||
$this->assertTrue($z->usesTimestamps()); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
|
||
Eloquent::withoutTimestampsOn([UserWithCreatedAndUpdated::class, UserWithUpdated::class], function () use ($a, $b, $z) { | ||
$this->assertFalse($a->usesTimestamps()); | ||
$this->assertFalse($b->usesTimestamps()); | ||
$this->assertFalse($z->usesTimestamps()); | ||
$this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertTrue(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
}); | ||
|
||
$this->assertTrue($a->usesTimestamps()); | ||
$this->assertTrue($b->usesTimestamps()); | ||
$this->assertTrue($z->usesTimestamps()); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithCreatedAndUpdated::class)); | ||
$this->assertFalse(Eloquent::isIgnoringTimestamps(UserWithUpdated::class)); | ||
} | ||
|
||
/** | ||
* Get a database connection instance. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters