Skip to content

[8.x] More flexibility when time traveling in tests #35326

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

Closed
wants to merge 2 commits into from
Closed

[8.x] More flexibility when time traveling in tests #35326

wants to merge 2 commits into from

Conversation

Gimcrack
Copy link
Contributor

The purpose of this PR is to bring parity to the InteractsWithTime::travelTo parameter type hint so it matches the Carbon::setTestNow type hint.

IMO this will allow for more flexibility when time traveling in tests. Here are some examples that would be supported if this PR were merged.

Currently, these all result in a type error.

$this->travelTo('Sunday'); 
$this->travelTo('tomorrow');
$this->travelTo('Monday +1 week');
$this->travelTo('+1 week 2 days 4 hours 2 seconds');
$this->travelTo('2020-11-23');

Meanwhile, these all work fine.

Carbon::setTestNow('Sunday'); 
Carbon::setTestNow('tomorrow');
Carbon::setTestNow('Monday +1 week');
Carbon::setTestNow('+1 week 2 days 4 hours 2 seconds');
Carbon::setTestNow('2020-11-23');

Unless I'm missing something, the DateTimeInterface requirement seems like an arbitrary restriction when Carbon's implementation of setTestNow has no such restriction.

// InteractsWithTime.php
public function travelTo(DateTimeInterface $date, $callback = null)
{
    Carbon::setTestNow($date);
    ...
{

// CarbonInterface.php
@param Closure|static|string|false|null $testNow real or mock Carbon instance
public static function setTestNow($testNow = null);

@taylorotwell
Copy link
Member

I'm not sure if we can remove that type hint on a patch release?

@Gimcrack
Copy link
Contributor Author

That's a fair point. Should I PR it to master or wait for more input?

@Gimcrack Gimcrack changed the title More flexibility when time traveling in tests [8.x] More flexibility when time traveling in tests Nov 23, 2020
@driesvints
Copy link
Member

This is indeed a breaking change and best sent to master.

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