|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Copyright © Magento, Inc. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | +namespace Magento\GroupedProduct\Test\Unit\Model\Wishlist\Product; |
| 8 | + |
| 9 | +use Magento\GroupedProduct\Model\Product\Type\Grouped as TypeGrouped; |
| 10 | + |
| 11 | +/** |
| 12 | + * Unit test for Wishlist Item Plugin. |
| 13 | + */ |
| 14 | +class ItemTest extends \PHPUnit\Framework\TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var \Magento\GroupedProduct\Model\Wishlist\Product\Item |
| 18 | + */ |
| 19 | + protected $model; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject |
| 23 | + */ |
| 24 | + protected $productMock; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject |
| 28 | + */ |
| 29 | + protected $subjectMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * Init Mock Objects |
| 33 | + */ |
| 34 | + protected function setUp() |
| 35 | + { |
| 36 | + $this->subjectMock = $this->createPartialMock( |
| 37 | + \Magento\Wishlist\Model\Item::class, |
| 38 | + [ |
| 39 | + 'getOptionsByCode', |
| 40 | + 'getBuyRequest', |
| 41 | + 'setOptions', |
| 42 | + 'mergeBuyRequest', |
| 43 | + 'getProduct' |
| 44 | + ] |
| 45 | + ); |
| 46 | + |
| 47 | + $this->productMock = $this->createPartialMock( |
| 48 | + \Magento\Catalog\Model\Product::class, |
| 49 | + [ |
| 50 | + 'getId', |
| 51 | + 'getTypeId', |
| 52 | + 'getCustomOptions' |
| 53 | + ] |
| 54 | + ); |
| 55 | + |
| 56 | + $this->model = new \Magento\GroupedProduct\Model\Wishlist\Product\Item(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Test Before Represent Product method |
| 61 | + */ |
| 62 | + public function testBeforeRepresentProduct() |
| 63 | + { |
| 64 | + $testSimpleProdId = 34; |
| 65 | + $prodInitQty = 2; |
| 66 | + $prodQtyInWishlist = 3; |
| 67 | + $resWishlistQty = $prodInitQty + $prodQtyInWishlist; |
| 68 | + $superGroup = [ |
| 69 | + 'super_group' => [ |
| 70 | + 33 => "0", |
| 71 | + 34 => 3, |
| 72 | + 35 => "0" |
| 73 | + ] |
| 74 | + ]; |
| 75 | + |
| 76 | + $superGroupObj = new \Magento\Framework\DataObject($superGroup); |
| 77 | + |
| 78 | + $this->productMock->expects($this->once())->method('getId')->willReturn($testSimpleProdId); |
| 79 | + $this->productMock->expects($this->once())->method('getTypeId') |
| 80 | + ->willReturn(TypeGrouped::TYPE_CODE); |
| 81 | + $this->productMock->expects($this->once())->method('getCustomOptions') |
| 82 | + ->willReturn( |
| 83 | + $this->getProductAssocOption($prodInitQty, $testSimpleProdId) |
| 84 | + ); |
| 85 | + |
| 86 | + $wishlistItemProductMock = $this->createPartialMock( |
| 87 | + \Magento\Catalog\Model\Product::class, |
| 88 | + [ |
| 89 | + 'getId', |
| 90 | + ] |
| 91 | + ); |
| 92 | + $wishlistItemProductMock->expects($this->once())->method('getId')->willReturn($testSimpleProdId); |
| 93 | + |
| 94 | + $this->subjectMock->expects($this->once())->method('getProduct') |
| 95 | + ->willReturn($wishlistItemProductMock); |
| 96 | + $this->subjectMock->expects($this->once())->method('getOptionsByCode') |
| 97 | + ->willReturn( |
| 98 | + $this->getWishlistAssocOption($prodQtyInWishlist, $resWishlistQty, $testSimpleProdId) |
| 99 | + ); |
| 100 | + $this->subjectMock->expects($this->once())->method('getBuyRequest')->willReturn($superGroupObj); |
| 101 | + |
| 102 | + $this->model->beforeRepresentProduct($this->subjectMock, $this->productMock); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Test Before Compare Options method with same keys |
| 107 | + */ |
| 108 | + public function testBeforeCompareOptionsSameKeys() |
| 109 | + { |
| 110 | + $options1 = ['associated_product_34' => 3]; |
| 111 | + $options2 = ['associated_product_34' => 2]; |
| 112 | + |
| 113 | + $res = $this->model->beforeCompareOptions($this->subjectMock, $options1, $options2); |
| 114 | + |
| 115 | + $this->assertEquals([], $res[0]); |
| 116 | + $this->assertEquals([], $res[1]); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Test Before Compare Options method with diff keys |
| 121 | + */ |
| 122 | + public function testBeforeCompareOptionsDiffKeys() |
| 123 | + { |
| 124 | + $options1 = ['associated_product_1' => 3]; |
| 125 | + $options2 = ['associated_product_34' => 2]; |
| 126 | + |
| 127 | + $res = $this->model->beforeCompareOptions($this->subjectMock, $options1, $options2); |
| 128 | + |
| 129 | + $this->assertEquals($options1, $res[0]); |
| 130 | + $this->assertEquals($options2, $res[1]); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Return mock array with wishlist options |
| 135 | + * |
| 136 | + * @param int $initVal |
| 137 | + * @param int $resVal |
| 138 | + * @param int $prodId |
| 139 | + * @return array |
| 140 | + */ |
| 141 | + private function getWishlistAssocOption($initVal, $resVal, $prodId) |
| 142 | + { |
| 143 | + $items = []; |
| 144 | + |
| 145 | + $optionMock = $this->createPartialMock( |
| 146 | + \Magento\Wishlist\Model\Item\Option::class, |
| 147 | + [ |
| 148 | + 'getValue', |
| 149 | + ] |
| 150 | + ); |
| 151 | + $optionMock->expects($this->at(0))->method('getValue')->willReturn($initVal); |
| 152 | + $optionMock->expects($this->at(1))->method('getValue')->willReturn($resVal); |
| 153 | + |
| 154 | + $items['associated_product_' . $prodId] = $optionMock; |
| 155 | + |
| 156 | + return $items; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Return mock array with product options |
| 161 | + * |
| 162 | + * @param int $initVal |
| 163 | + * @param int $prodId |
| 164 | + * @return array |
| 165 | + */ |
| 166 | + private function getProductAssocOption($initVal, $prodId) |
| 167 | + { |
| 168 | + $items = []; |
| 169 | + |
| 170 | + $optionMock = $this->createPartialMock( |
| 171 | + \Magento\Catalog\Model\Product\Configuration\Item\Option::class, |
| 172 | + [ |
| 173 | + 'getValue', |
| 174 | + ] |
| 175 | + ); |
| 176 | + |
| 177 | + $optionMock->expects($this->once())->method('getValue')->willReturn($initVal); |
| 178 | + |
| 179 | + $items['associated_product_' . $prodId] = $optionMock; |
| 180 | + |
| 181 | + return $items; |
| 182 | + } |
| 183 | +} |
0 commit comments