From 6c361c493ffff7d4fa3b0b45acb859d421986dfc Mon Sep 17 00:00:00 2001 From: Norbert Glanc Date: Mon, 14 Feb 2022 12:51:30 +0100 Subject: [PATCH 1/2] Add product bundle in edit product in admin panel --- ...ndleWhenEditNormalProductEventListener.php | 26 +++++++++++++++++++ src/Form/Extension/ProductTypeExtension.php | 8 ------ src/Menu/AdminProductFormMenuListener.php | 8 ------ src/Resources/config/services.xml | 1 + .../config/services/event_listener.xml | 10 +++++++ 5 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 src/EventListener/AddProductToProductBundleWhenEditNormalProductEventListener.php create mode 100644 src/Resources/config/services/event_listener.xml diff --git a/src/EventListener/AddProductToProductBundleWhenEditNormalProductEventListener.php b/src/EventListener/AddProductToProductBundleWhenEditNormalProductEventListener.php new file mode 100644 index 00000000..e09eb5e7 --- /dev/null +++ b/src/EventListener/AddProductToProductBundleWhenEditNormalProductEventListener.php @@ -0,0 +1,26 @@ +getSubject(); + if (null !== $product->getProductBundle() && null === $product->getProductBundle()->getProduct()) { + $product->getProductBundle()->setProduct($product); + } + } +} diff --git a/src/Form/Extension/ProductTypeExtension.php b/src/Form/Extension/ProductTypeExtension.php index 277156b7..24113674 100644 --- a/src/Form/Extension/ProductTypeExtension.php +++ b/src/Form/Extension/ProductTypeExtension.php @@ -10,7 +10,6 @@ namespace BitBag\SyliusProductBundlePlugin\Form\Extension; -use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface; use BitBag\SyliusProductBundlePlugin\Form\Type\ProductBundleType; use Sylius\Bundle\ProductBundle\Form\Type\ProductType; use Symfony\Component\Form\AbstractTypeExtension; @@ -21,13 +20,6 @@ final class ProductTypeExtension extends AbstractTypeExtension { public function buildForm(FormBuilderInterface $builder, array $options): void { - /** @var ProductInterface $product */ - $product = $builder->getData(); - - if (!$product->isBundle()) { - return; - } - $builder ->add('productBundle', ProductBundleType::class, [ 'label' => false, diff --git a/src/Menu/AdminProductFormMenuListener.php b/src/Menu/AdminProductFormMenuListener.php index 7782bfef..ab1d443b 100644 --- a/src/Menu/AdminProductFormMenuListener.php +++ b/src/Menu/AdminProductFormMenuListener.php @@ -10,20 +10,12 @@ namespace BitBag\SyliusProductBundlePlugin\Menu; -use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface; use Sylius\Bundle\AdminBundle\Event\ProductMenuBuilderEvent; final class AdminProductFormMenuListener { public function addItems(ProductMenuBuilderEvent $event): void { - /** @var ProductInterface $product */ - $product = $event->getProduct(); - - if (!$product->isBundle()) { - return; - } - $menu = $event->getMenu(); $menu diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 0ef0affb..0c4f960c 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -11,5 +11,6 @@ + diff --git a/src/Resources/config/services/event_listener.xml b/src/Resources/config/services/event_listener.xml new file mode 100644 index 00000000..797d027c --- /dev/null +++ b/src/Resources/config/services/event_listener.xml @@ -0,0 +1,10 @@ + + + + + + + + + From 4af02a9f5af69114989786882865d419491e737b Mon Sep 17 00:00:00 2001 From: Norbert Glanc Date: Tue, 15 Feb 2022 11:20:16 +0100 Subject: [PATCH 2/2] Add spec for add product in productBundle when edit non bundle product --- composer.json | 3 +- ...WhenEditNormalProductEventListenerSpec.php | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 spec/EventListener/AddProductToProductBundleWhenEditNormalProductEventListenerSpec.php diff --git a/composer.json b/composer.json index e95b56a1..244d03fb 100644 --- a/composer.json +++ b/composer.json @@ -57,7 +57,8 @@ "Tests\\BitBag\\SyliusProductBundlePlugin\\": [ "tests/", "tests/Application/src" - ] + ], + "spec\\BitBag\\SyliusProductBundlePlugin\\": "spec/" }, "classmap": ["tests/Application/Kernel.php"] } diff --git a/spec/EventListener/AddProductToProductBundleWhenEditNormalProductEventListenerSpec.php b/spec/EventListener/AddProductToProductBundleWhenEditNormalProductEventListenerSpec.php new file mode 100644 index 00000000..84df86b7 --- /dev/null +++ b/spec/EventListener/AddProductToProductBundleWhenEditNormalProductEventListenerSpec.php @@ -0,0 +1,39 @@ +shouldHaveType(AddProductToProductBundleWhenEditNormalProductEventListener::class); + } + + public function it_should_add_product_to_product_bundle_if_not_exist_on_pre_create_and_update_event( + ResourceControllerEvent $resourceControllerEvent, + ProductInterface $product, + ProductBundleInterface $productBundle + ): void { + $resourceControllerEvent->getSubject()->willReturn($product); + + $product->getProductBundle()->shouldBeCalled(); + + $product->getProductBundle()->willReturn($productBundle); + + $this->addProductToProductBundle($resourceControllerEvent); + } +}