-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[5.4] Add fresh method on Eloquent\Collection. (#19616)
* Add fresh method on Eloquent/Collection. * Rewrite completely the method to avoid multiple database calls. Add full tests for Model::fresh() and Collection::fresh() methods. * Update tests for php5.6 and Carbon v1.20.0
- Loading branch information
1 parent
c29b004
commit ff65fc9
Showing
2 changed files
with
64 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -1069,6 +1069,48 @@ public function testIsAfterRetrievingTheSameModel() | |
$this->assertTrue($saved->is($retrieved)); | ||
} | ||
|
||
public function testFreshMethodOnModel() | ||
{ | ||
$now = \Carbon\Carbon::now(); | ||
\Carbon\Carbon::setTestNow($now); | ||
|
||
$storedUser1 = EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']); | ||
$storedUser1->newQuery()->update(['email' => '[email protected]', 'name' => 'Mathieu TUDISCO']); | ||
$freshStoredUser1 = $storedUser1->fresh(); | ||
|
||
$storedUser2 = EloquentTestUser::create(['id' => 2, 'email' => '[email protected]']); | ||
$storedUser2->newQuery()->update(['email' => '[email protected]']); | ||
$freshStoredUser2 = $storedUser2->fresh(); | ||
|
||
$notStoredUser = new EloquentTestUser(['id' => 3, 'email' => '[email protected]']); | ||
$freshNotStoredUser = $notStoredUser->fresh(); | ||
|
||
$this->assertEquals(['id' => 1, 'email' => '[email protected]', 'created_at' => $now, 'updated_at' => $now], $storedUser1->toArray()); | ||
$this->assertEquals(['id' => 1, 'name' => 'Mathieu TUDISCO', 'email' => '[email protected]', 'created_at' => $now, 'updated_at' => $now], $freshStoredUser1->toArray()); | ||
$this->assertInstanceOf(EloquentTestUser::class, $storedUser1); | ||
|
||
$this->assertEquals(['id' => 2, 'email' => '[email protected]', 'created_at' => $now, 'updated_at' => $now], $storedUser2->toArray()); | ||
$this->assertEquals(['id' => 2, 'name' => null, 'email' => '[email protected]', 'created_at' => $now, 'updated_at' => $now], $freshStoredUser2->toArray()); | ||
$this->assertInstanceOf(EloquentTestUser::class, $storedUser2); | ||
|
||
$this->assertEquals(['id' => 3, 'email' => '[email protected]'], $notStoredUser->toArray()); | ||
$this->assertEquals(null, $freshNotStoredUser); | ||
} | ||
|
||
public function testFreshMethodOnCollection() | ||
{ | ||
EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']); | ||
EloquentTestUser::create(['id' => 2, 'email' => '[email protected]']); | ||
|
||
$users = EloquentTestUser::all() | ||
->add(new EloquentTestUser(['id' => 3, 'email' => '[email protected]'])); | ||
|
||
EloquentTestUser::find(1)->update(['name' => 'Mathieu TUDISCO']); | ||
EloquentTestUser::find(2)->update(['email' => '[email protected]']); | ||
|
||
$this->assertEquals($users->map->fresh(), $users->fresh()); | ||
} | ||
|
||
/** | ||
* Helpers... | ||
*/ | ||
|