Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartUpdateBeforeTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Wishlist\Observer\CartUpdateBefore as Observer;
10 
14 class CartUpdateBeforeTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $observer;
20 
24  protected $helper;
25 
29  protected $checkoutSession;
30 
34  protected $customerSession;
35 
39  protected $wishlistFactory;
40 
44  protected $wishlist;
45 
49  protected $messageManager;
50 
51  protected function setUp()
52  {
53  $this->helper = $this->getMockBuilder(\Magento\Wishlist\Helper\Data::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->wishlistFactory = $this->getMockBuilder(\Magento\Wishlist\Model\WishlistFactory::class)
57  ->disableOriginalConstructor()
58  ->setMethods(['create'])
59  ->getMock();
60  $this->wishlist = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63  $this->wishlistFactory->expects($this->any())
64  ->method('create')
65  ->willReturn($this->wishlist);
66 
67  $this->observer = new Observer(
68  $this->helper,
69  $this->wishlistFactory
70  );
71  }
72 
77  public function testExecute()
78  {
79  $customerId = 1;
80  $itemId = 5;
81  $itemQty = 123;
82  $productId = 321;
83 
84  $eventObserver = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
85  ->disableOriginalConstructor()
86  ->getMock();
87 
88  $event = $this->getMockBuilder(\Magento\Framework\Event::class)
89  ->setMethods(['getCart', 'getInfo'])
90  ->disableOriginalConstructor()
91  ->getMock();
92 
93  $eventObserver->expects($this->exactly(2))
94  ->method('getEvent')
95  ->willReturn($event);
96 
97  $quoteItem = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
98  ->setMethods(['getProductId', 'getBuyRequest', '__wakeup'])
99  ->disableOriginalConstructor()
100  ->getMock();
101 
102  $buyRequest = $this->getMockBuilder(\Magento\Framework\DataObject::class)
103  ->setMethods(['setQty'])
104  ->disableOriginalConstructor()
105  ->getMock();
106 
107  $infoData = $this->getMockBuilder(\Magento\Framework\DataObject::class)
108  ->setMethods(['toArray'])
109  ->disableOriginalConstructor()
110  ->getMock();
111 
112  $infoData->expects($this->once())
113  ->method('toArray')
114  ->willReturn([$itemId => ['qty' => $itemQty, 'wishlist' => true]]);
115 
116  $cart = $this->getMockBuilder(\Magento\Checkout\Model\Cart::class)->disableOriginalConstructor()->getMock();
117  $quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
118  ->setMethods(['getCustomerId', 'getItemById', 'removeItem', '__wakeup'])
119  ->disableOriginalConstructor()
120  ->getMock();
121 
122  $event->expects($this->once())
123  ->method('getCart')
124  ->willReturn($cart);
125 
126  $event->expects($this->once())
127  ->method('getInfo')
128  ->willReturn($infoData);
129 
130  $cart->expects($this->any())
131  ->method('getQuote')
132  ->willReturn($quote);
133 
134  $quoteItem->expects($this->once())
135  ->method('getProductId')
136  ->willReturn($productId);
137  $quoteItem->expects($this->once())
138  ->method('getBuyRequest')
139  ->willReturn($buyRequest);
140 
141  $buyRequest->expects($this->once())
142  ->method('setQty')
143  ->with($itemQty)
144  ->willReturnSelf();
145 
146  $quote->expects($this->once())
147  ->method('getCustomerId')
148  ->willReturn($customerId);
149 
150  $quote->expects($this->once())
151  ->method('getItemById')
152  ->with($itemId)
153  ->willReturn($quoteItem);
154 
155  $quote->expects($this->once())
156  ->method('removeItem')
157  ->with($itemId);
158 
159  $this->wishlist->expects($this->once())
160  ->method('loadByCustomerId')
161  ->with($this->logicalOr($customerId, true))
162  ->willReturnSelf();
163 
164  $this->wishlist->expects($this->once())
165  ->method('addNewItem')
166  ->with($this->logicalOr($productId, $buyRequest));
167 
168  $this->wishlist->expects($this->once())
169  ->method('save');
170 
171  $this->helper->expects($this->once())
172  ->method('calculate');
173 
175  $this->assertSame(
176  $this->observer,
177  $this->observer->execute($eventObserver)
178  );
179  }
180 }
$quote
$quoteItem
Definition: quote.php:38
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest