MergeCartTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Feature\GraphQL;
  3. use Webkul\BagistoApi\Models\StorefrontKey;
  4. use Webkul\BagistoApi\Tests\GraphQLTestCase;
  5. use Webkul\Product\Models\Product;
  6. class MergeCartTest extends GraphQLTestCase
  7. {
  8. public function setUp(): void
  9. {
  10. parent::setUp();
  11. $this->storefrontKey = StorefrontKey::generateKey();
  12. StorefrontKey::query()->create([
  13. 'name' => 'Merge Cart Test',
  14. 'key' => $this->storefrontKey,
  15. 'is_active' => true,
  16. 'rate_limit' => 1000,
  17. ]);
  18. }
  19. public function test_merge_recalculates_totals_when_same_product_exists_in_both_carts(): void
  20. {
  21. $product = Product::query()
  22. ->where('type', 'simple')
  23. ->whereHas('inventories', fn ($query) => $query->where('qty', '>=', 3))
  24. ->get()
  25. ->first(fn (Product $product) => (bool) $product->status);
  26. if (! $product) {
  27. $this->markTestSkipped('A saleable simple product is required for this test.');
  28. }
  29. $guestCart = $this->createCart();
  30. $this->addProduct($guestCart['token'], $product->id, 2);
  31. $customerData = $this->createTestCustomer();
  32. $this->addProduct($customerData['token'], $product->id, 1);
  33. $mutation = <<<'GQL'
  34. mutation mergeCart($cartId: Int!) {
  35. createMergeCart(input: {cartId: $cartId}) {
  36. mergeCart {
  37. success
  38. baseSubtotal
  39. items {
  40. edges {
  41. node {
  42. quantity
  43. basePrice
  44. baseTotal
  45. }
  46. }
  47. }
  48. }
  49. }
  50. }
  51. GQL;
  52. $response = $this->graphQL($mutation, [
  53. 'cartId' => $guestCart['id'],
  54. ], $this->customerHeaders($customerData['token']));
  55. $response->assertSuccessful();
  56. $cart = $response->json('data.createMergeCart.mergeCart');
  57. $item = $cart['items']['edges'][0]['node'];
  58. $this->assertTrue($cart['success']);
  59. $this->assertSame(3, $item['quantity']);
  60. $this->assertEqualsWithDelta(
  61. (float) $item['basePrice'] * 3,
  62. (float) $item['baseTotal'],
  63. 0.0001
  64. );
  65. $this->assertEqualsWithDelta(
  66. (float) $item['baseTotal'],
  67. (float) $cart['baseSubtotal'],
  68. 0.0001
  69. );
  70. }
  71. private function createCart(): array
  72. {
  73. $mutation = <<<'GQL'
  74. mutation createCart {
  75. createCartToken(input: {}) {
  76. cartToken {
  77. _id
  78. cartToken
  79. }
  80. }
  81. }
  82. GQL;
  83. $response = $this->graphQL($mutation);
  84. $response->assertSuccessful();
  85. return [
  86. 'id' => (int) $response->json('data.createCartToken.cartToken._id'),
  87. 'token' => (string) $response->json('data.createCartToken.cartToken.cartToken'),
  88. ];
  89. }
  90. private function addProduct(string $token, int $productId, int $quantity): void
  91. {
  92. $mutation = <<<'GQL'
  93. mutation addProduct($productId: Int!, $quantity: Int!) {
  94. createAddProductInCart(input: {
  95. productId: $productId
  96. quantity: $quantity
  97. }) {
  98. addProductInCart {
  99. success
  100. }
  101. }
  102. }
  103. GQL;
  104. $response = $this->graphQL($mutation, [
  105. 'productId' => $productId,
  106. 'quantity' => $quantity,
  107. ], $this->customerHeaders($token));
  108. $response->assertSuccessful();
  109. $this->assertTrue(
  110. (bool) $response->json('data.createAddProductInCart.addProductInCart.success'),
  111. $response->getContent()
  112. );
  113. }
  114. private function customerHeaders(string $token): array
  115. {
  116. return [
  117. 'Authorization' => 'Bearer '.$token,
  118. ];
  119. }
  120. }