Skip to content
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

Merged
merged 2 commits into from
Feb 3, 2022
Merged

[9.x] Fix Carbon::setTestNow usage #40790

merged 2 commits into from
Feb 3, 2022

Conversation

lucasmichot
Copy link
Contributor

@lucasmichot lucasmichot commented Feb 3, 2022

In many place in the tests, the usage of Carbon::setTestNow does not make sense or is not used properly.
This PR fixes that.

See PR code comments.


In a ideal world, a way to go would be to have an abstract test class that every other test (excepted integration) class could extend, this abstract class would close Mockery and reset Carbon in its tearDown method.

@lucasmichot lucasmichot marked this pull request as draft February 3, 2022 15:45
@@ -13,19 +13,11 @@

class AuthDatabaseTokenRepositoryTest extends TestCase
{
Copy link
Contributor Author

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

@@ -37,15 +44,13 @@ public function testMultipleItemsCanBeSetAndRetrieved()

public function testItemsCanExpire()
{
Carbon::setTestNow(Carbon::now());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless, Carbon will be now by default

{
parent::tearDown();

Carbon::setTestNow(null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to reset multiple time after each test

@@ -77,15 +82,13 @@ public function testNonExistingKeysCanBeIncremented()

public function testExpiredKeysAreIncrementedLikeNonExistingKeys()
{
Carbon::setTestNow(Carbon::now());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we dont want to compare a fake now

@@ -11,20 +11,6 @@

class CacheFileStoreTest extends TestCase
{
protected function setUp(): void
{
Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The 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 testIncrementDoesNotExtendCacheLife() to become brittle, intermittently failing if the second ticks over in the middle of this test case. Test setup Carbon::now()->addSeconds(50)->getTimestamp() and FileStore@put() -> InteractsWithTime@availableAt() code Carbon::now()->addRealSeconds($delay)->getTimestamp() must originate from the exact same Carbon::setTestNow() marker. When it doesn't, hash generation in the cache service can mismatch the setup test mock.

This brings into question every other Carbon::setTestNow(Carbon::now()) removal in this PR.

@@ -23,7 +23,8 @@ class CacheRepositoryTest extends TestCase
protected function tearDown(): void
{
m::close();
Carbon::setTestNow();

Carbon::setTestNow(null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null is set for consistency sake.

/*
* Use Carbon object...
*/
Carbon::setTestNow(Carbon::now());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No fake now here

@@ -26,6 +26,10 @@ class DatabaseEloquentBuilderTest extends TestCase
{
protected function tearDown(): void
{
parent::tearDown();

Carbon::setTestNow(null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can reset only once in the tearDown

@@ -165,6 +167,8 @@ protected function tearDown(): void

Relation::morphMap([], false);
Eloquent::unsetConnectionResolver();

Carbon::setTestNow(null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@@ -2058,7 +2051,6 @@ public function testScopesMethod()
$model = new EloquentModelStub;
$this->addMockConnection($model);

Carbon::setTestNow();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Carbon is already reset to now anyway

@@ -18,7 +18,7 @@ class DatabaseEloquentSoftDeletesIntegrationTest extends TestCase
{
protected function setUp(): void
{
Carbon::setTestNow(Carbon::now());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless

@@ -22,7 +24,6 @@ protected function setUp(): void
$db->setAsGlobal();

$this->createSchema();
Carbon::setTestNow(Carbon::now());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless

@@ -19,7 +19,6 @@ public function testDeletedAtIsAddedToCastsAsDefaultType()

public function testDeletedAtIsCastToCarbonInstance()
{
Carbon::setTestNow(Carbon::now());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless

@@ -37,8 +37,6 @@ public function testLocksCanBeAcquiredAndReleased()

public function testLocksCanBlockForSeconds()
{
Carbon::setTestNow();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless

@@ -27,7 +27,7 @@ public function testCanProperlyLogFailedJob()
return $uuid;
});

Carbon::setTestNow($now = CarbonImmutable::now());
$now = CarbonImmutable::now();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only use now, without faking it

@@ -36,7 +36,8 @@ class ValidationValidatorTest extends TestCase
{
protected function tearDown(): void
{
Carbon::setTestNow();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Carbon::setTestNow is already called in one of the test

@@ -4019,6 +4020,8 @@ public function testDateEqualsRespectsCarbonTestNowWhenParameterIsRelative()

$v = new Validator($trans, ['x' => new Carbon('2018-01-01')], ['x' => 'date_equals:tomorrow']);
$this->assertTrue($v->fails());

Carbon::setTestNow(null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to call Carbon::setTestNow in tearDown

@lucasmichot lucasmichot marked this pull request as ready for review February 3, 2022 15:59
@taylorotwell taylorotwell merged commit 2667e69 into laravel:9.x Feb 3, 2022
@GrahamCampbell GrahamCampbell changed the title [9.x] Fix Carbon::setTestNow usage. [9.x] Fix Carbon::setTestNow usage Feb 3, 2022
@lucasmichot lucasmichot deleted the feature/9.x/carbon-set-test-now branch August 6, 2022 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants