|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Copyright © 2015 Magento. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Magento\Quote\Test\Unit\Model\Quote\Item; |
| 9 | + |
| 10 | +use JsonSchema\Constraints\Object; |
| 11 | +use Magento\Framework\App\ObjectManager; |
| 12 | +use Magento\Quote\Model\GuestCart\GuestCartItemRepository; |
| 13 | +use Magento\Quote\Model\QuoteIdMaskFactory; |
| 14 | + |
| 15 | +class GuestCartItemRepositoryTest |
| 16 | +// extends RepositoryTest |
| 17 | +extends \PHPUnit_Framework_TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var GuestCartItemRepository |
| 21 | + */ |
| 22 | + protected $repository; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 26 | + */ |
| 27 | + protected $quoteRepositoryMock; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + protected $productRepositoryMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 36 | + */ |
| 37 | + protected $dataMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + protected $quoteMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 46 | + */ |
| 47 | + protected $productMock; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 51 | + */ |
| 52 | + protected $quoteItemMock; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 56 | + */ |
| 57 | + protected $itemDataFactoryMock; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var \Magento\Quote\Model\QuoteIdMaskFactory |
| 61 | + */ |
| 62 | + protected $quoteIdMaskFactoryMock; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 66 | + */ |
| 67 | + protected $quoteIdMaskMock; |
| 68 | + |
| 69 | + /** |
| 70 | + * @return void |
| 71 | + */ |
| 72 | + protected function setUp() |
| 73 | + { |
| 74 | + $this->quoteRepositoryMock = |
| 75 | + $this->getMock('\Magento\Quote\Model\QuoteRepository', [], [], '', false); |
| 76 | + $this->productRepositoryMock = |
| 77 | + $this->getMock('Magento\Catalog\Api\ProductRepositoryInterface', [], [], '', false); |
| 78 | + $this->itemDataFactoryMock = |
| 79 | + $this->getMock('Magento\Quote\Api\Data\CartItemInterfaceFactory', ['create'], [], '', false); |
| 80 | + $this->dataMock = $this->getMock('Magento\Quote\Api\Data\CartItemInterface'); |
| 81 | + $this->quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false); |
| 82 | + $this->productMock = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false); |
| 83 | + $this->quoteItemMock = |
| 84 | + $this->getMock('Magento\Quote\Model\Quote\Item', ['getId', 'getSku', 'setData', '__wakeUp'], [], '', false); |
| 85 | + $this->quoteIdMaskFactoryMock = $this->getMock('Magento\Quote\Model\QuoteIdMaskFactory', [], [], '', false); |
| 86 | + $this->quoteIdMaskMock = $this->getMock('Magento\Quote\Model\QuoteIdMask', [], [], '', false); |
| 87 | + |
| 88 | + $this->repository = new GuestCartItemRepository( |
| 89 | + $this->quoteRepositoryMock, |
| 90 | + $this->productRepositoryMock, |
| 91 | + $this->itemDataFactoryMock, |
| 92 | + $this->quoteIdMaskFactoryMock |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @return void |
| 98 | + */ |
| 99 | + public function testSave() |
| 100 | + { |
| 101 | + $maskedCartId = 'f216207248d65c789b17be8545e0aa73'; |
| 102 | + $cartId = 13; |
| 103 | + $this->quoteIdMaskFactoryMock->expects($this->any())->method('create')->willReturn($this->quoteIdMaskMock); |
| 104 | + $this->quoteIdMaskMock->expects($this->any()) |
| 105 | + ->method('load') |
| 106 | + ->with($maskedCartId, 'masked_id') |
| 107 | + ->willReturn($this->quoteIdMaskMock); |
| 108 | + $this->quoteIdMaskMock->expects($this->any()) |
| 109 | + ->method('getId') |
| 110 | + ->willReturn($cartId); |
| 111 | + |
| 112 | + $this->dataMock->expects($this->at(0))->method('getQuoteId')->willReturn($maskedCartId); |
| 113 | + $this->dataMock->expects($this->once())->method('setQuoteId')->with($cartId); |
| 114 | + $this->dataMock->expects($this->once())->method('getQty')->will($this->returnValue(12)); |
| 115 | + $this->dataMock->expects($this->at(3))->method('getQuoteId')->willReturn($cartId); |
| 116 | + $this->quoteRepositoryMock->expects($this->once()) |
| 117 | + ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock)); |
| 118 | + $this->productRepositoryMock->expects($this->once()) |
| 119 | + ->method('get') |
| 120 | + ->will($this->returnValue($this->productMock)); |
| 121 | + $this->dataMock->expects($this->once())->method('getSku'); |
| 122 | + $this->quoteMock->expects($this->once())->method('addProduct')->with($this->productMock, 12); |
| 123 | + $this->quoteMock->expects($this->never())->method('getItemById'); |
| 124 | + $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock)); |
| 125 | + $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock); |
| 126 | + $this->quoteMock |
| 127 | + ->expects($this->once()) |
| 128 | + ->method('getItemByProduct') |
| 129 | + ->with($this->productMock) |
| 130 | + ->will($this->returnValue($this->quoteItemMock)); |
| 131 | + $this->dataMock->expects($this->once())->method('getItemId')->will($this->returnValue(null)); |
| 132 | + $this->assertEquals($this->quoteItemMock, $this->repository->save($this->dataMock)); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @return void |
| 137 | + */ |
| 138 | + public function testUpdateItem() |
| 139 | + { |
| 140 | + $maskedCartId = 'f216207248d65c789b17be8545e0aa73'; |
| 141 | + $cartId = 13; |
| 142 | + $this->quoteIdMaskFactoryMock->expects($this->any())->method('create')->willReturn($this->quoteIdMaskMock); |
| 143 | + $this->quoteIdMaskMock->expects($this->any()) |
| 144 | + ->method('load') |
| 145 | + ->with($maskedCartId, 'masked_id') |
| 146 | + ->willReturn($this->quoteIdMaskMock); |
| 147 | + $this->quoteIdMaskMock->expects($this->any()) |
| 148 | + ->method('getId') |
| 149 | + ->willReturn($cartId); |
| 150 | + |
| 151 | + $itemId = 5; |
| 152 | + $productSku = 'product_sku'; |
| 153 | + $this->dataMock->expects($this->at(0))->method('getQuoteId')->willReturn($maskedCartId); |
| 154 | + $this->dataMock->expects($this->once())->method('setQuoteId')->with($cartId); |
| 155 | + $this->dataMock->expects($this->once())->method('getQty')->will($this->returnValue(12)); |
| 156 | + $this->dataMock->expects($this->at(3))->method('getQuoteId')->willReturn($cartId); |
| 157 | + $this->dataMock->expects($this->once())->method('getItemId')->will($this->returnValue($itemId)); |
| 158 | + $this->quoteRepositoryMock->expects($this->once()) |
| 159 | + ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock)); |
| 160 | + $this->quoteMock->expects($this->once()) |
| 161 | + ->method('getItemById')->with($itemId)->will($this->returnValue($this->quoteItemMock)); |
| 162 | + $this->quoteItemMock->expects($this->once())->method('setData')->with('qty', 12); |
| 163 | + $this->quoteItemMock->expects($this->once())->method('getSku')->willReturn($productSku); |
| 164 | + $this->productRepositoryMock |
| 165 | + ->expects($this->once()) |
| 166 | + ->method('get') |
| 167 | + ->with($productSku) |
| 168 | + ->willReturn($this->productMock); |
| 169 | + $this->quoteItemMock->expects($this->never())->method('addProduct'); |
| 170 | + $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock)); |
| 171 | + $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock); |
| 172 | + $this->quoteMock |
| 173 | + ->expects($this->once()) |
| 174 | + ->method('getItemByProduct') |
| 175 | + ->with($this->productMock) |
| 176 | + ->willReturn($this->quoteItemMock); |
| 177 | + $this->assertEquals($this->quoteItemMock, $this->repository->save($this->dataMock)); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * @return void |
| 182 | + */ |
| 183 | + public function testGetList() |
| 184 | + { |
| 185 | + $maskedCartId = 'f216207248d65c789b17be8545e0aa73'; |
| 186 | + $cartId = 33; |
| 187 | + $this->quoteIdMaskFactoryMock->expects($this->any())->method('create')->willReturn($this->quoteIdMaskMock); |
| 188 | + $this->quoteIdMaskMock->expects($this->any()) |
| 189 | + ->method('load') |
| 190 | + ->with($maskedCartId, 'masked_id') |
| 191 | + ->willReturn($this->quoteIdMaskMock); |
| 192 | + $this->quoteIdMaskMock->expects($this->any()) |
| 193 | + ->method('getId') |
| 194 | + ->willReturn($cartId); |
| 195 | + |
| 196 | + $quoteMock = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false); |
| 197 | + $this->quoteRepositoryMock->expects($this->once())->method('getActive') |
| 198 | + ->with($cartId) |
| 199 | + ->will($this->returnValue($quoteMock)); |
| 200 | + $itemMock = $this->getMock('\Magento\Quote\Model\Quote\Item', [], [], '', false); |
| 201 | + $quoteMock->expects($this->any())->method('getAllItems')->will($this->returnValue([$itemMock])); |
| 202 | + |
| 203 | + $this->assertEquals([$itemMock], $this->repository->getList($maskedCartId)); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * @return void |
| 208 | + */ |
| 209 | + public function testDeleteById() |
| 210 | + { |
| 211 | + $maskedCartId = 'f216207248d65c789b17be8545e0aa73'; |
| 212 | + $cartId = 33; |
| 213 | + $this->quoteIdMaskFactoryMock->expects($this->any())->method('create')->willReturn($this->quoteIdMaskMock); |
| 214 | + $this->quoteIdMaskMock->expects($this->any()) |
| 215 | + ->method('load') |
| 216 | + ->with($maskedCartId, 'masked_id') |
| 217 | + ->willReturn($this->quoteIdMaskMock); |
| 218 | + $this->quoteIdMaskMock->expects($this->any()) |
| 219 | + ->method('getId') |
| 220 | + ->willReturn($cartId); |
| 221 | + |
| 222 | + $itemId = 5; |
| 223 | + $this->itemDataFactoryMock->expects($this->once())->method('create')->willReturn($this->dataMock); |
| 224 | + $this->dataMock->expects($this->once())->method('setQuoteId') |
| 225 | + ->with($cartId)->willReturn($this->dataMock); |
| 226 | + $this->dataMock->expects($this->once())->method('setItemId') |
| 227 | + ->with($itemId)->willReturn($this->dataMock); |
| 228 | + $this->dataMock->expects($this->once())->method('getQuoteId')->willReturn($cartId); |
| 229 | + $this->dataMock->expects($this->once())->method('getItemId')->willReturn($itemId); |
| 230 | + $this->quoteRepositoryMock->expects($this->once()) |
| 231 | + ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock)); |
| 232 | + $this->quoteMock->expects($this->once()) |
| 233 | + ->method('getItemById')->with($itemId)->will($this->returnValue($this->quoteItemMock)); |
| 234 | + $this->quoteMock->expects($this->once())->method('removeItem'); |
| 235 | + $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock)); |
| 236 | + $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock); |
| 237 | + |
| 238 | + $this->assertTrue($this->repository->deleteById($maskedCartId, $itemId)); |
| 239 | + } |
| 240 | +} |
0 commit comments