Skip to content

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
use stdClass;

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


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


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

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.

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

{
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

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

$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

{
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

$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


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


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

});

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

{
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

$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