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,25 @@ 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 ->getMockBuilder (OrderExtension::class)
90
+ ->disableOriginalConstructor ()
91
+ ->addMethods (['getGiftMessage ' , 'setGiftMessage ' ])
92
+ ->getMock ();
111
93
$ this ->giftMessageMock = $ this ->createMock (
112
94
MessageInterface::class
113
95
);
114
- $ this ->orderItemMock = $ this ->createMock (
115
- OrderItemInterface::class
116
- );
117
- $ this ->orderItemExtensionMock = $ this ->getOrderItemExtensionMock ();
96
+ $ this ->orderItemMock = $ this ->getMockBuilder (OrderItemInterface::class)
97
+ ->disableOriginalConstructor ()
98
+ ->addMethods (['getGiftMessageId ' ])
99
+ ->getMockForAbstractClass ();
100
+ $ this ->orderItemExtensionMock = $ this ->getMockBuilder (OrderItemExtension::class)
101
+ ->disableOriginalConstructor ()
102
+ ->addMethods (['getGiftMessage ' , 'setGiftMessage ' ])
103
+ ->getMock ();
118
104
$ this ->orderRepositoryMock = $ this ->createMock (
119
105
\Magento \Sales \Api \OrderRepositoryInterface::class
120
106
);
@@ -123,16 +109,19 @@ protected function setUp(): void
123
109
124
110
$ this ->plugin = new OrderGet (
125
111
$ this ->giftMessageOrderRepositoryMock ,
126
- $ this ->giftMessageOrderItemRepositoryMock ,
127
- $ this ->orderExtensionFactoryMock ,
128
- $ this ->orderItemExtensionFactoryMock
112
+ $ this ->giftMessageOrderItemRepositoryMock
129
113
);
130
114
}
131
115
132
- public function testAfterGetGiftMessageOnOrderLevel ()
116
+ /**
117
+ * @return void
118
+ */
119
+ public function testAfterGetGiftMessageOnOrderLevel (): void
133
120
{
134
121
//set Gift Message for Order
135
122
$ orderId = 1 ;
123
+ $ messageId = 1 ;
124
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn ($ messageId );
136
125
$ this ->orderMock ->expects ($ this ->once ())->method ('getEntityId ' )->willReturn ($ orderId );
137
126
$ this ->orderMock
138
127
->expects ($ this ->once ())
@@ -160,12 +149,17 @@ public function testAfterGetGiftMessageOnOrderLevel()
160
149
$ this ->plugin ->afterGet ($ this ->orderRepositoryMock , $ this ->orderMock );
161
150
}
162
151
163
- public function testAfterGetGiftMessageOnItemLevel ()
152
+ /**
153
+ * @return void
154
+ */
155
+ public function testAfterGetGiftMessageOnItemLevel (): void
164
156
{
165
157
//set Gift Message for Order
166
158
$ orderId = 1 ;
167
159
$ orderItemId = 2 ;
160
+ $ messageId = 1 ;
168
161
$ this ->orderItemMock ->expects ($ this ->once ())->method ('getItemId ' )->willReturn ($ orderItemId );
162
+ $ this ->orderItemMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn ($ messageId );
169
163
$ this ->orderMock ->expects ($ this ->once ())->method ('getEntityId ' )->willReturn ($ orderId );
170
164
$ this ->orderMock
171
165
->expects ($ this ->once ())
@@ -198,23 +192,23 @@ public function testAfterGetGiftMessageOnItemLevel()
198
192
$ this ->plugin ->afterGet ($ this ->orderRepositoryMock , $ this ->orderMock );
199
193
}
200
194
201
- public function testGetAfterWhenMessagesAreNotSet ()
195
+ /**
196
+ * @return void
197
+ */
198
+ public function testGetAfterWhenMessagesAreNotSet (): void
202
199
{
203
- $ orderId = 1 ;
204
- $ orderItemId = 2 ;
205
200
//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 );
201
+ $ this ->orderMock ->expects ($ this ->never ())->method ('getEntityId ' );
202
+ $ this ->orderItemMock ->expects ($ this ->never ())->method ('getItemId ' );
203
+ $ this ->orderItemMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn (null );
208
204
$ this ->orderMock
209
205
->expects ($ this ->once ())
210
206
->method ('getExtensionAttributes ' )
211
207
->willReturn ($ this ->orderExtensionMock );
212
208
$ this ->orderExtensionMock ->expects ($ this ->once ())->method ('getGiftMessage ' )->willReturn ([]);
213
209
$ this ->giftMessageOrderRepositoryMock
214
- ->expects ($ this ->once ())
215
- ->method ('get ' )
216
- ->with ($ orderId )
217
- ->willThrowException (new NoSuchEntityException ());
210
+ ->expects ($ this ->never ())
211
+ ->method ('get ' );
218
212
$ this ->orderExtensionMock
219
213
->expects ($ this ->never ())
220
214
->method ('setGiftMessage ' );
@@ -227,22 +221,25 @@ public function testGetAfterWhenMessagesAreNotSet()
227
221
->willReturn ($ this ->orderItemExtensionMock );
228
222
$ this ->orderItemExtensionMock ->expects ($ this ->once ())->method ('getGiftMessage ' )->willReturn ([]);
229
223
$ this ->giftMessageOrderItemRepositoryMock
230
- ->expects ($ this ->once ())
231
- ->method ('get ' )
232
- ->with ($ orderId , $ orderItemId )
233
- ->willThrowException (new NoSuchEntityException ());
224
+ ->expects ($ this ->never ())
225
+ ->method ('get ' );
234
226
$ this ->orderItemExtensionMock
235
227
->expects ($ this ->never ())
236
228
->method ('setGiftMessage ' );
237
229
238
230
$ this ->plugin ->afterGet ($ this ->orderRepositoryMock , $ this ->orderMock );
239
231
}
240
232
241
- public function testAfterGetList ()
233
+ /**
234
+ * @return void
235
+ */
236
+ public function testAfterGetList (): void
242
237
{
243
238
//set Gift Message List for Order
244
239
$ orderId = 1 ;
240
+ $ messageId = 1 ;
245
241
$ this ->orderMock ->expects ($ this ->once ())->method ('getEntityId ' )->willReturn ($ orderId );
242
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn ($ messageId );
246
243
$ this ->orderMock
247
244
->expects ($ this ->once ())
248
245
->method ('getExtensionAttributes ' )
@@ -269,38 +266,4 @@ public function testAfterGetList()
269
266
$ this ->collectionMock ->expects ($ this ->once ())->method ('getItems ' )->willReturn ([$ this ->orderMock ]);
270
267
$ this ->plugin ->afterGetList ($ this ->orderRepositoryMock , $ this ->collectionMock );
271
268
}
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
269
}
0 commit comments