-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[10.x] Use model cast when builder created updated at value #47942
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database\MySql; | ||
|
||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Database\Eloquent\Casts\Attribute; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class EloquentCastTest extends MySqlTestCase | ||
{ | ||
protected $driver = 'mysql'; | ||
|
||
protected function defineDatabaseMigrationsAfterDatabaseRefreshed() | ||
{ | ||
Schema::create('users', function ($table) { | ||
$table->increments('id'); | ||
$table->string('email')->unique(); | ||
$table->integer('created_at'); | ||
$table->integer('updated_at'); | ||
}); | ||
} | ||
|
||
protected function destroyDatabaseMigrations() | ||
{ | ||
Schema::drop('users'); | ||
} | ||
|
||
public function testItCastTimestampsCreatedByTheBuilderWhenTimeHasNotPassed() | ||
{ | ||
Carbon::setTestNow(now()); | ||
$createdAt = now()->timestamp; | ||
|
||
$castUser = UserWithIntTimestampsViaCasts::create([ | ||
'email' => fake()->unique()->email, | ||
]); | ||
$attributeUser = UserWithIntTimestampsViaAttribute::create([ | ||
'email' => fake()->unique()->email, | ||
]); | ||
$mutatorUser = UserWithIntTimestampsViaMutator::create([ | ||
'email' => fake()->unique()->email, | ||
]); | ||
|
||
$this->assertSame($createdAt, $castUser->created_at->timestamp); | ||
$this->assertSame($createdAt, $castUser->updated_at->timestamp); | ||
$this->assertSame($createdAt, $attributeUser->created_at->timestamp); | ||
$this->assertSame($createdAt, $attributeUser->updated_at->timestamp); | ||
$this->assertSame($createdAt, $mutatorUser->created_at->timestamp); | ||
$this->assertSame($createdAt, $mutatorUser->updated_at->timestamp); | ||
|
||
$castUser->update([ | ||
'email' => fake()->unique()->email, | ||
]); | ||
$attributeUser->update([ | ||
'email' => fake()->unique()->email, | ||
]); | ||
$mutatorUser->update([ | ||
'email' => fake()->unique()->email, | ||
]); | ||
|
||
$this->assertSame($createdAt, $castUser->created_at->timestamp); | ||
$this->assertSame($createdAt, $castUser->updated_at->timestamp); | ||
$this->assertSame($createdAt, $castUser->fresh()->updated_at->timestamp); | ||
$this->assertSame($createdAt, $attributeUser->created_at->timestamp); | ||
$this->assertSame($createdAt, $attributeUser->updated_at->timestamp); | ||
$this->assertSame($createdAt, $attributeUser->fresh()->updated_at->timestamp); | ||
$this->assertSame($createdAt, $mutatorUser->created_at->timestamp); | ||
$this->assertSame($createdAt, $mutatorUser->updated_at->timestamp); | ||
$this->assertSame($createdAt, $mutatorUser->fresh()->updated_at->timestamp); | ||
} | ||
|
||
public function testItCastTimestampsCreatedByTheBuilderWhenTimeHasPassed() | ||
{ | ||
Carbon::setTestNow(now()); | ||
$createdAt = now()->timestamp; | ||
|
||
$castUser = UserWithIntTimestampsViaCasts::create([ | ||
'email' => fake()->unique()->email, | ||
]); | ||
$attributeUser = UserWithIntTimestampsViaAttribute::create([ | ||
'email' => fake()->unique()->email, | ||
]); | ||
$mutatorUser = UserWithIntTimestampsViaMutator::create([ | ||
'email' => fake()->unique()->email, | ||
]); | ||
|
||
$this->assertSame($createdAt, $castUser->created_at->timestamp); | ||
$this->assertSame($createdAt, $castUser->updated_at->timestamp); | ||
$this->assertSame($createdAt, $attributeUser->created_at->timestamp); | ||
$this->assertSame($createdAt, $attributeUser->updated_at->timestamp); | ||
$this->assertSame($createdAt, $mutatorUser->created_at->timestamp); | ||
$this->assertSame($createdAt, $mutatorUser->updated_at->timestamp); | ||
|
||
Carbon::setTestNow(now()->addSecond()); | ||
$updatedAt = now()->timestamp; | ||
|
||
$castUser->update([ | ||
'email' => fake()->unique()->email, | ||
]); | ||
$attributeUser->update([ | ||
'email' => fake()->unique()->email, | ||
]); | ||
$mutatorUser->update([ | ||
'email' => fake()->unique()->email, | ||
]); | ||
|
||
$this->assertSame($createdAt, $castUser->created_at->timestamp); | ||
$this->assertSame($updatedAt, $castUser->updated_at->timestamp); | ||
$this->assertSame($updatedAt, $castUser->fresh()->updated_at->timestamp); | ||
$this->assertSame($createdAt, $attributeUser->created_at->timestamp); | ||
$this->assertSame($updatedAt, $attributeUser->updated_at->timestamp); | ||
$this->assertSame($updatedAt, $attributeUser->fresh()->updated_at->timestamp); | ||
$this->assertSame($createdAt, $mutatorUser->created_at->timestamp); | ||
$this->assertSame($updatedAt, $mutatorUser->updated_at->timestamp); | ||
$this->assertSame($updatedAt, $mutatorUser->fresh()->updated_at->timestamp); | ||
} | ||
} | ||
|
||
class UserWithIntTimestampsViaCasts extends Model | ||
{ | ||
protected $table = 'users'; | ||
|
||
protected $guarded = []; | ||
|
||
protected $casts = [ | ||
'created_at' => UnixTimeStampToCarbon::class, | ||
'updated_at' => UnixTimeStampToCarbon::class, | ||
]; | ||
} | ||
|
||
class UnixTimeStampToCarbon implements CastsAttributes | ||
{ | ||
public function get($model, string $key, $value, array $attributes) | ||
{ | ||
return Carbon::parse($value); | ||
} | ||
|
||
public function set($model, string $key, $value, array $attributes) | ||
{ | ||
return Carbon::parse($value)->timestamp; | ||
} | ||
} | ||
|
||
class UserWithIntTimestampsViaAttribute extends Model | ||
{ | ||
protected $table = 'users'; | ||
|
||
protected $guarded = []; | ||
|
||
protected function updatedAt(): Attribute | ||
{ | ||
return Attribute::make( | ||
get: fn ($value) => Carbon::parse($value), | ||
set: fn ($value) => Carbon::parse($value)->timestamp, | ||
); | ||
} | ||
|
||
protected function createdAt(): Attribute | ||
{ | ||
return Attribute::make( | ||
get: fn ($value) => Carbon::parse($value), | ||
set: fn ($value) => Carbon::parse($value)->timestamp, | ||
); | ||
} | ||
} | ||
|
||
class UserWithIntTimestampsViaMutator extends Model | ||
{ | ||
protected $table = 'users'; | ||
|
||
protected $guarded = []; | ||
|
||
protected function getUpdatedAtAttribute($value) | ||
{ | ||
return Carbon::parse($value); | ||
} | ||
|
||
protected function setUpdatedAtAttribute($value) | ||
{ | ||
$this->attributes['updated_at'] = Carbon::parse($value)->timestamp; | ||
} | ||
|
||
protected function getCreatedAtAttribute($value) | ||
{ | ||
return Carbon::parse($value); | ||
} | ||
|
||
protected function setCreatedAtAttribute($value) | ||
{ | ||
$this->attributes['created_at'] = Carbon::parse($value)->timestamp; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The outer
array_key_exists
check is a micro optimisation here, as per the performance section of the description. It can be safely removed if we don't want it.