Skip to content

Commit

Permalink
Merge branch 'EasyCorp:4.x' into 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
tezrik authored Jul 31, 2024
2 parents 6041109 + e4f9521 commit 76bc4c6
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 7 deletions.
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
6 changes: 3 additions & 3 deletions src/Factory/AdminContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ private function getDashboardDto(Request $request, DashboardControllerInterface

foreach ($dashboardControllerRoutes as $routeName => $controller) {
if ($controller === $dashboardController) {
// if present, remove the suffix of i18n route names (it's a two-letter locale at the end
// of the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.es' -> remove '.es')
$dashboardRouteName = preg_replace('~\.\w{2}$~', '', $routeName);
// if present, remove the suffix of i18n route names (it's the content after the last dot
// in the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.en_US' -> remove '.en_US')
$dashboardRouteName = preg_replace('~\.[a-z]{2}(_[A-Z]{2})?$~', '', $routeName);

break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Router/AdminUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ public function generateUrl(): string
$this->dashboardRoute = $this->dashboardControllerRegistry->getFirstDashboardRoute();
}

// if present, remove the suffix of i18n route names (it's a two-letter locale at the end
// of the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.es' -> remove '.es')
$this->dashboardRoute = preg_replace('~\.\w{2}$~', '', $this->dashboardRoute);
// if present, remove the suffix of i18n route names (it's the content after the last dot
// in the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.en_US' -> remove '.en_US')
$this->dashboardRoute = preg_replace('~\.[a-z]{2}(_[A-Z]{2})?$~', '', $this->dashboardRoute);

// this removes any parameter with a NULL value
$routeParameters = array_filter(
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 76bc4c6

Please sign in to comment.