Skip to content

Commit

Permalink
Update products tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arfenis committed Jul 5, 2023
1 parent e620cd0 commit 1c2a9ff
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/GraphQL/Inventory/Mutations/Variants/Variants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions graphql/schemas/Inventory/variant.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
128 changes: 118 additions & 10 deletions tests/GraphQL/Inventory/ProductsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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']);
}

/**
Expand All @@ -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,
Expand All @@ -232,6 +331,7 @@ public function testDeleteVariantToProduct(): void
}', ['data' => $data])->assertJson([
'data' => ['createProduct' => $data]
]);

$response = $this->graphQL('
query {
products {
Expand All @@ -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)
{
Expand All @@ -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]
]);
}
Expand Down

0 comments on commit 1c2a9ff

Please sign in to comment.