Skip to content

Commit

Permalink
Restore Date::create() compatibility with no params
Browse files Browse the repository at this point in the history
Restore the historical behavior of create() that uses the current day
by default.

Fixes #434
  • Loading branch information
markstory committed Oct 7, 2023
1 parent 1b563f0 commit 496447a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ChronosDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,13 @@ public function __debugInfo(): array
* @param int $day The day to create an instance with.
* @return static
*/
public static function create(int $year, int $month, int $day)
public static function create(?int $year = null, ?int $month = null, ?int $day = null)
{
$now = static::today();
$year = $year ?? (int)$now->format('Y');
$month = $month ?? $now->format('m');
$day = $day ?? $now->format('d');

$instance = static::createFromFormat(
'Y-m-d',
sprintf('%s-%s-%s', 0, $month, $day)
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/Date/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,26 @@ public function testConstructWithTestNowNoMutation($class)
$class::setTestNow(null);
}

/**
* @dataProvider dateClassProvider
*/
public function testCreateWithTestNowNoParams($class)
{
$class::setTestNow($class::create(2001, 1, 1));
$date = $class::create();
$this->assertDate($date, 2001, 1, 1);
}

/**
* @dataProvider dateClassProvider
*/
public function testCreateWithSomeParams($class)
{
$now = $class::today();
$date = $class::create(2022, 1);
$this->assertDate($date, 2022, 1, (int)$now->format('d'));
}

/**
* @dataProvider dateClassProvider
*/
Expand Down

0 comments on commit 496447a

Please sign in to comment.