QuoteItemTest.php 3.39 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Bundle\Test\Unit\Model\Plugin;

use Magento\Quote\Model\Quote\Item\ToOrderItem;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Quote\Model\Quote\Item\AbstractItem;
use Magento\Catalog\Model\Product;

class QuoteItemTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \PHPUnit_Framework_MockObject_MockObject|Product
     */
    private $productMock;

    /** @var \Magento\Bundle\Model\Plugin\QuoteItem */
    protected $model;

    /** @var \PHPUnit_Framework_MockObject_MockObject|AbstractItem */
    protected $quoteItemMock;

    /** @var \PHPUnit_Framework_MockObject_MockObject|OrderItemInterface */
    protected $orderItemMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject|ToOrderItem
     */
    protected $subjectMock;

    protected function setUp()
    {
        $this->orderItemMock = $this->getMockForAbstractClass(
            OrderItemInterface::class,
            [],
            '',
            false,
            false,
            true,
            ['getProductOptions', 'setProductOptions']
        );
        $this->quoteItemMock = $this->getMockForAbstractClass(
            AbstractItem::class,
            [],
            '',
            false,
            false,
            true,
            ['getProduct']
        );
        $this->subjectMock = $this->createMock(ToOrderItem::class);
        $this->productMock = $this->createMock(Product::class);
        $this->model = new \Magento\Bundle\Model\Plugin\QuoteItem();
    }

    public function testAroundItemToOrderItemPositive()
    {
        $attributeValue = 'test_value';
        $productOptions = [
            'option_1' => 'value_1',
            'option_2' => 'value_2'
        ];
        $expectedOptions = $productOptions + ['bundle_selection_attributes' => $attributeValue];

        $bundleAttribute = $this->createMock(\Magento\Catalog\Model\Product\Configuration\Item\Option::class);
        $bundleAttribute->expects($this->once())
            ->method('getValue')
            ->willReturn($attributeValue);

        $this->productMock->expects($this->once())
            ->method('getCustomOption')
            ->with('bundle_selection_attributes')
            ->willReturn($bundleAttribute);
        $this->quoteItemMock->expects($this->once())->method('getProduct')->willReturn($this->productMock);

        $this->orderItemMock->expects($this->once())->method('getProductOptions')->willReturn($productOptions);
        $this->orderItemMock->expects($this->once())->method('setProductOptions')->with($expectedOptions);

        $orderItem = $this->model->afterConvert($this->subjectMock, $this->orderItemMock, $this->quoteItemMock);
        $this->assertSame($this->orderItemMock, $orderItem);
    }

    public function testAroundItemToOrderItemNegative()
    {
        $this->productMock->expects($this->once())
            ->method('getCustomOption')
            ->with('bundle_selection_attributes')->willReturn(false);

        $this->quoteItemMock->expects($this->once())->method('getProduct')
            ->willReturn($this->productMock);
        $this->orderItemMock->expects($this->never())->method('setProductOptions');

        $orderItem = $this->model->afterConvert($this->subjectMock, $this->orderItemMock, $this->quoteItemMock);
        $this->assertSame($this->orderItemMock, $orderItem);
    }
}