Skip to content

Commit

Permalink
Import product model from webhook (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Dec 13, 2023
1 parent 47565ce commit 85cf924
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Controller/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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();
Expand Down
36 changes: 36 additions & 0 deletions tests/Integration/Controller/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 */
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 85cf924

Please sign in to comment.