CartTest.php 2.97 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Tax\Test\Unit\Plugin\Checkout\CustomerData;

use Magento\Checkout\CustomerData\Cart as CheckoutCart;
use Magento\Checkout\Helper\Data;
use Magento\Checkout\Model\Session;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item;
use Magento\Tax\Block\Item\Price\Renderer;
use Magento\Tax\Plugin\Checkout\CustomerData\Cart;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

class CartTest extends TestCase
{
    /**
     * @var Session|MockObject
     */
    private $checkoutSession;

    /**
     * @var Data|MockObject
     */
    private $checkoutHelper;

    /**
     * @var Renderer|MockObject
     */
    private $itemPriceRenderer;

    /**
     * @var CheckoutCart|MockObject
     */
    private $checkoutCart;

    /**
     * @var Quote|MockObject
     */
    private $quote;

    /**
     * @var Cart
     */
    private $cart;

    protected function setUp()
    {
        $this->checkoutSession = $this->createMock(Session::class);
        $this->checkoutHelper = $this->createMock(Data::class);
        $this->itemPriceRenderer = $this->createMock(Renderer::class);
        $this->checkoutCart = $this->createMock(CheckoutCart::class);
        $this->quote = $this->createMock(Quote::class);

        $this->checkoutSession->method('getQuote')
            ->willReturn($this->quote);
        
        $this->cart = new Cart(
            $this->checkoutSession,
            $this->checkoutHelper,
            $this->itemPriceRenderer
        );
    }

    public function testAfterGetSectionData()
    {
        $input = ['items' => [
                [
                    'item_id' => 1,
                    'product_price' => ''
                ],
                [
                    'item_id' => 2,
                    'product_price' => ''
                ],
            ]
        ];

        $this->checkoutHelper->method('formatPrice')
            ->willReturn('formatted');

        $item1 = $this->createMock(Item::class);
        $item2 = $this->createMock(Item::class);

        $item1->method('getItemId')
            ->willReturn(1);
        $item2->method('getItemId')
            ->willReturn(2);

        $this->quote->method('getAllVisibleItems')
            ->willReturn([
                $item1,
                $item2,
            ]);

        $this->itemPriceRenderer->method('toHtml')
            ->willReturn(1);

        $result = $this->cart->afterGetSectionData($this->checkoutCart, $input);

        self::assertArrayHasKey('subtotal_incl_tax', $result);
        self::assertArrayHasKey('subtotal_excl_tax', $result);
        self::assertArrayHasKey('items', $result);
        self::assertTrue(is_array($result['items']));
        self::assertEquals(2, count($result['items']));
        self::assertEquals(1, $result['items'][0]['product_price']);
        self::assertEquals(1, $result['items'][1]['product_price']);
    }
}