UpdateItemQtyTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Checkout\Controller\Cart;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\Checkout\Model\Session;
  10. use Magento\Catalog\Api\ProductRepositoryInterface;
  11. use Magento\Framework\Data\Form\FormKey;
  12. use Magento\Framework\Serialize\Serializer\Json;
  13. class UpdateItemQtyTest extends \Magento\TestFramework\TestCase\AbstractController
  14. {
  15. /**
  16. * @var Json
  17. */
  18. private $json;
  19. /**
  20. * @var FormKey
  21. */
  22. private $formKey;
  23. /**
  24. * @var Session
  25. */
  26. private $session;
  27. /**
  28. * @var ProductRepositoryInterface
  29. */
  30. private $productRepository;
  31. protected function setUp()
  32. {
  33. parent::setUp();
  34. $this->json = $this->_objectManager->create(Json::class);
  35. $this->formKey = $this->_objectManager->get(FormKey::class);
  36. $this->session = $this->_objectManager->create(Session::class);
  37. $this->productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
  38. }
  39. /**
  40. * Tests of cart validation.
  41. *
  42. * @param array $requestQuantity
  43. * @param array $expectedResponse
  44. *
  45. * @magentoDbIsolation enabled
  46. * @magentoAppArea frontend
  47. * @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product.php
  48. * @dataProvider requestDataProvider
  49. */
  50. public function testExecute($requestQuantity, $expectedResponse)
  51. {
  52. try {
  53. /** @var $product Product */
  54. $product = $this->productRepository->get('simple');
  55. } catch (\Exception $e) {
  56. $this->fail('No such product entity');
  57. }
  58. $quoteItem = $this->session
  59. ->getQuote()
  60. ->getItemByProduct($product);
  61. $this->assertNotNull($quoteItem, 'Cannot get quote item for simple product');
  62. $request = [];
  63. if (!empty($requestQuantity) && is_array($requestQuantity)) {
  64. $request= [
  65. 'form_key' => $this->formKey->getFormKey(),
  66. 'cart' => [
  67. $quoteItem->getId() => $requestQuantity,
  68. ]
  69. ];
  70. }
  71. $this->getRequest()->setPostValue($request);
  72. $this->dispatch('checkout/cart/updateItemQty');
  73. $response = $this->getResponse()->getBody();
  74. $this->assertEquals($this->json->unserialize($response), $expectedResponse);
  75. }
  76. /**
  77. * Variations of request data.
  78. * @returns array
  79. */
  80. public function requestDataProvider(): array
  81. {
  82. return [
  83. [
  84. 'request' => [],
  85. 'response' => [
  86. 'success' => false,
  87. 'error_message' => 'Something went wrong while saving the page.'.
  88. ' Please refresh the page and try again.'
  89. ]
  90. ],
  91. [
  92. 'request' => ['qty' => 2],
  93. 'response' => [
  94. 'success' => true,
  95. ]
  96. ],
  97. [
  98. 'request' => ['qty' => 230],
  99. 'response' => [
  100. 'success' => false,
  101. 'error_message' => 'The requested qty is not available']
  102. ],
  103. ];
  104. }
  105. }