Skip to content

Commit

Permalink
Flash helper with plural name
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Jun 20, 2023
1 parent af29556 commit 68ea8b7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
18 changes: 8 additions & 10 deletions src/Component/Symfony/Session/Flash/FlashHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sylius\Component\Resource\Context\Context;
use Sylius\Component\Resource\Context\Option\RequestOption;
use Sylius\Component\Resource\Humanizer\StringHumanizer;
use Sylius\Component\Resource\Metadata\BulkOperationInterface;
use Sylius\Component\Resource\Metadata\Operation;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Translation\TranslatorBagInterface;
Expand Down Expand Up @@ -55,11 +56,6 @@ private function buildMessage(Operation $operation, string $type): string
$resource = $operation->getResource();
Assert::notNull($resource);

$keys = [
sprintf('%s.%s.%s', $resource->getApplicationName() ?? '', $resource->getName() ?? '', $operation->getShortName() ?? ''),
sprintf('sylius.resource.%s', $operation->getShortName() ?? ''),
];

$specifyKey = sprintf('%s.%s.%s', $resource->getApplicationName() ?? '', $resource->getName() ?? '', $operation->getShortName() ?? '');
$defaultKey = sprintf('sylius.resource.%s', $operation->getShortName() ?? '');

Expand All @@ -84,11 +80,13 @@ private function getTranslationParameters(Operation $operation): array
return [];
}

// TODO plural name with Bulk operation
// if ($operation instanceof BulkOperationInterface) {
// return ['%resources%' => ucfirst($resource->getPluralName() ?? '')];
// }
$resourceName = $operation instanceof BulkOperationInterface ? $resource->getPluralName() : $resource->getName();
$humanizedName = ucfirst(StringHumanizer::humanize($resourceName ?? ''));

if ($operation instanceof BulkOperationInterface) {
return ['%resources%' => $humanizedName];
}

return ['%resource%' => ucfirst(StringHumanizer::humanize($resource->getName() ?? ''))];
return ['%resource%' => $humanizedName];
}
}
6 changes: 6 additions & 0 deletions src/Component/spec/State/EventDispatcherProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ function it_dispatches_events_for_index_operation(
$decorated->provide($operation, $context)->shouldBeCalled();

$operationEventDispatcher->dispatch(null, $operation, $context)->willReturn($operationEvent)->shouldBeCalled();
$decorated->provide($operation, $context)->shouldBeCalled();

$operationEventDispatcher->dispatch(null, $operation, $context)->shouldBeCalled();

$this->provide($operation, $context);
}
Expand All @@ -65,6 +68,9 @@ function it_dispatches_events_for_show_operation(
$decorated->provide($operation, $context)->shouldBeCalled();

$operationEventDispatcher->dispatch(null, $operation, $context)->willReturn($operationEvent)->shouldBeCalled();
$decorated->provide($operation, $context)->shouldBeCalled();

$operationEventDispatcher->dispatch(null, $operation, $context)->shouldBeCalled();

$this->provide($operation, $context);
}
Expand Down
38 changes: 32 additions & 6 deletions src/Component/spec/Symfony/Session/Flash/FlashHelperSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Context\Context;
use Sylius\Component\Resource\Context\Option\RequestOption;
use Sylius\Component\Resource\Metadata\BulkDelete;
use Sylius\Component\Resource\Metadata\Create;
use Sylius\Component\Resource\Metadata\Resource;
use Sylius\Component\Resource\Symfony\Session\Flash\FlashHelper;
Expand Down Expand Up @@ -45,7 +46,7 @@ function it_adds_success_flashes_with_specific_message(
TranslatorBagInterface $translator,
MessageCatalogueInterface $messageCatalogue,
): void {
$operation = (new Create())->withResource(new Resource(alias: 'app.dummy', applicationName: 'app', name: 'dummy'));
$operation = (new Create())->withResource(new Resource(alias: 'app.dummy', name: 'dummy', applicationName: 'app'));
$context = new Context(new RequestOption($request->getWrappedObject()));

$request->getSession()->willReturn($session);
Expand All @@ -70,7 +71,7 @@ function it_adds_success_flashes_with_default_message(
TranslatorBagInterface $translator,
MessageCatalogueInterface $messageCatalogue,
): void {
$operation = (new Create())->withResource(new Resource(alias: 'app.dummy', applicationName: 'app', name: 'dummy'));
$operation = (new Create())->withResource(new Resource(alias: 'app.dummy', name: 'dummy', applicationName: 'app'));
$context = new Context(new RequestOption($request->getWrappedObject()));

$request->getSession()->willReturn($session);
Expand All @@ -94,7 +95,7 @@ function it_adds_success_flashes_with_default_message_when_translator_is_not_a_b
FlashBagInterface $flashBag,
TranslatorInterface $translator,
): void {
$operation = (new Create())->withResource(new Resource(alias: 'app.dummy', applicationName: 'app', name: 'dummy'));
$operation = (new Create())->withResource(new Resource(alias: 'app.dummy', name: 'dummy', applicationName: 'app'));
$context = new Context(new RequestOption($request->getWrappedObject()));

$request->getSession()->willReturn($session);
Expand All @@ -115,7 +116,7 @@ function it_adds_success_flashes_with_humanized_message(
TranslatorBagInterface $translator,
MessageCatalogueInterface $messageCatalogue,
): void {
$operation = (new Create())->withResource(new Resource(alias: 'app.dummy', applicationName: 'app', name: 'admin_user'));
$operation = (new Create())->withResource(new Resource(alias: 'app.dummy', name: 'admin_user', applicationName: 'app'));
$context = new Context(new RequestOption($request->getWrappedObject()));

$request->getSession()->willReturn($session);
Expand All @@ -126,9 +127,34 @@ function it_adds_success_flashes_with_humanized_message(

$messageCatalogue->has('app.admin_user.create', 'flashes')->willReturn(true)->shouldBeCalled();

$translator->trans('app.admin_user.create', ['%resource%' => 'Admin user'], 'flashes')->willReturn('Dummy was created successfully.')->shouldBeCalled();
$translator->trans('app.admin_user.create', ['%resource%' => 'Admin user'], 'flashes')->willReturn('Admin user was created successfully.')->shouldBeCalled();

$flashBag->add('success', 'Dummy was created successfully.')->shouldBeCalled();
$flashBag->add('success', 'Admin user was created successfully.')->shouldBeCalled();

$this->addSuccessFlash($operation, $context);
}

function it_adds_success_flashes_with_humanized_message_and_plural_name_on_bulk_operation(
Request $request,
SessionInterface $session,
FlashBagInterface $flashBag,
TranslatorBagInterface $translator,
MessageCatalogueInterface $messageCatalogue,
): void {
$operation = (new BulkDelete())->withResource(new Resource(alias: 'app.dummy', name: 'admin_user', pluralName: 'admin_users', applicationName: 'app'));
$context = new Context(new RequestOption($request->getWrappedObject()));

$request->getSession()->willReturn($session);

$session->getBag('flashes')->willReturn($flashBag);

$translator->getCatalogue()->willReturn($messageCatalogue);

$messageCatalogue->has('app.admin_user.bulk_delete', 'flashes')->willReturn(true)->shouldBeCalled();

$translator->trans('app.admin_user.bulk_delete', ['%resources%' => 'Admin users'], 'flashes')->willReturn('Admin users was removed successfully.')->shouldBeCalled();

$flashBag->add('success', 'Admin users was removed successfully.')->shouldBeCalled();

$this->addSuccessFlash($operation, $context);
}
Expand Down

0 comments on commit 68ea8b7

Please sign in to comment.