-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[9.x] Fix Carbon::setTestNow usage #40790
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,13 @@ | |
|
||
class CacheArrayStoreTest extends TestCase | ||
{ | ||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
Carbon::setTestNow(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to reset multiple time after each test |
||
} | ||
|
||
public function testItemsCanBeSetAndRetrieved() | ||
{ | ||
$store = new ArrayStore; | ||
|
@@ -37,15 +44,13 @@ public function testMultipleItemsCanBeSetAndRetrieved() | |
|
||
public function testItemsCanExpire() | ||
{ | ||
Carbon::setTestNow(Carbon::now()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useless, Carbon will be now by default |
||
$store = new ArrayStore; | ||
|
||
$store->put('foo', 'bar', 10); | ||
Carbon::setTestNow(Carbon::now()->addSeconds(10)->addSecond()); | ||
$result = $store->get('foo'); | ||
|
||
$this->assertNull($result); | ||
Carbon::setTestNow(null); | ||
} | ||
|
||
public function testStoreItemForeverProperlyStoresInArray() | ||
|
@@ -77,15 +82,13 @@ public function testNonExistingKeysCanBeIncremented() | |
|
||
public function testExpiredKeysAreIncrementedLikeNonExistingKeys() | ||
{ | ||
Carbon::setTestNow(Carbon::now()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, we dont want to compare a fake now |
||
$store = new ArrayStore; | ||
|
||
$store->put('foo', 999, 10); | ||
Carbon::setTestNow(Carbon::now()->addSeconds(10)->addSecond()); | ||
$result = $store->increment('foo'); | ||
|
||
$this->assertEquals(1, $result); | ||
Carbon::setTestNow(null); | ||
} | ||
|
||
public function testValuesCanBeDecremented() | ||
|
@@ -134,7 +137,6 @@ public function testCannotAcquireLockTwice() | |
|
||
public function testCanAcquireLockAgainAfterExpiry() | ||
{ | ||
Carbon::setTestNow(Carbon::now()); | ||
$store = new ArrayStore; | ||
$lock = $store->lock('foo', 10); | ||
$lock->acquire(); | ||
|
@@ -146,6 +148,7 @@ public function testCanAcquireLockAgainAfterExpiry() | |
public function testLockExpirationLowerBoundary() | ||
{ | ||
Carbon::setTestNow(Carbon::now()); | ||
|
||
$store = new ArrayStore; | ||
$lock = $store->lock('foo', 10); | ||
$lock->acquire(); | ||
|
@@ -156,7 +159,6 @@ public function testLockExpirationLowerBoundary() | |
|
||
public function testLockWithNoExpirationNeverExpires() | ||
{ | ||
Carbon::setTestNow(Carbon::now()); | ||
$store = new ArrayStore; | ||
$lock = $store->lock('foo'); | ||
$lock->acquire(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,20 +11,6 @@ | |
|
||
class CacheFileStoreTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We never need to pretent that Carbon::now is different than now in this test class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lucasmichot https://github.com/laravel/framework/runs/5798854285?check_suite_focus=true This causes This brings into question every other |
||
parent::setUp(); | ||
|
||
Carbon::setTestNow(Carbon::now()); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
Carbon::setTestNow(null); | ||
} | ||
|
||
public function testNullIsReturnedIfFileDoesntExist() | ||
{ | ||
$files = $this->mockFilesystem(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,8 @@ class CacheRepositoryTest extends TestCase | |
protected function tearDown(): void | ||
{ | ||
m::close(); | ||
Carbon::setTestNow(); | ||
|
||
Carbon::setTestNow(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
public function testGetReturnsValueFromCache() | ||
|
@@ -96,11 +97,6 @@ public function testRememberMethodCallsPutAndReturnsDefault() | |
}); | ||
$this->assertSame('bar', $result); | ||
|
||
/* | ||
* Use Carbon object... | ||
*/ | ||
Carbon::setTestNow(Carbon::now()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No fake now here |
||
|
||
$repo = $this->getRepository(); | ||
$repo->getStore()->shouldReceive('get')->times(2)->andReturn(null); | ||
$repo->getStore()->shouldReceive('put')->once()->with('foo', 'bar', 602); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,10 @@ class DatabaseEloquentBuilderTest extends TestCase | |
{ | ||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
Carbon::setTestNow(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can reset only once in the tearDown |
||
|
||
m::close(); | ||
} | ||
|
||
|
@@ -1665,8 +1669,6 @@ public function testUpdate() | |
|
||
$result = $builder->update(['foo' => 'bar']); | ||
$this->assertEquals(1, $result); | ||
|
||
Carbon::setTestNow(null); | ||
} | ||
|
||
public function testUpdateWithTimestampValue() | ||
|
@@ -1711,8 +1713,6 @@ public function testUpdateWithAlias() | |
|
||
$result = $builder->from('table as alias')->update(['foo' => 'bar']); | ||
$this->assertEquals(1, $result); | ||
|
||
Carbon::setTestNow(null); | ||
} | ||
|
||
public function testUpsert() | ||
|
@@ -1736,8 +1736,6 @@ public function testUpsert() | |
$result = $builder->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], ['email']); | ||
|
||
$this->assertEquals(2, $result); | ||
|
||
Carbon::setTestNow(null); | ||
} | ||
|
||
public function testWithCastsMethod() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,6 +155,8 @@ protected function createSchema() | |
*/ | ||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
foreach (['default', 'second_connection'] as $connection) { | ||
$this->schema($connection)->drop('users'); | ||
$this->schema($connection)->drop('friends'); | ||
|
@@ -165,6 +167,8 @@ protected function tearDown(): void | |
|
||
Relation::morphMap([], false); | ||
Eloquent::unsetConnectionResolver(); | ||
|
||
Carbon::setTestNow(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
} | ||
|
||
/** | ||
|
@@ -1627,8 +1631,6 @@ public function testUpdatingChildModelTouchesParent() | |
|
||
$this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is not touching model own timestamps.'); | ||
$this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is not touching models related timestamps.'); | ||
|
||
Carbon::setTestNow($before); | ||
} | ||
|
||
public function testMultiLevelTouchingWorks() | ||
|
@@ -1647,8 +1649,6 @@ public function testMultiLevelTouchingWorks() | |
|
||
$this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is not touching models related timestamps.'); | ||
$this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is not touching models related timestamps.'); | ||
|
||
Carbon::setTestNow($before); | ||
} | ||
|
||
public function testDeletingChildModelTouchesParentTimestamps() | ||
|
@@ -1666,8 +1666,6 @@ public function testDeletingChildModelTouchesParentTimestamps() | |
$post->delete(); | ||
|
||
$this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is not touching models related timestamps.'); | ||
|
||
Carbon::setTestNow($before); | ||
} | ||
|
||
public function testTouchingChildModelUpdatesParentsTimestamps() | ||
|
@@ -1686,8 +1684,6 @@ public function testTouchingChildModelUpdatesParentsTimestamps() | |
|
||
$this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is not touching model own timestamps.'); | ||
$this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is not touching models related timestamps.'); | ||
|
||
Carbon::setTestNow($before); | ||
} | ||
|
||
public function testTouchingChildModelRespectsParentNoTouching() | ||
|
@@ -1715,8 +1711,6 @@ public function testTouchingChildModelRespectsParentNoTouching() | |
$before->isSameDay($user->fresh()->updated_at), | ||
'It is touching model own timestamps in withoutTouching scope, when it should not.' | ||
); | ||
|
||
Carbon::setTestNow($before); | ||
} | ||
|
||
public function testUpdatingChildPostRespectsNoTouchingDefinition() | ||
|
@@ -1737,8 +1731,6 @@ public function testUpdatingChildPostRespectsNoTouchingDefinition() | |
|
||
$this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is not touching model own timestamps when it should.'); | ||
$this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models relationships when it should be disabled.'); | ||
|
||
Carbon::setTestNow($before); | ||
} | ||
|
||
public function testUpdatingModelInTheDisabledScopeTouchesItsOwnTimestamps() | ||
|
@@ -1759,8 +1751,6 @@ public function testUpdatingModelInTheDisabledScopeTouchesItsOwnTimestamps() | |
|
||
$this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.'); | ||
$this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.'); | ||
|
||
Carbon::setTestNow($before); | ||
} | ||
|
||
public function testDeletingChildModelRespectsTheNoTouchingRule() | ||
|
@@ -1780,8 +1770,6 @@ public function testDeletingChildModelRespectsTheNoTouchingRule() | |
}); | ||
|
||
$this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.'); | ||
|
||
Carbon::setTestNow($before); | ||
} | ||
|
||
public function testRespectedMultiLevelTouchingChain() | ||
|
@@ -1802,8 +1790,6 @@ public function testRespectedMultiLevelTouchingChain() | |
|
||
$this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.'); | ||
$this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.'); | ||
|
||
Carbon::setTestNow($before); | ||
} | ||
|
||
public function testTouchesGreatParentEvenWhenParentIsInNoTouchScope() | ||
|
@@ -1824,8 +1810,6 @@ public function testTouchesGreatParentEvenWhenParentIsInNoTouchScope() | |
|
||
$this->assertTrue($before->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.'); | ||
$this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.'); | ||
|
||
Carbon::setTestNow($before); | ||
} | ||
|
||
public function testCanNestCallsOfNoTouching() | ||
|
@@ -1848,8 +1832,6 @@ public function testCanNestCallsOfNoTouching() | |
|
||
$this->assertTrue($before->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.'); | ||
$this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.'); | ||
|
||
Carbon::setTestNow($before); | ||
} | ||
|
||
public function testCanPassArrayOfModelsToIgnore() | ||
|
@@ -1870,8 +1852,6 @@ public function testCanPassArrayOfModelsToIgnore() | |
|
||
$this->assertTrue($before->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.'); | ||
$this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.'); | ||
|
||
Carbon::setTestNow($before); | ||
} | ||
|
||
public function testWhenBaseModelIsIgnoredAllChildModelsAreIgnored() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,13 +42,6 @@ class DatabaseEloquentModelTest extends TestCase | |
{ | ||
use InteractsWithTime; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Carbon::setTestNow(Carbon::now()); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
@@ -2058,8 +2051,6 @@ public function testScopesMethod() | |
$model = new EloquentModelStub; | ||
$this->addMockConnection($model); | ||
|
||
Carbon::setTestNow(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Carbon is already reset to now anyway |
||
|
||
$scopes = [ | ||
'published', | ||
'category' => 'Laravel', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ class DatabaseEloquentSoftDeletesIntegrationTest extends TestCase | |
{ | ||
protected function setUp(): void | ||
{ | ||
Carbon::setTestNow(Carbon::now()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useless |
||
parent::setUp(); | ||
|
||
$db = new DB; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ class DatabaseEloquentTimestampsTest extends TestCase | |
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$db = new DB; | ||
|
||
$db->addConnection([ | ||
|
@@ -22,7 +24,6 @@ protected function setUp(): void | |
$db->setAsGlobal(); | ||
|
||
$this->createSchema(); | ||
Carbon::setTestNow(Carbon::now()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useless |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ public function testDeletedAtIsAddedToCastsAsDefaultType() | |
|
||
public function testDeletedAtIsCastToCarbonInstance() | ||
{ | ||
Carbon::setTestNow(Carbon::now()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useless |
||
$expected = Carbon::createFromFormat('Y-m-d H:i:s', '2018-12-29 13:59:39'); | ||
$model = new SoftDeletingModel(['deleted_at' => $expected->format('Y-m-d H:i:s')]); | ||
|
||
|
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.
We never need to pretent that Carbon::now is different than now in this test class