| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- <?php
- namespace Webkul\BagistoApi\Tests\Feature\GraphQL;
- use Webkul\BagistoApi\Tests\GraphQLTestCase;
- use Webkul\Core\Models\Channel;
- use Webkul\Customer\Models\Customer;
- use Webkul\Product\Models\Product;
- use Webkul\Sales\Models\Order;
- use Webkul\Sales\Models\OrderItem;
- use Webkul\Sales\Models\OrderPayment;
- class CustomerOrderReorderTest extends GraphQLTestCase
- {
- /**
- * Create test data — customer with completed order
- */
- private function createTestData(): array
- {
- $this->seedRequiredData();
- $customer = $this->createCustomer();
- $channel = Channel::first();
-
- // Create multiple products for testing
- $product1 = Product::factory()->create(['sku' => 'REORDER-PROD-1']);
- $product2 = Product::factory()->create(['sku' => 'REORDER-PROD-2']);
- // Create a completed order with multiple items
- $completedOrder = Order::factory()->create([
- 'customer_id' => $customer->id,
- 'customer_email' => $customer->email,
- 'customer_first_name' => $customer->first_name,
- 'customer_last_name' => $customer->last_name,
- 'channel_id' => $channel->id,
- 'status' => 'completed',
- ]);
- OrderItem::factory()->create([
- 'order_id' => $completedOrder->id,
- 'product_id' => $product1->id,
- 'sku' => 'REORDER-PROD-1',
- 'type' => 'simple',
- 'name' => 'Reorder Product 1',
- 'qty_ordered' => 2,
- ]);
- OrderItem::factory()->create([
- 'order_id' => $completedOrder->id,
- 'product_id' => $product2->id,
- 'sku' => 'REORDER-PROD-2',
- 'type' => 'simple',
- 'name' => 'Reorder Product 2',
- 'qty_ordered' => 1,
- ]);
- OrderPayment::factory()->create([
- 'order_id' => $completedOrder->id,
- ]);
- // Create a pending order (also reorderable)
- $pendingOrder = Order::factory()->create([
- 'customer_id' => $customer->id,
- 'customer_email' => $customer->email,
- 'customer_first_name' => $customer->first_name,
- 'customer_last_name' => $customer->last_name,
- 'channel_id' => $channel->id,
- 'status' => 'pending',
- ]);
- OrderItem::factory()->create([
- 'order_id' => $pendingOrder->id,
- 'product_id' => $product1->id,
- 'sku' => 'REORDER-PROD-1',
- 'type' => 'simple',
- 'name' => 'Reorder Product 1',
- 'qty_ordered' => 1,
- ]);
- OrderPayment::factory()->create([
- 'order_id' => $pendingOrder->id,
- ]);
- return compact('customer', 'completedOrder', 'pendingOrder', 'product1', 'product2', 'channel');
- }
- // ── Reorder Mutation Tests ────────────────────────────────────
- /**
- * Test: Reorder a completed order with available products
- */
- public function test_reorder_completed_order_success(): void
- {
- $testData = $this->createTestData();
- $mutation = <<<'GQL'
- mutation reorderOrder($input: ReorderInput!) {
- reorderOrder(input: $input) {
- success
- message
- orderId
- itemsAddedCount
- }
- }
- GQL;
- $variables = [
- 'input' => [
- 'orderId' => $testData['completedOrder']->id,
- ],
- ];
- $response = $this->authenticatedGraphQL(
- $testData['customer'],
- $mutation,
- $variables
- );
- $response->assertJson([
- 'data' => [
- 'reorderOrder' => [
- 'success' => true,
- 'orderId' => $testData['completedOrder']->id,
- ],
- ],
- ]);
- // Verify items were added to cart
- $this->assertGreaterThan(0, $response['data']['reorderOrder']['itemsAddedCount']);
- }
- /**
- * Test: Reorder returns correct item count
- */
- public function test_reorder_returns_correct_item_count(): void
- {
- $testData = $this->createTestData();
- $mutation = <<<'GQL'
- mutation reorderOrder($input: ReorderInput!) {
- reorderOrder(input: $input) {
- success
- message
- orderId
- itemsAddedCount
- }
- }
- GQL;
- $variables = [
- 'input' => [
- 'orderId' => $testData['completedOrder']->id,
- ],
- ];
- $response = $this->authenticatedGraphQL(
- $testData['customer'],
- $mutation,
- $variables
- );
- // The completed order has 2 items
- $itemsAddedCount = $response['data']['reorderOrder']['itemsAddedCount'];
- $this->assertEquals(2, $itemsAddedCount);
- }
- /**
- * Test: Cannot reorder non-existent order
- */
- public function test_cannot_reorder_non_existent_order(): void
- {
- $testData = $this->createTestData();
- $mutation = <<<'GQL'
- mutation reorderOrder($input: ReorderInput!) {
- reorderOrder(input: $input) {
- success
- message
- orderId
- }
- }
- GQL;
- $variables = [
- 'input' => [
- 'orderId' => 99999, // Non-existent ID
- ],
- ];
- $response = $this->authenticatedGraphQL(
- $testData['customer'],
- $mutation,
- $variables
- );
- // Should return an error
- $this->assertArrayHasKey('errors', $response);
- }
- /**
- * Test: Cannot reorder order of another customer
- */
- public function test_cannot_reorder_other_customers_order(): void
- {
- $testData = $this->createTestData();
- $otherCustomer = $this->createCustomer();
- $mutation = <<<'GQL'
- mutation reorderOrder($input: ReorderInput!) {
- reorderOrder(input: $input) {
- success
- message
- orderId
- }
- }
- GQL;
- $variables = [
- 'input' => [
- 'orderId' => $testData['completedOrder']->id,
- ],
- ];
- $response = $this->authenticatedGraphQL(
- $otherCustomer,
- $mutation,
- $variables
- );
- // Should return an error
- $this->assertArrayHasKey('errors', $response);
- }
- /**
- * Test: Missing order ID should return error
- */
- public function test_reorder_without_order_id(): void
- {
- $testData = $this->createTestData();
- $mutation = <<<'GQL'
- mutation reorderOrder($input: ReorderInput!) {
- reorderOrder(input: $input) {
- success
- message
- }
- }
- GQL;
- $variables = [
- 'input' => [],
- ];
- $response = $this->authenticatedGraphQL(
- $testData['customer'],
- $mutation,
- $variables
- );
- // Should return an error for missing orderId
- $this->assertArrayHasKey('errors', $response);
- }
- /**
- * Test: Unauthenticated reorder request fails
- */
- public function test_unauthenticated_reorder_fails(): void
- {
- $testData = $this->createTestData();
- $mutation = <<<'GQL'
- mutation reorderOrder($input: ReorderInput!) {
- reorderOrder(input: $input) {
- success
- message
- }
- }
- GQL;
- $variables = [
- 'input' => [
- 'orderId' => $testData['completedOrder']->id,
- ],
- ];
- // Call without authentication
- $response = $this->graphQL($mutation, $variables);
- // GraphQL returns response as array from postJson
- $data = $response->json();
- $this->assertArrayHasKey('errors', $data);
- $this->assertTrue(
- isset($data['errors'][0]['message']),
- 'Expected GraphQL error message to be present'
- );
- }
- /**
- * Test: Reorder response includes all expected fields
- */
- public function test_reorder_response_includes_all_fields(): void
- {
- $testData = $this->createTestData();
- }
- /**
- * Test: Reorder a pending order
- */
- public function test_reorder_pending_order_success(): void
- {
- $testData = $this->createTestData();
- $mutation = <<<'GQL'
- mutation reorderOrder($input: ReorderInput!) {
- reorderOrder(input: $input) {
- success
- message
- orderId
- itemsAddedCount
- }
- }
- GQL;
- $variables = [
- 'input' => [
- 'orderId' => $testData['pendingOrder']->id,
- ],
- ];
- $response = $this->authenticatedGraphQL(
- $testData['customer'],
- $mutation,
- $variables
- );
- $response->assertJson([
- 'data' => [
- 'reorderOrder' => [
- 'success' => true,
- 'orderId' => $testData['pendingOrder']->id,
- ],
- ],
- ]);
- // Should have added 1 item
- $this->assertEquals(1, $response['data']['reorderOrder']['itemsAddedCount']);
- }
- }
|