Skip to content

Commit

Permalink
add test for order creation with free transport/payment promo code
Browse files Browse the repository at this point in the history
  • Loading branch information
vitek-rostislav committed Dec 17, 2024
1 parent 095688a commit 266a12d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function testMinimalOrderAsAuthenticatedUser(): void
$expectedOrderItems = $this->getExpectedOrderItems();

$expected = [
'orderCreated' => true,
'order' => [
'transport' => [
'name' => t('Czech post', [], Translator::DATA_FIXTURES_TRANSLATION_DOMAIN, $firstDomainLocale),
Expand Down Expand Up @@ -74,6 +75,7 @@ public function testMinimalOrderAsAuthenticatedUser(): void
],
'note' => null,
],
'cart' => null,
];

$response = $this->getResponseContentForGql(__DIR__ . '/graphql/CreateMinimalOrderMutation.graphql', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function testCreateMinimalOrderMutation(): void
$firstDomainLocale = $this->getLocaleForFirstDomain();
$expectedOrderItems = $this->getExpectedOrderItems();
$expected = [
'orderCreated' => true,
'order' => [
'transport' => [
'name' => t('Czech post', [], Translator::DATA_FIXTURES_TRANSLATION_DOMAIN, $firstDomainLocale),
Expand Down Expand Up @@ -56,6 +57,7 @@ public function testCreateMinimalOrderMutation(): void
],
'note' => null,
],
'cart' => null,
];

$product = $this->getReference(ProductDataFixture::PRODUCT_PREFIX . '1', Product::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,22 @@ class OrderWithPromoCodeTest extends GraphQlTestCase
*/
private PromoCodeDataFactory $promoCodeDataFactory;

public function testCreateOrderWithPromoCode()
private const array DEFAULT_ORDER_INPUT_VALUES = [
'firstName' => 'firstName',
'lastName' => 'lastName',
'email' => '[email protected]',
'telephone' => '+53 123456789',
'onCompanyBehalf' => false,
'street' => '123 Fake Street',
'city' => 'Springfield',
'postcode' => '12345',
'country' => 'CZ',
'isDeliveryAddressDifferentFromBilling' => false,
];

public function testCreateOrderWithPromoCode(): void
{
$expectedOrderItems = $this->getExpectedOrderItems();
$expected = [
'data' => [
'CreateOrder' => [
'orderCreated' => true,
'order' => [
'totalPrice' => $this->getSerializedOrderTotalPriceByExpectedOrderItems($expectedOrderItems),
'items' => $expectedOrderItems,
],
'cart' => null,
],
],
];
$cartUuid = $this->addProductToCart();
$this->addCzechPostTransportToCart($cartUuid);
$this->addCashOnDeliveryPaymentToCart($cartUuid);
Expand All @@ -59,7 +60,12 @@ public function testCreateOrderWithPromoCode()

$this->applyPromoCode($cartUuid, $validPromoCode->getCode());

$this->assertQueryWithExpectedArray($this->getMutation($cartUuid), $expected);
$responseData = $this->createOrderAndGetResponseData($cartUuid);

$this->assertTrue($responseData['orderCreated']);
$this->assertSame(self::getSerializedOrderTotalPriceByExpectedOrderItems($expectedOrderItems), $responseData['order']['totalPrice']);
$this->assertSame($expectedOrderItems, $responseData['order']['items']);
$this->assertNull($responseData['cart']);
}

public function testCreateOrderWithInvalidPromoCode(): void
Expand All @@ -77,21 +83,31 @@ public function testCreateOrderWithInvalidPromoCode(): void

$this->promoCodeFacade->edit($validPromoCode->getId(), $promoCodeData);

$mutation = $this->getMutation($cartUuid);
$response = $this->getResponseContentForQuery($mutation);

$this->assertArrayHasKey('data', $response);
$this->assertArrayHasKey('CreateOrder', $response['data']);
$this->assertArrayHasKey('orderCreated', $response['data']['CreateOrder']);
$this->assertFalse($response['data']['CreateOrder']['orderCreated']);
$this->assertArrayHasKey('cart', $response['data']['CreateOrder']);
$this->assertArrayHasKey('promoCode', $response['data']['CreateOrder']['cart']);
$this->assertNull($response['data']['CreateOrder']['cart']['promoCode']);
$this->assertArrayHasKey('modifications', $response['data']['CreateOrder']['cart']);
$this->assertArrayHasKey('promoCodeModifications', $response['data']['CreateOrder']['cart']['modifications']);
$this->assertArrayHasKey('noLongerApplicablePromoCode', $response['data']['CreateOrder']['cart']['modifications']['promoCodeModifications']);
$this->assertCount(1, $response['data']['CreateOrder']['cart']['modifications']['promoCodeModifications']['noLongerApplicablePromoCode']);
$this->assertEquals('test', $response['data']['CreateOrder']['cart']['modifications']['promoCodeModifications']['noLongerApplicablePromoCode'][0]);
$responseData = $this->createOrderAndGetResponseData($cartUuid);

$this->assertArrayHasKey('orderCreated', $responseData);
$this->assertFalse($responseData['orderCreated']);
$this->assertArrayHasKey('cart', $responseData);
$this->assertArrayHasKey('promoCode', $responseData['cart']);
$this->assertNull($responseData['cart']['promoCode']);
$this->assertArrayHasKey('modifications', $responseData['cart']);
$this->assertArrayHasKey('promoCodeModifications', $responseData['cart']['modifications']);
$this->assertArrayHasKey('noLongerApplicablePromoCode', $responseData['cart']['modifications']['promoCodeModifications']);
$this->assertCount(1, $responseData['cart']['modifications']['promoCodeModifications']['noLongerApplicablePromoCode']);
$this->assertEquals('test', $responseData['cart']['modifications']['promoCodeModifications']['noLongerApplicablePromoCode'][0]);
}

public function testOrderWithFreeTransportAndPaymentPromoCode(): void
{
$cartUuid = $this->addProductToCart();
$freeTransportAndPaymentPromoCode = $this->getReferenceForDomain(PromoCodeDataFixture::PROMO_CODE_FOR_FREE_TRANSPORT_PAYMENT, 1, PromoCode::class);
$this->addCzechPostTransportToCart($cartUuid);
$this->addCashOnDeliveryPaymentToCart($cartUuid);
$this->applyPromoCode($cartUuid, $freeTransportAndPaymentPromoCode->getCode());

$responseData = $this->createOrderAndGetResponseData($cartUuid);

$this->assertTransportAndPaymentItemsAreFree($responseData);
}

/**
Expand Down Expand Up @@ -155,67 +171,16 @@ protected function getExpectedOrderItems(): array

/**
* @param string $cartUuid
* @return string
* @return array
*/
private function getMutation(string $cartUuid): string
private function createOrderAndGetResponseData(string $cartUuid): array
{
return 'mutation {
CreateOrder(
input: {
cartUuid: "' . $cartUuid . '"
firstName: "firstName"
lastName: "lastName"
email: "[email protected]"
telephone: "+53 123456789"
onCompanyBehalf: false
street: "123 Fake Street"
city: "Springfield"
postcode: "12345"
country: "CZ"
isDeliveryAddressDifferentFromBilling: false
}
) {
orderCreated
order {
totalPrice {
priceWithVat
priceWithoutVat
vatAmount
}
items {
name
unitPrice {
priceWithVat
priceWithoutVat
vatAmount
}
totalPrice {
priceWithVat
priceWithoutVat
vatAmount
}
quantity
vatRate
unit
type
product {
uuid
}
}
}
cart {
promoCode {
code
type
}
modifications {
promoCodeModifications {
noLongerApplicablePromoCode
}
}
}
}
}';
$response = $this->getResponseContentForGql(__DIR__ . '/graphql/CreateMinimalOrderMutation.graphql', [
'cartUuid' => $cartUuid,
...self::DEFAULT_ORDER_INPUT_VALUES,
]);

return $this->getResponseDataForGraphQlType($response, 'CreateOrder');
}

/**
Expand Down Expand Up @@ -260,4 +225,20 @@ private function addProductToCart(): string

return $response['data']['AddToCart']['cart']['uuid'];
}

/**
* @param array $responseData
*/
private function assertTransportAndPaymentItemsAreFree(array $responseData): void
{
foreach ($responseData['order']['items'] as $item) {
if ($item['type'] === OrderItemTypeEnum::TYPE_TRANSPORT || $item['type'] === OrderItemTypeEnum::TYPE_PAYMENT) {
$this->assertSame(
$this->getFormattedMoneyAmountConvertedToDomainDefaultCurrency('0'),
$item['totalPrice']['priceWithVat'],
sprintf('Total price of %s should be zero', $item['type']),
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mutation CreateMinimalOrderMutation(
deliveryAddressUuid: $deliveryAddressUuid
}
) {
orderCreated
order {
transport {
name
Expand Down Expand Up @@ -87,5 +88,16 @@ mutation CreateMinimalOrderMutation(
}
note
}
cart {
promoCode {
code
type
}
modifications {
promoCodeModifications {
noLongerApplicablePromoCode
}
}
}
}
}

0 comments on commit 266a12d

Please sign in to comment.