Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get quantity from request as an integer. #239

Merged
merged 5 commits into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Request/ChangeItemQuantityRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(Request $request)
{
$this->token = $request->attributes->get('token');
$this->id = $request->attributes->get('id');
$this->quantity = $request->request->get('quantity');
$this->quantity = $request->request->has('quantity') ? $request->request->getInt('quantity') : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quantity cannot be null, it's declared as int. $request->request->getInt('quantity', 1) instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$request->request->getInt('quantity', 0)? This should trigger validation error. Question is, what should be a default behaviour. 1 as a default would be more flexible for frontends...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we allow not to pass quantity then it makes sense to use 1 as the default value. If we require quantity then we should throw an exception here if it does not exist.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The decision is up to you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add 1 as default. But we need to mention it in docs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you adjust it, please?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, but it will take some time

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function fromArray(array $item)

public static function fromRequest(Request $request)
{
return new self($request->attributes->get('token'), $request->request->get('productCode'), $request->request->get('options'), $request->request->get('quantity'));
return new self($request->attributes->get('token'), $request->request->get('productCode'), $request->request->get('options'), $request->request->has('quantity') ? $request->request->getInt('quantity') : null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here and so on.

}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Request/PutSimpleItemToCartRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function fromArray(array $item)

public static function fromRequest(Request $request)
{
return new self($request->attributes->get('token'), $request->request->get('productCode'), $request->request->get('quantity'));
return new self($request->attributes->get('token'), $request->request->get('productCode'), $request->request->has('quantity') ? $request->request->getInt('quantity') : null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function fromArray(array $item)

public static function fromRequest(Request $request)
{
return new self($request->attributes->get('token'), $request->request->get('productCode'), $request->request->get('variantCode'), $request->request->get('quantity'));
return new self($request->attributes->get('token'), $request->request->get('productCode'), $request->request->get('variantCode'), $request->request->has('quantity') ? $request->request->getInt('quantity') : null);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/Controller/CartPutItemToCartApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function it_validates_if_quantity_is_larger_than_0_during_add_simple_prod
/**
* @test
*/
public function it_validates_if_quantity_is_is_an_integer_during_add_simple_product()
public function it_converts_quantity_as_an_integer_and_adds_simple_product()
{
$this->loadFixturesFromFile('shop.yml');

Expand All @@ -173,7 +173,7 @@ public function it_validates_if_quantity_is_is_an_integer_during_add_simple_prod
$this->client->request('POST', sprintf('/shop-api/carts/%s/items', $token), [], [], static::$acceptAndContentTypeHeader, $data);
$response = $this->client->getResponse();

$this->assertResponse($response, 'cart/validation_quantity_not_integer_response', Response::HTTP_BAD_REQUEST);
$this->assertResponse($response, 'cart/add_simple_product_to_cart_response', Response::HTTP_CREATED);
}

/**
Expand Down Expand Up @@ -417,7 +417,7 @@ public function it_validates_if_quantity_is_larger_than_0_during_add_variant_bas
/**
* @test
*/
public function it_validates_if_quantity_is_is_an_integer_during_add_variant_based_configurable_product()
public function it_converts_quantity_as_an_integer_and_adds_variant_based_configurable_product()
{
$this->loadFixturesFromFile('shop.yml');

Expand All @@ -438,7 +438,7 @@ public function it_validates_if_quantity_is_is_an_integer_during_add_variant_bas
$this->client->request('POST', sprintf('/shop-api/carts/%s/items', $token), [], [], static::$acceptAndContentTypeHeader, $data);
$response = $this->client->getResponse();

$this->assertResponse($response, 'cart/validation_quantity_not_integer_response', Response::HTTP_BAD_REQUEST);
$this->assertResponse($response, 'cart/add_product_variant_to_cart_response', Response::HTTP_CREATED);
}

/**
Expand Down