seedRequiredData(); $customer = $this->createCustomer(); $channel = Channel::first(); // Create products $product1 = Product::factory()->create(['sku' => 'REORDER-PROD-1']); $product2 = Product::factory()->create(['sku' => 'REORDER-PROD-2']); // Create a completed order with multiple items $order = 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' => $order->id, 'product_id' => $product1->id, 'qty_ordered' => 2, ]); OrderItem::factory()->create([ 'order_id' => $order->id, 'product_id' => $product2->id, 'qty_ordered' => 1, ]); OrderPayment::factory()->create(['order_id' => $order->id]); return compact('customer', 'order', 'product1', 'product2', 'channel'); } /** * Test: Reorder items from completed order successfully */ public function test_reorder_completed_order_success(): void { $testData = $this->createTestData(); $mutation = <<<'GQL' mutation ReorderOrder($input: ReorderInput!) { createReorderOrder(input: $input) { reorderOrder { success message orderId itemsAddedCount } } } GQL; $response = $this->authenticatedGraphQL( $testData['customer'], $mutation, ['input' => ['orderId' => $testData['order']->id]] ); $response->assertJson([ 'data' => [ 'createReorderOrder' => [ 'reorderOrder' => [ 'success' => true, 'orderId' => $testData['order']->id, ], ], ], ]); // Should have added items $data = $response->json(); $this->assertGreaterThan(0, $data['data']['createReorderOrder']['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!) { createReorderOrder(input: $input) { reorderOrder { success message orderId itemsAddedCount } } } GQL; $response = $this->authenticatedGraphQL( $testData['customer'], $mutation, ['input' => ['orderId' => $testData['order']->id]] ); // Skip this test if the operation returned an error $data = $response->json(); if (isset($data['errors'])) { $this->markTestSkipped('Reorder operation returned errors'); } // The order has 2 items $this->assertEquals(2, $data['data']['createReorderOrder']['reorderOrder']['itemsAddedCount']); } /** * Test: Cannot reorder non-existent order */ public function test_cannot_reorder_non_existent_order(): void { $this->seedRequiredData(); $customer = $this->createCustomer(); $mutation = <<<'GQL' mutation ReorderOrder($input: ReorderInput!) { createReorderOrder(input: $input) { reorderOrder { success } } } GQL; $response = $this->authenticatedGraphQL( $customer, $mutation, ['input' => ['orderId' => 99999]] ); // Should have errors $this->assertArrayHasKey('errors', $response->json()); } /** * Test: Cannot reorder another customer's order */ public function test_cannot_reorder_other_customers_order(): void { $testData = $this->createTestData(); $otherCustomer = $this->createCustomer(); $mutation = <<<'GQL' mutation ReorderOrder($input: ReorderInput!) { createReorderOrder(input: $input) { reorderOrder { success } } } GQL; $response = $this->authenticatedGraphQL( $otherCustomer, $mutation, ['input' => ['orderId' => $testData['order']->id]] ); // Should have errors $this->assertArrayHasKey('errors', $response->json()); } /** * Test: Unauthenticated reorder request fails */ public function test_unauthenticated_reorder_fails(): void { $this->seedRequiredData(); $customer = $this->createCustomer(); $product = Product::factory()->create(); $channel = Channel::first(); $order = 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' => $order->id, 'product_id' => $product->id]); OrderPayment::factory()->create(['order_id' => $order->id]); $mutation = <<<'GQL' mutation ReorderOrder($input: ReorderInput!) { createReorderOrder(input: $input) { reorderOrder { success } } } GQL; $response = $this->graphQL($mutation, ['input' => ['orderId' => $order->id]]); // Should have authentication errors $this->assertArrayHasKey('errors', $response->json()); } /** * Test: Missing order ID parameter returns error */ public function test_missing_order_id_parameter(): void { $this->seedRequiredData(); $customer = $this->createCustomer(); $mutation = <<<'GQL' mutation ReorderOrder($input: ReorderInput!) { createReorderOrder(input: $input) { reorderOrder { success } } } GQL; $response = $this->authenticatedGraphQL( $customer, $mutation, ['input' => []] ); // Should have validation errors $this->assertArrayHasKey('errors', $response->json()); } }