UpdateItemQtyTest.php 3.26 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Checkout\Controller\Cart;

use Magento\Catalog\Model\Product;
use Magento\Checkout\Model\Session;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Serialize\Serializer\Json;

class UpdateItemQtyTest extends \Magento\TestFramework\TestCase\AbstractController
{
    /**
     * @var Json
     */
    private $json;

    /**
     * @var FormKey
     */
    private $formKey;

    /**
     * @var Session
     */
    private $session;

    /**
     * @var ProductRepositoryInterface
     */
    private $productRepository;

    protected function setUp()
    {
        parent::setUp();

        $this->json = $this->_objectManager->create(Json::class);
        $this->formKey = $this->_objectManager->get(FormKey::class);
        $this->session = $this->_objectManager->create(Session::class);
        $this->productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
    }

    /**
     * Tests of cart validation.
     *
     * @param array $requestQuantity
     * @param array $expectedResponse
     *
     * @magentoDbIsolation enabled
     * @magentoAppArea frontend
     * @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product.php
     * @dataProvider requestDataProvider
     */
    public function testExecute($requestQuantity, $expectedResponse)
    {
        try {
            /** @var $product Product */
            $product = $this->productRepository->get('simple');
        } catch (\Exception $e) {
            $this->fail('No such product entity');
        }

        $quoteItem = $this->session
            ->getQuote()
            ->getItemByProduct($product);

        $this->assertNotNull($quoteItem, 'Cannot get quote item for simple product');

        $request = [];
        if (!empty($requestQuantity) && is_array($requestQuantity)) {
            $request= [
                'form_key' => $this->formKey->getFormKey(),
                'cart' => [
                    $quoteItem->getId() => $requestQuantity,
                ]
            ];
        }

        $this->getRequest()->setPostValue($request);
        $this->dispatch('checkout/cart/updateItemQty');
        $response = $this->getResponse()->getBody();

        $this->assertEquals($this->json->unserialize($response), $expectedResponse);
    }

    /**
     * Variations of request data.
     * @returns array
     */
    public function requestDataProvider(): array
    {
        return [
            [
                'request' => [],
                'response' => [
                    'success' => false,
                    'error_message' => 'Something went wrong while saving the page.'.
                        ' Please refresh the page and try again.'
                ]
            ],
            [
                'request' => ['qty' => 2],
                'response' => [
                    'success' => true,
                ]
            ],
            [
                'request' => ['qty' => 230],
                'response' => [
                    'success' => false,
                    'error_message' => 'The requested qty is not available']
            ],
        ];
    }
}