diff --git a/app/GraphQL/Inventory/Mutations/Variants/Variants.php b/app/GraphQL/Inventory/Mutations/Variants/Variants.php index 48af2a000..1ae99d676 100644 --- a/app/GraphQL/Inventory/Mutations/Variants/Variants.php +++ b/app/GraphQL/Inventory/Mutations/Variants/Variants.php @@ -60,7 +60,7 @@ public function update(mixed $root, array $req): VariantModel */ public function delete(mixed $root, array $req): bool { - $variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany()); + $variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany()); return $variant->delete(); } diff --git a/graphql/schemas/Inventory/variant.graphql b/graphql/schemas/Inventory/variant.graphql index 577b0ea13..f92af4b92 100644 --- a/graphql/schemas/Inventory/variant.graphql +++ b/graphql/schemas/Inventory/variant.graphql @@ -117,12 +117,12 @@ extend type Mutation @guard { @field( resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@update" ) - deleteVariant(id: Int!): Boolean + deleteVariant(id: ID!): Boolean @field( resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@delete" ) addVariantToWarehouse( - id: Int! + id: ID! warehouse_id: Int! input: VariantsWarehousesInput! ): Variant diff --git a/tests/GraphQL/Inventory/ProductsTest.php b/tests/GraphQL/Inventory/ProductsTest.php index e1996a882..36ff7cbae 100644 --- a/tests/GraphQL/Inventory/ProductsTest.php +++ b/tests/GraphQL/Inventory/ProductsTest.php @@ -162,6 +162,55 @@ public function testDeleteProduct(): void */ public function testAddVariantToProduct(): void { + $region = [ + 'name' => 'Test Region', + 'slug' => 'test-region', + 'short_slug' => 'test-region', + 'is_default' => 1, + 'currency_id' => 1, + ]; + $regionResponse = $this->graphQL(' + mutation($data: RegionInput!) { + createRegion(input: $data) + { + id + name + slug + short_slug + currency_id + is_default + } + } + ', [ + 'data' => $region + ])->assertJson([ + 'data' => ['createRegion' => $region] + ]); + $regionResponse = $regionResponse->decodeResponseJson(); + + $warehouseData = [ + 'regions_id' => $regionResponse['data']['createRegion']['id'], + 'name' => fake()->name, + 'location' => 'Test Location', + 'is_default' => false, + 'is_published' => 1, + ]; + + $warehouseResponse = $this->graphQL(' + mutation($data: WarehouseInput!) { + createWarehouse(input: $data) + { + id + regions_id + name + location + is_default + is_published + } + }', ['data' => $warehouseData])->assertJson([ + 'data' => ['createWarehouse' => $warehouseData] + ]); + $data = [ 'name' => fake()->name, 'description' => fake()->text, @@ -196,19 +245,20 @@ public function testAddVariantToProduct(): void $data = [ 'name' => fake()->name, 'description' => fake()->text, - 'products_id' => $id + 'products_id' => $id, + 'warehouse_id' => $warehouseResponse['data']['createWarehouse']['id'] ]; - $this->graphQL(' + $variantResponse = $this->graphQL(' mutation($data: VariantsInput!) { createVariant(input: $data) { + id name description products_id } - }', ['data' => $data])->assertJson([ - 'data' => ['createVariant' => $data] - ]); + }', ['data' => $data]); + $this->assertArrayHasKey('id', $variantResponse->json()['data']['createVariant']); } /** @@ -218,6 +268,55 @@ public function testAddVariantToProduct(): void */ public function testDeleteVariantToProduct(): void { + $region = [ + 'name' => 'Test Region', + 'slug' => 'test-region', + 'short_slug' => 'test-region', + 'is_default' => 1, + 'currency_id' => 1, + ]; + $regionResponse = $this->graphQL(' + mutation($data: RegionInput!) { + createRegion(input: $data) + { + id + name + slug + short_slug + currency_id + is_default + } + } + ', [ + 'data' => $region + ])->assertJson([ + 'data' => ['createRegion' => $region] + ]); + $regionResponse = $regionResponse->decodeResponseJson(); + + $warehouseData = [ + 'regions_id' => $regionResponse['data']['createRegion']['id'], + 'name' => fake()->name, + 'location' => 'Test Location', + 'is_default' => false, + 'is_published' => 1, + ]; + + $warehouseResponse = $this->graphQL(' + mutation($data: WarehouseInput!) { + createWarehouse(input: $data) + { + id + regions_id + name + location + is_default + is_published + } + }', ['data' => $warehouseData])->assertJson([ + 'data' => ['createWarehouse' => $warehouseData] + ]); + $data = [ 'name' => fake()->name, 'description' => fake()->text, @@ -232,6 +331,7 @@ public function testDeleteVariantToProduct(): void }', ['data' => $data])->assertJson([ 'data' => ['createProduct' => $data] ]); + $response = $this->graphQL(' query { products { @@ -242,13 +342,19 @@ public function testDeleteVariantToProduct(): void } } }'); + $this->assertArrayHasKey('data', $response->json()); + $this->assertArrayHasKey('products', $response->json()['data']); + $this->assertArrayHasKey('data', $response->json()['data']['products']); + $this->assertArrayHasKey('id', $response->json()['data']['products']['data'][0]); + $id = $response->json()['data']['products']['data'][0]['id']; $data = [ 'name' => fake()->name, 'description' => fake()->text, - 'products_id' => $id + 'products_id' => $id, + 'warehouse_id' => $warehouseResponse['data']['createWarehouse']['id'] ]; - $response = $this->graphQL(' + $variantResponse = $this->graphQL(' mutation($data: VariantsInput!) { createVariant(input: $data) { @@ -258,11 +364,13 @@ public function testDeleteVariantToProduct(): void products_id } }', ['data' => $data]); - $id = $response->json()['data']['createVariant']['id']; + $this->assertArrayHasKey('id', $variantResponse->json()['data']['createVariant']); + + $variantResponseId = $variantResponse->json()['data']['createVariant']['id']; $this->graphQL(' - mutation($id: Int!) { + mutation($id: ID!) { deleteVariant(id: $id) - }', ['id' => $id])->assertJson([ + }', ['id' => $variantResponseId])->assertJson([ 'data' => ['deleteVariant' => true] ]); }