Skip to content

Commit 85036fa

Browse files
committed
Refactor code to pass review
1 parent 8201ea2 commit 85036fa

File tree

1 file changed

+91
-62
lines changed

1 file changed

+91
-62
lines changed

app/code/Magento/GiftMessage/Test/Unit/Observer/SalesEventOrderItemToQuoteItemObserverTest.php

Lines changed: 91 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ class SalesEventOrderItemToQuoteItemObserverTest extends TestCase
9393
*/
9494
public function setUp(): void
9595
{
96-
$this->messageFactoryMock = $this->createMock(MessageFactory::class);
96+
$this->messageFactoryMock = $this->getMockBuilder(MessageFactory::class)
97+
->disableOriginalConstructor()
98+
->setMethods(['create'])
99+
->getMock();
97100
$this->giftMessageHelperMock = $this->createMock(MessageHelper::class);
98101
$this->observerMock = $this->createMock(Observer::class);
99102
$this->eventMock = $this->createPartialMock(Event::class, ['getOrderItem', 'getQuoteItem']);
@@ -106,6 +109,18 @@ public function setUp(): void
106109
$this->storeMock = $this->createMock(Store::class);
107110
$this->messageMock = $this->createMock(MessageModel::class);
108111

112+
$this->eventMock->expects($this->atLeastOnce())
113+
->method('getOrderItem')
114+
->willReturn($this->orderItemMock);
115+
116+
$this->orderItemMock->expects($this->atLeastOnce())
117+
->method('getOrder')
118+
->willReturn($this->orderMock);
119+
120+
$this->observerMock->expects($this->atLeastOnce())
121+
->method('getEvent')
122+
->willReturn($this->eventMock);
123+
109124
$objectManager = new ObjectManager($this);
110125

111126
$this->observer = $objectManager->getObject(
@@ -118,79 +133,93 @@ public function setUp(): void
118133
}
119134

120135
/**
121-
* Tests duplicating gift message from order item to quote item
122-
*
123-
* @param bool $orderIsReordered
124-
* @param bool $isMessagesAllowed
125-
* @dataProvider giftMessageDataProvider
136+
* Test when the order is reorder
126137
*/
127-
public function testExecute($orderIsReordered, $isMessagesAllowed)
138+
public function testReorder()
128139
{
129-
$this->eventMock->expects($this->atLeastOnce())
130-
->method('getOrderItem')
131-
->willReturn($this->orderItemMock);
140+
$this->orderMock->expects($this->once())
141+
->method('getReordered')
142+
->willReturn(true);
132143

133-
$this->orderItemMock->expects($this->atLeastOnce())
134-
->method('getOrder')
135-
->willReturn($this->orderMock);
144+
$this->giftMessageHelperMock->expects($this->never())
145+
->method('isMessagesAllowed');
136146

137-
$this->observerMock->expects($this->atLeastOnce())
138-
->method('getEvent')
139-
->willReturn($this->eventMock);
147+
$this->eventMock
148+
->expects($this->never())
149+
->method('getQuoteItem')
150+
->willReturn($this->quoteItemMock);
151+
152+
/** Run observer */
153+
$this->observer->execute($this->observerMock);
154+
}
140155

141-
if (!$orderIsReordered && $isMessagesAllowed) {
142-
$this->eventMock
143-
->expects($this->atLeastOnce())
144-
->method('getQuoteItem')
145-
->willReturn($this->quoteItemMock);
146-
$this->orderMock->expects($this->once())
147-
->method('getReordered')
148-
->willReturn($orderIsReordered);
149-
$this->orderItemMock->expects($this->once())
150-
->method('getGiftMessageId')
151-
->willReturn(self::STUB_MESSAGE_ID);
152-
$this->giftMessageHelperMock->expects($this->once())
153-
->method('isMessagesAllowed')
154-
->willReturn($isMessagesAllowed);
155-
$this->messageFactoryMock->expects($this->once())
156-
->method('create')
157-
->willReturn($this->messageMock);
158-
$this->messageMock->expects($this->once())
159-
->method('load')
160-
->with(self::STUB_MESSAGE_ID)
161-
->willReturnSelf();
162-
$this->messageMock->expects($this->once())
163-
->method('setId')
164-
->with(null)
165-
->willReturnSelf();
166-
$this->messageMock->expects($this->once())
167-
->method('save')
168-
->willReturnSelf();
169-
$this->messageMock->expects($this->once())
170-
->method('getId')
171-
->willReturn(self::STUB_NEW_MESSAGE_ID);
172-
$this->quoteItemMock->expects($this->once())
173-
->method('setGiftMessageId')
174-
->with(self::STUB_NEW_MESSAGE_ID)
175-
->willReturnSelf();
176-
}
156+
/**
157+
* Test when the order is new reorder and gift message is not allowed
158+
*/
159+
public function testNewOrderWhenGiftMessageIsNotAllowed()
160+
{
161+
$this->orderMock->expects($this->once())
162+
->method('getReordered')
163+
->willReturn(false);
164+
165+
$this->giftMessageHelperMock->expects($this->once())
166+
->method('isMessagesAllowed')
167+
->willReturn(false);
168+
169+
$this->eventMock
170+
->expects($this->never())
171+
->method('getQuoteItem')
172+
->willReturn($this->quoteItemMock);
177173

178174
/** Run observer */
179175
$this->observer->execute($this->observerMock);
180176
}
181177

182178
/**
183-
* Providing gift message data for test
184-
*
185-
* @return array
179+
* Test when the order is new reorder and gift message is allowed
186180
*/
187-
public function giftMessageDataProvider()
181+
public function testNewOrderWhenGiftMessageIsAllowed()
188182
{
189-
return [
190-
'order is not reordered, messages is allowed' => [false, true],
191-
'order is reordered, messages is allowed' => [true, true],
192-
'order is reordered, messages is not allowed' => [true, false],
193-
'order is not reordered, messages is not allowed' => [false, false]
194-
];
183+
$this->orderMock->expects($this->once())
184+
->method('getReordered')
185+
->willReturn(false);
186+
187+
$this->giftMessageHelperMock->expects($this->once())
188+
->method('isMessagesAllowed')
189+
->willReturn(true);
190+
191+
$this->eventMock
192+
->expects($this->atLeastOnce())
193+
->method('getQuoteItem')
194+
->willReturn($this->quoteItemMock);
195+
196+
$this->orderItemMock->expects($this->once())
197+
->method('getGiftMessageId')
198+
->willReturn(self::STUB_MESSAGE_ID);
199+
200+
$this->messageFactoryMock->expects($this->once())
201+
->method('create')
202+
->willReturn($this->messageMock);
203+
$this->messageMock->expects($this->once())
204+
->method('load')
205+
->with(self::STUB_MESSAGE_ID)
206+
->willReturnSelf();
207+
$this->messageMock->expects($this->once())
208+
->method('setId')
209+
->with(null)
210+
->willReturnSelf();
211+
$this->messageMock->expects($this->once())
212+
->method('save')
213+
->willReturnSelf();
214+
$this->messageMock->expects($this->once())
215+
->method('getId')
216+
->willReturn(self::STUB_NEW_MESSAGE_ID);
217+
$this->quoteItemMock->expects($this->once())
218+
->method('setGiftMessageId')
219+
->with(self::STUB_NEW_MESSAGE_ID)
220+
->willReturnSelf();
221+
222+
/** Run observer */
223+
$this->observer->execute($this->observerMock);
195224
}
196225
}

0 commit comments

Comments
 (0)