3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \CatalogInventory \Model \Quote \Item ;
7
9
8
10
use Magento \Catalog \Model \Product ;
14
16
use Magento \Eav \Model \Config ;
15
17
use Magento \Framework \Exception \CouldNotSaveException ;
16
18
use Magento \Framework \Exception \LocalizedException ;
19
+ use Magento \Framework \ObjectManagerInterface ;
17
20
use Magento \Quote \Api \Data \CartInterface ;
18
21
use Magento \Quote \Model \Quote ;
22
+ use Magento \Quote \Model \Quote \Item ;
19
23
use Magento \TestFramework \Helper \Bootstrap ;
20
24
use Magento \CatalogInventory \Model \Quote \Item \QuantityValidator \Initializer \Option ;
21
25
use Magento \Framework \Event \Observer ;
22
26
use Magento \Framework \Event ;
23
27
use Magento \Catalog \Api \ProductRepositoryInterface ;
24
28
use Magento \Framework \DataObject ;
25
29
use Magento \Checkout \Model \Session ;
30
+ use PHPUnit \Framework \MockObject \MockObject ;
31
+ use PHPUnit \Framework \TestCase ;
26
32
27
33
/**
28
34
* Class QuantityValidatorTest
29
35
*
30
36
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
31
37
*/
32
- class QuantityValidatorTest extends \ PHPUnit \ Framework \ TestCase
38
+ class QuantityValidatorTest extends TestCase
33
39
{
34
40
/**
35
41
* @var QuantityValidator
36
42
*/
37
43
private $ quantityValidator ;
38
44
39
45
/**
40
- * @var \PHPUnit\Framework\MockObject\ MockObject
46
+ * @var MockObject
41
47
*/
42
48
private $ observerMock ;
43
49
44
50
/**
45
- * @var \PHPUnit\Framework\MockObject\ MockObject
51
+ * @var MockObject
46
52
*/
47
53
private $ eventMock ;
48
54
49
55
/**
50
- * @var \Magento\Framework\ ObjectManagerInterface
56
+ * @var ObjectManagerInterface
51
57
*/
52
58
private $ objectManager ;
53
59
54
60
/**
55
- * @var \PHPUnit\Framework\MockObject\ MockObject
61
+ * @var MockObject
56
62
*/
57
63
private $ optionInitializer ;
58
64
59
65
/**
60
- * @var \PHPUnit\Framework\MockObject\ MockObject
66
+ * @var MockObject
61
67
*/
62
68
private $ stockState ;
63
69
64
70
/**
65
- * @var \Magento\CatalogInventory\Observer\ QuantityValidatorObserver
71
+ * @var QuantityValidatorObserver
66
72
*/
67
73
private $ observer ;
68
74
69
75
/**
70
- * Set up
76
+ * @inheritdoc
71
77
*/
72
78
protected function setUp (): void
73
79
{
74
- /** @var \Magento\Framework\ ObjectManagerInterface objectManager */
80
+ /** @var ObjectManagerInterface objectManager */
75
81
$ this ->objectManager = Bootstrap::getObjectManager ();
76
82
$ this ->observerMock = $ this ->createMock (Observer::class);
77
83
$ this ->optionInitializer = $ this ->createMock (Option::class);
@@ -89,27 +95,36 @@ protected function setUp(): void
89
95
'quantityValidator ' => $ this ->quantityValidator
90
96
]
91
97
);
92
- $ this ->eventMock = $ this ->createPartialMock (Event::class, ['getItem ' ]);
98
+
99
+ $ this ->eventMock = $ this ->getMockBuilder (Event::class)
100
+ ->disableOriginalConstructor ()
101
+ ->disableOriginalClone ()
102
+ ->disableArgumentCloning ()
103
+ ->disallowMockingUnknownTypes ()
104
+ ->addMethods (['getItem ' ])
105
+ ->getMock ();
93
106
}
94
107
95
108
/**
96
109
* @magentoDataFixture Magento/Checkout/_files/quote_with_bundle_product.php
97
110
* @magentoDbIsolation disabled
98
111
* @magentoAppIsolation enabled
112
+ *
113
+ * @return void
99
114
*/
100
- public function testQuoteWithOptions ()
115
+ public function testQuoteWithOptions (): void
101
116
{
102
- /** @var $session \Magento\Checkout\Model\ Session */
117
+ /** @var $session Session */
103
118
$ session = $ this ->objectManager ->create (Session::class);
104
119
105
- /** @var \Magento\Catalog\Api\ ProductRepositoryInterface $productRepository */
120
+ /** @var ProductRepositoryInterface $productRepository */
106
121
$ productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
107
122
/** @var $product Product */
108
123
$ product = $ productRepository ->get ('bundle-product ' );
109
124
$ resultMock = $ this ->createMock (DataObject::class);
110
125
$ this ->stockState ->expects ($ this ->any ())->method ('checkQtyIncrements ' )->willReturn ($ resultMock );
111
- /* @var $quoteItem \Magento\Quote\Model\Quote\ Item */
112
- $ quoteItem = $ this ->_getQuoteItemIdByProductId ($ session ->getQuote (), $ product ->getId ());
126
+ /* @var $quoteItem Item */
127
+ $ quoteItem = $ this ->getQuoteItemIdByProductId ($ session ->getQuote (), $ product ->getId ());
113
128
$ this ->observerMock ->expects ($ this ->once ())->method ('getEvent ' )->willReturn ($ this ->eventMock );
114
129
$ this ->optionInitializer ->expects ($ this ->any ())->method ('initialize ' )->willReturn ($ resultMock );
115
130
$ this ->eventMock ->expects ($ this ->once ())->method ('getItem ' )->willReturn ($ quoteItem );
@@ -121,21 +136,26 @@ public function testQuoteWithOptions()
121
136
* @magentoDataFixture Magento/Checkout/_files/quote_with_bundle_product.php
122
137
* @magentoDbIsolation disabled
123
138
* @magentoAppIsolation enabled
139
+ *
140
+ * @return void
124
141
*/
125
- public function testQuoteWithOptionsWithErrors ()
142
+ public function testQuoteWithOptionsWithErrors (): void
126
143
{
127
- /** @var $session \Magento\Checkout\Model\ Session */
144
+ /** @var $session Session */
128
145
$ session = $ this ->objectManager ->create (Session::class);
129
- /** @var \Magento\Catalog\Api\ ProductRepositoryInterface $productRepository */
146
+ /** @var ProductRepositoryInterface $productRepository */
130
147
$ productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
131
148
/** @var $product Product */
132
149
$ product = $ productRepository ->get ('bundle-product ' );
133
- /* @var $quoteItem \Magento\Quote\Model\Quote\Item */
134
- $ quoteItem = $ this ->_getQuoteItemIdByProductId ($ session ->getQuote (), $ product ->getId ());
135
- $ resultMock = $ this ->createPartialMock (
136
- DataObject::class,
137
- ['checkQtyIncrements ' , 'getMessage ' , 'getQuoteMessage ' , 'getHasError ' ]
138
- );
150
+ /* @var $quoteItem Item */
151
+ $ quoteItem = $ this ->getQuoteItemIdByProductId ($ session ->getQuote (), $ product ->getId ());
152
+ $ resultMock = $ this ->getMockBuilder (DataObject::class)
153
+ ->disableOriginalConstructor ()
154
+ ->disableOriginalClone ()
155
+ ->disableArgumentCloning ()
156
+ ->disallowMockingUnknownTypes ()
157
+ ->addMethods (['checkQtyIncrements ' , 'getMessage ' , 'getQuoteMessage ' , 'getHasError ' ])
158
+ ->getMock ();
139
159
$ this ->observerMock ->expects ($ this ->once ())->method ('getEvent ' )->willReturn ($ this ->eventMock );
140
160
$ this ->eventMock ->expects ($ this ->once ())->method ('getItem ' )->willReturn ($ quoteItem );
141
161
$ this ->stockState ->expects ($ this ->any ())->method ('checkQtyIncrements ' )->willReturn ($ resultMock );
@@ -150,10 +170,12 @@ public function testQuoteWithOptionsWithErrors()
150
170
* Set mock of Stock State Result to Quote Item Options.
151
171
*
152
172
*
153
- * @param \Magento\Quote\Model\Quote\Item $quoteItem
154
- * @param \PHPUnit\Framework\MockObject\MockObject $resultMock
173
+ * @param Item $quoteItem
174
+ * @param MockObject $resultMock
175
+ *
176
+ * @return void
155
177
*/
156
- private function setMockStockStateResultToQuoteItemOptions ($ quoteItem , $ resultMock )
178
+ private function setMockStockStateResultToQuoteItemOptions ($ quoteItem , $ resultMock ): void
157
179
{
158
180
if ($ options = $ quoteItem ->getQtyOptions ()) {
159
181
foreach ($ options as $ option ) {
@@ -252,27 +274,29 @@ public function quantityDataProvider(): array
252
274
[
253
275
'quantity ' => 1000 ,
254
276
'error_regexp ' => ''
255
- ],
256
-
277
+ ]
257
278
];
258
279
}
259
280
260
281
/**
261
- * Gets \Magento\Quote\Model\Quote\Item from \Magento\Quote\Model\Quote by product id
282
+ * Gets \Magento\Quote\Model\Quote\Item from \Magento\Quote\Model\Quote by product id.
262
283
*
263
284
* @param Quote $quote
264
285
* @param int $productId
265
- * @return \Magento\Quote\Model\Quote\Item
286
+ *
287
+ * @return Item
266
288
*/
267
- private function _getQuoteItemIdByProductId ($ quote , $ productId )
289
+ private function getQuoteItemIdByProductId ($ quote , $ productId ): Item
268
290
{
269
- /** @var $quoteItems \Magento\Quote\Model\Quote\ Item[] */
291
+ /** @var $quoteItems Item[] */
270
292
$ quoteItems = $ quote ->getAllItems ();
293
+
271
294
foreach ($ quoteItems as $ quoteItem ) {
272
295
if ($ productId == $ quoteItem ->getProductId ()) {
273
296
return $ quoteItem ;
274
297
}
275
298
}
276
- $ this ->fail ('Test failed since no quoteItem found by productId ' .$ productId );
299
+
300
+ $ this ->fail ('Test failed since no quoteItem found by productId ' . $ productId );
277
301
}
278
302
}
0 commit comments