Skip to content

Commit

Permalink
minor #6393 Add tests for the Crud methods that configure date/time/d…
Browse files Browse the repository at this point in the history
…ateTime (javiereguiluz)

This PR was merged into the 4.x branch.

Discussion
----------

Add tests for the Crud methods that configure date/time/dateTime

Commits
-------

3503223 Add tests for the Crud methods that configure date/time/dateTime
  • Loading branch information
javiereguiluz committed Jul 30, 2024
2 parents bb6ae0c + 3503223 commit ed59756
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Config/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function setDateTimeFormat(string $dateFormatOrPattern, string $timeForma
}

if (!$isDatePattern && !\in_array($timeFormat, DateTimeField::VALID_DATE_FORMATS, true)) {
throw new \InvalidArgumentException(sprintf('The value of the time format can only be one of the following: %s (but "%s" was given).', implode(', ', DateTimeField::VALID_DATE_FORMATS), $timeFormat));
throw new \InvalidArgumentException(sprintf('When using a predefined format for the date, the time format must also be a predefined format (one of the following: %s) but "%s" was given.', implode(', ', DateTimeField::VALID_DATE_FORMATS), $timeFormat));
}

$this->dto->setDateTimePattern($dateFormatOrPattern, $timeFormat);
Expand Down
115 changes: 115 additions & 0 deletions tests/Config/CrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,119 @@ public function testSetDecimalSeparator(string $separator)

$this->assertSame($separator, $crudConfig->getAsDto()->getDecimalSeparator());
}

/**
* @testWith ["short", "M/d/yy"]
* ["medium", "MMM d, y"]
* ["long", "MMMM d, y"]
* ["full", "EEEE, MMMM d, y"]
* ["EEEE", "EEEE"]
* ["MMMM/d, EEEE", "MMMM/d, EEEE"]
* // the following invalid format is used on purpose to test that the method accept any custom format
* ["this is wrong", "this is wrong"]
*
*/
public function testSetDateFormat(string $dateFormat, string $parsedFormat)
{
$crudConfig = Crud::new();
$crudConfig->setDateFormat($dateFormat);

$this->assertSame($parsedFormat, $crudConfig->getAsDto()->getDatePattern());
}

/**
* @testWith ["none", ""]
*/
public function testSetDateFormatWithInvalidFormat(string $dateFormat)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The first argument of the "EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Crud::setDateFormat()" method cannot be "none" or an empty string. Use either the special date formats (short, medium, long, full) or a datetime Intl pattern.');

$crudConfig = Crud::new();
$crudConfig->setDateFormat($dateFormat);
}

/**
* @testWith ["short", "h:mm a"]
* ["medium", "h:mm:ss a"]
* ["long", "h:mm:ss a z"]
* ["full", "h:mm:ss a zzzz"]
* ["zzzz", "zzzz"]
* ["ss, a, mm", "ss, a, mm"]
* // the following invalid format is used on purpose to test that the method accept any custom format
* ["this is wrong", "this is wrong"]
*
*/
public function testSetTimeFormat(string $timeFormat, string $parsedFormat)
{
$crudConfig = Crud::new();
$crudConfig->setTimeFormat($timeFormat);

$this->assertSame($parsedFormat, $crudConfig->getAsDto()->getTimePattern());
}

/**
* @testWith ["none", ""]
*/
public function testSetTimeFormatWithInvalidFormat(string $timeFormat)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The first argument of the "EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Crud::setTimeFormat()" method cannot be "none" or an empty string. Use either the special time formats (short, medium, long, full) or a datetime Intl pattern.');

$crudConfig = Crud::new();
$crudConfig->setTimeFormat($timeFormat);
}

/**
* @testWith ["none", "short"]
* ["none", "medium"]
* ["none", "long"]
* ["none", "full"]
* ["short", "none"]
* ["short", "short"]
* ["short", "medium"]
* ["short", "long"]
* ["short", "full"]
* ["medium", "none"]
* ["medium", "short"]
* ["medium", "medium"]
* ["medium", "long"]
* ["medium", "full"]
* ["long", "none"]
* ["long", "short"]
* ["long", "medium"]
* ["long", "long"]
* ["long", "full"]
* ["full", "none"]
* ["full", "short"]
* ["full", "medium"]
* ["full", "long"]
* ["full", "full"]
* ["MMMM/d, EEEE ss, a, mm", "none"]
* // the following invalid formats are used on purpose to test that the method accept any custom formats
* ["this is wrong", "this is wrong"]
*/
public function testSetDateTimeFormat(string $dateFormat, string $timeFormat)
{
$crudConfig = Crud::new();
$crudConfig->setDateTimeFormat($dateFormat, $timeFormat);

$this->assertSame([$dateFormat, $timeFormat], $crudConfig->getAsDto()->getDateTimePattern());
}

/**
* @testWith ["", "short", "The first argument of the \"EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Crud::setDateTimeFormat()\" method cannot be an empty string. Use either a date format (none, short, medium, long, full) or a datetime Intl pattern."]
* ["none", "", "The values of the arguments of \"EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Crud::setDateTimeFormat()\" cannot be \"none\" or an empty string at the same time. Change any of them (or both)."]
* ["none", "none", "The values of the arguments of \"EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Crud::setDateTimeFormat()\" cannot be \"none\" or an empty string at the same time. Change any of them (or both)."]
* ["EEEE", "zzzz", "When the first argument of \"EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Crud::setDateTimeFormat()\" is a datetime pattern, you cannot set the time format in the second argument (define the time format inside the datetime pattern)."]
* ["long", "zzz", "When using a predefined format for the date, the time format must also be a predefined format (one of the following: none, short, medium, long, full) but \"zzz\" was given."]
*/
public function testSetDateTimeExceptions(string $dateFormat, string $timeFormat, string $exceptionMessage)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($exceptionMessage);

$crudConfig = Crud::new();
$crudConfig->setDateTimeFormat($dateFormat, $timeFormat);
}
}

0 comments on commit ed59756

Please sign in to comment.