Skip to content

Commit

Permalink
Add test for order packed handler
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Aug 12, 2024
1 parent 3eac072 commit 408f858
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/WebhookHandler/OrderPackedWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private static function assertSame(array $syliusOrderLines, array $peakOrderLine
{
foreach ($syliusOrderLines as $syliusOrderLine) {
foreach ($peakOrderLines as $key => $peakOrderLine) {
if ($peakOrderLine['id'] === $syliusOrderLine['id']) {
if ($peakOrderLine['id'] !== $syliusOrderLine['id']) {
continue;
}

Expand Down
101 changes: 94 additions & 7 deletions tests/Functional/Controller/HandleWebhookControllerActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,38 @@
namespace Tests\Setono\SyliusPeakPlugin\Functional\Controller;

use Setono\SyliusPeakPlugin\Message\Command\UpdateInventory;
use Setono\SyliusPeakPlugin\Model\OrderInterface;
use Sylius\Bundle\CoreBundle\Factory\OrderFactoryInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Core\OrderShippingStates;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Zenstruck\Messenger\Test\InteractsWithMessenger;

final class HandleWebhookControllerActionTest extends WebTestCase
{
use InteractsWithMessenger;

private static KernelBrowser $client;

protected function setUp(): void
{
self::$client = self::createClient();
}

/**
* @test
*/
public function it_handles_stock_adjustments(): void
{
$client = static::createClient();

$client->request(
self::$client->request(
method: 'POST',
uri: '/peak/webhook?name=100',
server: ['CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'],
Expand All @@ -41,17 +56,89 @@ public function it_handles_stock_adjustments(): void
$messages = $this->transport('async')->queue()->messages(UpdateInventory::class);
self::assertCount(1, $messages);

self::assertSame($this->getProductVariantId('Everyday_white_basic_T_Shirt-variant-0'), $messages[0]->productVariant);
self::assertSame($this->getProductVariant('Everyday_white_basic_T_Shirt-variant-0')->getId(), $messages[0]->productVariant);
}

/**
* @test
*/
public function it_handles_order_packed(): void
{
$productVariant = $this->getProductVariant('Everyday_white_basic_T_Shirt-variant-0');

$orderItem = $this->createOrderItem($productVariant);

$order = $this->createOrder('USD', 'en_US');
$order->setCheckoutCompletedAt(new \DateTimeImmutable());
$order->setState(OrderInterface::STATE_NEW);
$order->setCheckoutState(OrderCheckoutStates::STATE_COMPLETED);
$order->setPaymentState(OrderPaymentStates::STATE_PAID);
$order->setShippingState(OrderShippingStates::STATE_READY);
$order->addItem($orderItem);

/** @var OrderRepositoryInterface $orderRepository */
$orderRepository = self::getContainer()->get('sylius.repository.order');
$orderRepository->add($order);

self::$client->request(
method: 'POST',
uri: '/peak/webhook?name=102',
server: ['CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'],
content: json_encode([
'orderId' => (string) $order->getId(),
'pickOrderId' => 123,
'paymentCaptured' => true,
'orderLines' => [
[
'orderLineId' => (string) $orderItem->getId(),
'quantity' => 1,
],
],
], \JSON_THROW_ON_ERROR),
);

self::assertResponseStatusCodeSame(204);

self::assertSame(OrderInterface::STATE_FULFILLED, $order->getState());
}

private function getProductVariantId(string $productVariantCode): int
private function getProductVariant(string $code): ProductVariantInterface
{
/** @var ProductVariantRepositoryInterface $productVariantRepository */
$productVariantRepository = self::getContainer()->get('sylius.repository.product_variant');

$productVariant = $productVariantRepository->findOneBy(['code' => $productVariantCode]);
$productVariant = $productVariantRepository->findOneBy(['code' => $code]);
self::assertInstanceOf(ProductVariantInterface::class, $productVariant);

return (int) $productVariant->getId();
return $productVariant;
}

private function createOrder(string $currencyCode, string $localeCode): OrderInterface
{
/** @var OrderFactoryInterface $factory */
$factory = self::getContainer()->get('sylius.factory.order');

Check failure on line 119 in tests/Functional/Controller/HandleWebhookControllerActionTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)

UndefinedDocblockClass

tests/Functional/Controller/HandleWebhookControllerActionTest.php:119:9: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sylius\Bundle\CoreBundle\Factory\OrderFactoryInterface does not exist (see https://psalm.dev/200)

Check failure on line 119 in tests/Functional/Controller/HandleWebhookControllerActionTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)

UndefinedDocblockClass

tests/Functional/Controller/HandleWebhookControllerActionTest.php:119:9: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sylius\Bundle\CoreBundle\Factory\OrderFactoryInterface does not exist (see https://psalm.dev/200)

Check failure on line 119 in tests/Functional/Controller/HandleWebhookControllerActionTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~6.4.0)

UndefinedDocblockClass

tests/Functional/Controller/HandleWebhookControllerActionTest.php:119:9: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sylius\Bundle\CoreBundle\Factory\OrderFactoryInterface does not exist (see https://psalm.dev/200)

/** @var OrderInterface $order */
$order = $factory->createNew();

Check failure on line 122 in tests/Functional/Controller/HandleWebhookControllerActionTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)

UndefinedDocblockClass

tests/Functional/Controller/HandleWebhookControllerActionTest.php:122:18: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sylius\Bundle\CoreBundle\Factory\OrderFactoryInterface does not exist (see https://psalm.dev/200)

Check failure on line 122 in tests/Functional/Controller/HandleWebhookControllerActionTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)

UndefinedDocblockClass

tests/Functional/Controller/HandleWebhookControllerActionTest.php:122:18: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sylius\Bundle\CoreBundle\Factory\OrderFactoryInterface does not exist (see https://psalm.dev/200)

Check failure on line 122 in tests/Functional/Controller/HandleWebhookControllerActionTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~6.4.0)

UndefinedDocblockClass

tests/Functional/Controller/HandleWebhookControllerActionTest.php:122:18: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sylius\Bundle\CoreBundle\Factory\OrderFactoryInterface does not exist (see https://psalm.dev/200)
$order->setCurrencyCode($currencyCode);
$order->setLocaleCode($localeCode);

return $order;
}

private function createOrderItem(ProductVariantInterface $productVariant, int $quantity = 1): OrderItemInterface
{
/** @var FactoryInterface $factory */
$factory = self::getContainer()->get('sylius.factory.order_item');

/** @var OrderItemInterface $orderItem */
$orderItem = $factory->createNew();
$orderItem->setVariant($productVariant);

/** @var OrderItemQuantityModifierInterface $orderItemQuantityModifier */
$orderItemQuantityModifier = self::getContainer()->get('sylius.order_item_quantity_modifier');
$orderItemQuantityModifier->modify($orderItem, $quantity);

return $orderItem;
}
}

0 comments on commit 408f858

Please sign in to comment.