Skip to content

Commit

Permalink
Add missing checkout complete validation group
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Gruber committed May 28, 2017
1 parent 0e16a6e commit 60c8512
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
Expand Up @@ -75,7 +75,10 @@ sylius_admin_api_checkout_complete:
_controller: sylius.controller.order:updateAction
_sylius:
serialization_version: $version
form: Sylius\Bundle\CoreBundle\Form\Type\Checkout\CompleteType
form:
type: Sylius\Bundle\CoreBundle\Form\Type\Checkout\CompleteType
options:
validation_groups: 'sylius_checkout_complete'
repository:
method: find
arguments: [$orderId]
Expand Down
74 changes: 71 additions & 3 deletions tests/Controller/CheckoutCompleteApiTest.php
Expand Up @@ -11,9 +11,11 @@

namespace Sylius\Tests\Controller;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Core\Model\ProductVariant;
use Sylius\Component\Product\Model\ProductInterface;
use Sylius\Component\Product\Repository\ProductRepositoryInterface;
use Sylius\Component\Product\Repository\ProductVariantRepositoryInterface;
use Symfony\Component\HttpFoundation\Response;

/**
Expand Down Expand Up @@ -64,6 +66,72 @@ public function it_does_not_allow_to_complete_order_that_is_not_addressed_and_ha
$this->assertResponse($response, 'checkout/complete_invalid_order_state', Response::HTTP_INTERNAL_SERVER_ERROR);
}

/**
* @test
*/
public function it_does_not_allow_to_complete_order_with_disabled_product()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$this->loadFixturesFromFile('resources/checkout.yml');

$cartId = $this->createCart();
$this->addItemToCart($cartId);
$this->addressOrder($cartId);
$this->selectOrderShippingMethod($cartId);
$this->selectOrderPaymentMethod($cartId);

/** @var ProductRepositoryInterface $productRepository */
$productRepository = $this->client->getContainer()->get('sylius.repository.product');
/** @var ProductInterface $product */
$product = $productRepository->findOneBy(['code' => 'MUG']);
$this->assertNotNull($product);
$product->disable();

/** @var EntityManagerInterface $productManager */
$productManager = $this->client->getContainer()->get('sylius.manager.product');
$productManager->persist($product);
$productManager->flush();

$this->client->request('PUT', $this->getCheckoutCompleteUrl($cartId), [], [], static::$authorizedHeaderWithContentType);

$response = $this->client->getResponse();
$this->assertResponse($response, 'checkout/complete_validation_failed_disabled_product', Response::HTTP_BAD_REQUEST);
}

/**
* @test
*/
public function it_does_not_allow_to_complete_order_with_insufficient_stock()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$this->loadFixturesFromFile('resources/checkout.yml');

$cartId = $this->createCart();
$this->addItemToCart($cartId);
$this->addressOrder($cartId);
$this->selectOrderShippingMethod($cartId);
$this->selectOrderPaymentMethod($cartId);

/** @var ProductVariantRepositoryInterface $productVariantRepository */
$productVariantRepository = $this->client->getContainer()->get('sylius.repository.product_variant');
/** @var ProductVariant $productVariant */
$productVariant = $productVariantRepository->findOneByCodeAndProductCode('MUG_SW', 'MUG');
$this->assertNotNull($productVariant);
$productVariant->setTracked(true);
$productVariant->setOnHand(0);
$productVariant->setOnHold(0);

/** @var EntityManagerInterface $productVariantManager */
$productVariantManager = $this->client->getContainer()->get('sylius.manager.product_variant');
$productVariantManager->persist($productVariant);
$productVariantManager->flush();

$this->client->request('PUT', $this->getCheckoutCompleteUrl($cartId), [], [], static::$authorizedHeaderWithContentType);

$response = $this->client->getResponse();
$this->assertResponse($response, 'checkout/complete_validation_failed_insufficient_stock', Response::HTTP_BAD_REQUEST);
}

/**
* @test
*/
Expand Down
@@ -0,0 +1,12 @@
{
"code": 400,
"message": "Validation Failed",
"errors": {
"errors": [
"This product Mug has been disabled."
],
"children": {
"notes": {}
}
}
}
@@ -0,0 +1,12 @@
{
"code": 400,
"message": "Validation Failed",
"errors": {
"errors": [
"Mug does not have sufficient stock."
],
"children": {
"notes": {}
}
}
}

0 comments on commit 60c8512

Please sign in to comment.