7
7
8
8
namespace Magento \GiftMessage \Test \Unit \Model \Plugin ;
9
9
10
- use Magento \Framework \Exception \NoSuchEntityException ;
11
10
use Magento \GiftMessage \Api \Data \MessageInterface ;
12
11
use Magento \GiftMessage \Api \OrderItemRepositoryInterface ;
13
12
use Magento \GiftMessage \Api \OrderRepositoryInterface ;
14
13
use Magento \GiftMessage \Model \Plugin \OrderGet ;
15
14
use Magento \Sales \Api \Data \OrderExtension ;
16
- use Magento \Sales \Api \Data \OrderExtensionFactory ;
17
15
use Magento \Sales \Api \Data \OrderInterface ;
18
16
use Magento \Sales \Api \Data \OrderItemExtension ;
19
- use Magento \Sales \Api \Data \OrderItemExtensionFactory ;
20
17
use Magento \Sales \Api \Data \OrderItemInterface ;
21
18
use Magento \Sales \Model \ResourceModel \Order \Collection ;
22
19
use PHPUnit \Framework \MockObject \MockObject ;
23
- use PHPUnit \Framework \MockObject \RuntimeException ;
24
20
use PHPUnit \Framework \TestCase ;
25
21
26
22
/**
@@ -43,16 +39,6 @@ class OrderGetTest extends TestCase
43
39
*/
44
40
private $ giftMessageOrderItemRepositoryMock ;
45
41
46
- /**
47
- * @var MockObject
48
- */
49
- private $ orderExtensionFactoryMock ;
50
-
51
- /**
52
- * @var MockObject
53
- */
54
- private $ orderItemExtensionFactoryMock ;
55
-
56
42
/**
57
43
* @var MockObject
58
44
*/
@@ -96,25 +82,19 @@ protected function setUp(): void
96
82
$ this ->giftMessageOrderItemRepositoryMock = $ this ->createMock (
97
83
OrderItemRepositoryInterface::class
98
84
);
99
- $ this ->orderExtensionFactoryMock = $ this ->createPartialMock (
100
- OrderExtensionFactory::class,
101
- ['create ' ]
102
- );
103
- $ this ->orderItemExtensionFactoryMock = $ this ->createPartialMock (
104
- OrderItemExtensionFactory::class,
105
- ['create ' ]
106
- );
107
- $ this ->orderMock = $ this ->createMock (
108
- OrderInterface::class
109
- );
110
- $ this ->orderExtensionMock = $ this ->getOrderExtensionMock ();
85
+ $ this ->orderMock = $ this ->getMockBuilder (OrderInterface::class)
86
+ ->disableOriginalConstructor ()
87
+ ->addMethods (['getGiftMessageId ' ])
88
+ ->getMockForAbstractClass ();
89
+ $ this ->orderExtensionMock = $ this ->createMock (OrderExtension::class);
111
90
$ this ->giftMessageMock = $ this ->createMock (
112
91
MessageInterface::class
113
92
);
114
- $ this ->orderItemMock = $ this ->createMock (
115
- OrderItemInterface::class
116
- );
117
- $ this ->orderItemExtensionMock = $ this ->getOrderItemExtensionMock ();
93
+ $ this ->orderItemMock = $ this ->getMockBuilder (OrderItemInterface::class)
94
+ ->disableOriginalConstructor ()
95
+ ->addMethods (['getGiftMessageId ' ])
96
+ ->getMockForAbstractClass ();
97
+ $ this ->orderItemExtensionMock = $ this ->createMock (OrderItemExtension::class);
118
98
$ this ->orderRepositoryMock = $ this ->createMock (
119
99
\Magento \Sales \Api \OrderRepositoryInterface::class
120
100
);
@@ -123,16 +103,19 @@ protected function setUp(): void
123
103
124
104
$ this ->plugin = new OrderGet (
125
105
$ this ->giftMessageOrderRepositoryMock ,
126
- $ this ->giftMessageOrderItemRepositoryMock ,
127
- $ this ->orderExtensionFactoryMock ,
128
- $ this ->orderItemExtensionFactoryMock
106
+ $ this ->giftMessageOrderItemRepositoryMock
129
107
);
130
108
}
131
109
132
- public function testAfterGetGiftMessageOnOrderLevel ()
110
+ /**
111
+ * @return void
112
+ */
113
+ public function testAfterGetGiftMessageOnOrderLevel (): void
133
114
{
134
115
//set Gift Message for Order
135
116
$ orderId = 1 ;
117
+ $ messageId = 1 ;
118
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn ($ messageId );
136
119
$ this ->orderMock ->expects ($ this ->once ())->method ('getEntityId ' )->willReturn ($ orderId );
137
120
$ this ->orderMock
138
121
->expects ($ this ->once ())
@@ -160,12 +143,17 @@ public function testAfterGetGiftMessageOnOrderLevel()
160
143
$ this ->plugin ->afterGet ($ this ->orderRepositoryMock , $ this ->orderMock );
161
144
}
162
145
163
- public function testAfterGetGiftMessageOnItemLevel ()
146
+ /**
147
+ * @return void
148
+ */
149
+ public function testAfterGetGiftMessageOnItemLevel (): void
164
150
{
165
151
//set Gift Message for Order
166
152
$ orderId = 1 ;
167
153
$ orderItemId = 2 ;
154
+ $ messageId = 1 ;
168
155
$ this ->orderItemMock ->expects ($ this ->once ())->method ('getItemId ' )->willReturn ($ orderItemId );
156
+ $ this ->orderItemMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn ($ messageId );
169
157
$ this ->orderMock ->expects ($ this ->once ())->method ('getEntityId ' )->willReturn ($ orderId );
170
158
$ this ->orderMock
171
159
->expects ($ this ->once ())
@@ -198,23 +186,25 @@ public function testAfterGetGiftMessageOnItemLevel()
198
186
$ this ->plugin ->afterGet ($ this ->orderRepositoryMock , $ this ->orderMock );
199
187
}
200
188
201
- public function testGetAfterWhenMessagesAreNotSet ()
189
+ /**
190
+ * @return void
191
+ */
192
+ public function testGetAfterWhenMessagesAreNotSet (): void
202
193
{
203
194
$ orderId = 1 ;
204
195
$ orderItemId = 2 ;
205
196
//set Gift Message for Order
206
- $ this ->orderMock ->expects ($ this ->exactly (2 ))->method ('getEntityId ' )->willReturn ($ orderId );
207
- $ this ->orderItemMock ->expects ($ this ->once ())->method ('getItemId ' )->willReturn ($ orderItemId );
197
+ $ this ->orderMock ->expects ($ this ->never ())->method ('getEntityId ' );
198
+ $ this ->orderItemMock ->expects ($ this ->never ())->method ('getItemId ' );
199
+ $ this ->orderItemMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn (null );
208
200
$ this ->orderMock
209
201
->expects ($ this ->once ())
210
202
->method ('getExtensionAttributes ' )
211
203
->willReturn ($ this ->orderExtensionMock );
212
204
$ this ->orderExtensionMock ->expects ($ this ->once ())->method ('getGiftMessage ' )->willReturn ([]);
213
205
$ this ->giftMessageOrderRepositoryMock
214
- ->expects ($ this ->once ())
215
- ->method ('get ' )
216
- ->with ($ orderId )
217
- ->willThrowException (new NoSuchEntityException ());
206
+ ->expects ($ this ->never ())
207
+ ->method ('get ' );
218
208
$ this ->orderExtensionMock
219
209
->expects ($ this ->never ())
220
210
->method ('setGiftMessage ' );
@@ -227,22 +217,25 @@ public function testGetAfterWhenMessagesAreNotSet()
227
217
->willReturn ($ this ->orderItemExtensionMock );
228
218
$ this ->orderItemExtensionMock ->expects ($ this ->once ())->method ('getGiftMessage ' )->willReturn ([]);
229
219
$ this ->giftMessageOrderItemRepositoryMock
230
- ->expects ($ this ->once ())
231
- ->method ('get ' )
232
- ->with ($ orderId , $ orderItemId )
233
- ->willThrowException (new NoSuchEntityException ());
220
+ ->expects ($ this ->never ())
221
+ ->method ('get ' );
234
222
$ this ->orderItemExtensionMock
235
223
->expects ($ this ->never ())
236
224
->method ('setGiftMessage ' );
237
225
238
226
$ this ->plugin ->afterGet ($ this ->orderRepositoryMock , $ this ->orderMock );
239
227
}
240
228
241
- public function testAfterGetList ()
229
+ /**
230
+ * @return void
231
+ */
232
+ public function testAfterGetList (): void
242
233
{
243
234
//set Gift Message List for Order
244
235
$ orderId = 1 ;
236
+ $ messageId = 1 ;
245
237
$ this ->orderMock ->expects ($ this ->once ())->method ('getEntityId ' )->willReturn ($ orderId );
238
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn ($ messageId );
246
239
$ this ->orderMock
247
240
->expects ($ this ->once ())
248
241
->method ('getExtensionAttributes ' )
@@ -269,38 +262,4 @@ public function testAfterGetList()
269
262
$ this ->collectionMock ->expects ($ this ->once ())->method ('getItems ' )->willReturn ([$ this ->orderMock ]);
270
263
$ this ->plugin ->afterGetList ($ this ->orderRepositoryMock , $ this ->collectionMock );
271
264
}
272
-
273
- /**
274
- * Build order extension mock.
275
- *
276
- * @return MockObject
277
- */
278
- private function getOrderExtensionMock (): MockObject
279
- {
280
- $ mockObject = $ this ->getMockBuilder (OrderExtension::class);
281
- try {
282
- $ mockObject ->addMethods (['getGiftMessage ' , 'setGiftMessage ' ]);
283
- } catch (RuntimeException $ e ) {
284
- // Order extension already generated.
285
- }
286
-
287
- return $ mockObject ->getMock ();
288
- }
289
-
290
- /**
291
- * Build order item extension mock.
292
- *
293
- * @return MockObject
294
- */
295
- private function getOrderItemExtensionMock (): MockObject
296
- {
297
- $ mockObject = $ this ->getMockBuilder (OrderItemExtension::class);
298
- try {
299
- $ mockObject ->addMethods (['getGiftMessage ' , 'setGiftMessage ' ]);
300
- } catch (RuntimeException $ e ) {
301
- // Order extension already generated.
302
- }
303
-
304
- return $ mockObject ->getMock ();
305
- }
306
265
}
0 commit comments