7
7
8
8
namespace Magento \Catalog \Model \Product ;
9
9
10
+ use Magento \Catalog \Api \Data \ProductInterface ;
10
11
use Magento \Catalog \Api \Data \ProductLinkInterface ;
11
12
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
13
use Magento \Catalog \Model \Product ;
13
14
use Magento \Catalog \Model \ProductLink \Link ;
14
- use Magento \Catalog \Model \ProductRepository ;
15
+ use Magento \Catalog \Model \ResourceModel \ Product as ProductResource ;
15
16
use Magento \TestFramework \Helper \Bootstrap ;
16
17
use Magento \TestFramework \ObjectManager ;
17
18
use PHPUnit \Framework \TestCase ;
@@ -61,12 +62,15 @@ class LinksTest extends TestCase
61
62
],
62
63
];
63
64
64
- /** @var ProductRepository $productRepository */
65
+ /** @var ProductRepositoryInterface $productRepository */
65
66
private $ productRepository ;
66
67
67
68
/** @var ObjectManager */
68
69
private $ objectManager ;
69
70
71
+ /** @var ProductResource */
72
+ private $ productResource ;
73
+
70
74
/**
71
75
* @inheritdoc
72
76
*/
@@ -75,6 +79,7 @@ protected function setUp()
75
79
parent ::setUp ();
76
80
$ this ->objectManager = Bootstrap::getObjectManager ();
77
81
$ this ->productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
82
+ $ this ->productResource = $ this ->objectManager ->create (ProductResource::class);
78
83
}
79
84
80
85
/**
@@ -90,24 +95,23 @@ protected function setUp()
90
95
*/
91
96
public function testEditRemoveRelatedUpSellCrossSellProducts (array $ data ): void
92
97
{
93
- /** @var Product $product */
98
+ /** @var ProductInterface| Product $product */
94
99
$ product = $ this ->productRepository ->get ('simple ' );
95
100
$ this ->setCustomProductLinks ($ product , $ this ->getProductData ($ data ['defaultLinks ' ]));
96
101
$ this ->productRepository ->save ($ product );
97
102
98
103
$ productData = $ this ->getProductData ($ data ['productLinks ' ]);
99
104
$ this ->setCustomProductLinks ($ product , $ productData );
100
- $ product -> save ();
105
+ $ this -> productResource -> save ($ product );
101
106
102
107
$ product = $ this ->productRepository ->get ('simple ' );
103
108
$ expectedLinks = isset ($ data ['expectedProductLinks ' ])
104
109
? $ this ->getProductData ($ data ['expectedProductLinks ' ])
105
110
: $ productData ;
106
- $ actualLinks = $ this ->getActualLinks ($ product );
107
111
108
112
$ this ->assertEquals (
109
113
$ expectedLinks ,
110
- $ actualLinks ,
114
+ $ this -> getActualLinks ( $ product ) ,
111
115
"Expected linked products do not match actual linked products! "
112
116
);
113
117
}
@@ -187,19 +191,20 @@ private function getProductData(array $productFixture): array
187
191
foreach ($ this ->linkTypes as $ linkType ) {
188
192
$ productData [$ linkType ] = [];
189
193
foreach ($ productFixture as $ data ) {
190
- $ productData [$ linkType ][$ data [ ' id ' ] ] = $ data ;
194
+ $ productData [$ linkType ][] = $ data ;
191
195
}
192
196
}
197
+
193
198
return $ productData ;
194
199
}
195
200
196
201
/**
197
202
* Link related, up-sells, cross-sells products received from the array
198
203
*
199
- * @param Product $product
204
+ * @param ProductInterface| Product $product
200
205
* @param array $productData
201
206
*/
202
- private function setCustomProductLinks (Product $ product , array $ productData ): void
207
+ private function setCustomProductLinks (ProductInterface $ product , array $ productData ): void
203
208
{
204
209
$ productLinks = [];
205
210
foreach ($ productData as $ linkType => $ links ) {
@@ -221,10 +226,10 @@ private function setCustomProductLinks(Product $product, array $productData): vo
221
226
/**
222
227
* Get an array of received related, up-sells, cross-sells products
223
228
*
224
- * @param Product $product
229
+ * @param ProductInterface| Product $product
225
230
* @return array
226
231
*/
227
- private function getActualLinks (Product $ product ): array
232
+ private function getActualLinks (ProductInterface $ product ): array
228
233
{
229
234
$ actualLinks = [];
230
235
foreach ($ this ->linkTypes as $ linkType ) {
@@ -241,9 +246,9 @@ private function getActualLinks(Product $product): array
241
246
$ products = $ product ->getRelatedProducts ();
242
247
break ;
243
248
}
244
- /** @var Product $product */
249
+ /** @var ProductInterface| Product $productItem */
245
250
foreach ($ products as $ productItem ) {
246
- $ actualLinks [$ linkType ][$ productItem -> getId () ] = [
251
+ $ actualLinks [$ linkType ][] = [
247
252
'id ' => $ productItem ->getId (),
248
253
'sku ' => $ productItem ->getSku (),
249
254
'position ' => $ productItem ->getPosition (),
0 commit comments