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] More flexibility when time traveling in tests #35338

Merged
merged 1 commit into from
Nov 24, 2020

Conversation

Gimcrack
Copy link
Contributor

Originally targeted 8.x #35326 , was informed it was a breaking change, so switched to 9.x

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);

@@ -22,11 +21,11 @@ public function travel($value)
/**
* Travel to another time.
*
* @param \DateTimeInterface $date
* @param \DateTimeInterface|Closure|Carbon|string|false|null $date
Copy link
Member

Choose a reason for hiding this comment

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

Please use FQN here for the classes.

Copy link
Member

Choose a reason for hiding this comment

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

Also please use bool instead of false.

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.

None yet

3 participants