From 85cf9249447240060a2ba8396692bc562ecbe014 Mon Sep 17 00:00:00 2001 From: Lorenzo Ruozzi Date: Mon, 11 Dec 2023 11:12:38 +0100 Subject: [PATCH] Import product model from webhook (#102) --- src/Controller/WebhookController.php | 12 +++++++ .../Controller/WebhookControllerTest.php | 36 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/Controller/WebhookController.php b/src/Controller/WebhookController.php index 82cb21bc..8c8db488 100644 --- a/src/Controller/WebhookController.php +++ b/src/Controller/WebhookController.php @@ -14,6 +14,7 @@ use Webgriffe\SyliusAkeneoPlugin\Message\ItemImport; use Webgriffe\SyliusAkeneoPlugin\Product\Importer as ProductImporter; use Webgriffe\SyliusAkeneoPlugin\ProductAssociations\Importer as ProductAssociationsImporter; +use Webgriffe\SyliusAkeneoPlugin\ProductModel\Importer as ProductModelImporter; /** * @psalm-type AkeneoEventProduct = array{ @@ -136,6 +137,17 @@ public function postAction(Request $request): Response $productCode, )); } + if (array_key_exists('code', $resource)) { + $productModelCode = $resource['code']; + $this->logger->debug(sprintf( + 'Dispatching product model import message for %s', + $productModelCode, + )); + $this->messageBus->dispatch(new ItemImport( + ProductModelImporter::AKENEO_ENTITY, + $productModelCode, + )); + } } return new Response(); diff --git a/tests/Integration/Controller/WebhookControllerTest.php b/tests/Integration/Controller/WebhookControllerTest.php index 3ec56a5d..00097d83 100644 --- a/tests/Integration/Controller/WebhookControllerTest.php +++ b/tests/Integration/Controller/WebhookControllerTest.php @@ -8,7 +8,9 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\HttpFoundation\Request; use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\InMemoryProductApi; +use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\InMemoryProductModelApi; use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model\Product; +use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model\ProductModel; use Webgriffe\SyliusAkeneoPlugin\Controller\WebhookController; use Webgriffe\SyliusAkeneoPlugin\Respository\ItemImportResultRepositoryInterface; @@ -29,6 +31,11 @@ protected function setUp(): void $fixtureLoader->load([], [], [], PurgeMode::createDeleteMode()); InMemoryProductApi::addResource(Product::create('PRODUCT')); + + InMemoryProductModelApi::addResource(ProductModel::create('PRODUCT_MODEL', [ + 'family' => 'family', + 'family_variant' => 'family_variant', + ])); } /** @test */ @@ -60,6 +67,35 @@ public function it_imports_created_products_on_akeneo(): void self::assertEquals('Successfully imported item "ProductAssociations" with identifier "PRODUCT" from Akeneo.', $itemImportResults[1]->getMessage()); } + /** @test */ + public function it_imports_created_product_models_on_akeneo(): void + { + $body = ['events' => [ + [ + 'action' => 'product_model.created', + 'event_id' => '1', + 'data' => [ + 'resource' => [ + 'code' => 'PRODUCT_MODEL', + ], + ], + ], + ]]; + $request = new Request([], [], [], [], [], [], json_encode($body, \JSON_THROW_ON_ERROR)); + + $timestamp = (string) time(); + $signature = hash_hmac('sha256', $timestamp . '.' . json_encode($body, \JSON_THROW_ON_ERROR), ''); + + $request->headers->set('x-akeneo-request-timestamp', $timestamp); + $request->headers->set('x-akeneo-request-signature', $signature); + $this->webhookController->postAction($request); + + $itemImportResults = $this->itemImportResultRepository->findAll(); + self::assertCount(2, $itemImportResults); + self::assertEquals('Successfully imported item "Product" with identifier "PRODUCT" from Akeneo.', $itemImportResults[0]->getMessage()); + self::assertEquals('Successfully imported item "ProductModel" with identifier "PRODUCT_MODEL" from Akeneo.', $itemImportResults[1]->getMessage()); + } + /** @test */ public function it_fails_if_secret_is_not_right(): void {