@@ -47,11 +47,13 @@ class SaveHandlerTest extends TestCase
47
47
*/
48
48
protected function setUp ()
49
49
{
50
- $ this ->productRepository = Bootstrap::getObjectManager ()->create (ProductRepositoryInterface::class);
51
- $ metadataPool = Bootstrap::getObjectManager ()->create (MetadataPool::class);
52
- $ this ->productLinkField = $ metadataPool ->getMetadata (ProductInterface::class)->getLinkField ();
53
- $ this ->categoryLinkFactory = Bootstrap::getObjectManager ()->create (CategoryLinkInterfaceFactory::class);
54
- $ this ->saveHandler = Bootstrap::getObjectManager ()->create (SaveHandler::class);
50
+ $ objectManager = Bootstrap::getObjectManager ();
51
+ $ this ->productRepository = $ objectManager ->create (ProductRepositoryInterface::class);
52
+ $ metadataPool = $ objectManager ->create (MetadataPool::class);
53
+ $ this ->productLinkField = $ metadataPool ->getMetadata (ProductInterface::class)
54
+ ->getLinkField ();
55
+ $ this ->categoryLinkFactory = $ objectManager ->create (CategoryLinkInterfaceFactory::class);
56
+ $ this ->saveHandler = $ objectManager ->create (SaveHandler::class);
55
57
}
56
58
57
59
/**
@@ -92,22 +94,14 @@ public function testExecute(): void
92
94
$ categoryLinks [] = $ categoryLink ;
93
95
}
94
96
$ categoryLinks = $ this ->updateCategoryLinks ($ product , $ categoryLinks );
95
- foreach ($ categoryLinks as $ categoryLink ) {
96
- $ categoryPosition = $ categoryPositions [$ categoryLink ->getCategoryId ()];
97
- $ this ->assertEquals ($ categoryPosition ['category_id ' ], $ categoryLink ->getCategoryId ());
98
- $ this ->assertEquals ($ categoryPosition ['position ' ], $ categoryLink ->getPosition ());
99
- }
97
+ $ this ->assertPositions ($ categoryPositions , $ categoryLinks );
100
98
101
99
$ categoryPositions [4 ]['position ' ] = 2 ;
102
100
$ categoryLink = $ this ->categoryLinkFactory ->create ()
103
101
->setCategoryId (4 )
104
102
->setPosition ($ categoryPositions [4 ]['position ' ]);
105
103
$ categoryLinks = $ this ->updateCategoryLinks ($ product , [$ categoryLink ]);
106
- foreach ($ categoryLinks as $ categoryLink ) {
107
- $ categoryPosition = $ categoryPositions [$ categoryLink ->getCategoryId ()];
108
- $ this ->assertEquals ($ categoryPosition ['category_id ' ], $ categoryLink ->getCategoryId ());
109
- $ this ->assertEquals ($ categoryPosition ['position ' ], $ categoryLink ->getPosition ());
110
- }
104
+ $ this ->assertPositions ($ categoryPositions , $ categoryLinks );
111
105
}
112
106
113
107
/**
@@ -123,8 +117,25 @@ private function updateCategoryLinks(ProductInterface $product, array $categoryL
123
117
$ arguments = [$ this ->productLinkField => $ product ->getData ($ this ->productLinkField )];
124
118
$ this ->saveHandler ->execute ($ product , $ arguments );
125
119
$ product = $ this ->productRepository ->get ($ product ->getSku (), false , null , true );
126
- $ categoryLinks = $ product ->getExtensionAttributes ()->getCategoryLinks ();
120
+ $ categoryLinks = $ product ->getExtensionAttributes ()
121
+ ->getCategoryLinks ();
127
122
128
123
return $ categoryLinks ;
129
124
}
125
+
126
+ /**
127
+ * Assert positions
128
+ *
129
+ * @param array $categoryPositions
130
+ * @param array $categoryLinks
131
+ * @return void
132
+ */
133
+ private function assertPositions (array $ categoryPositions , array $ categoryLinks ): void
134
+ {
135
+ foreach ($ categoryLinks as $ categoryLink ) {
136
+ $ categoryPosition = $ categoryPositions [$ categoryLink ->getCategoryId ()];
137
+ $ this ->assertEquals ($ categoryPosition ['category_id ' ], $ categoryLink ->getCategoryId ());
138
+ $ this ->assertEquals ($ categoryPosition ['position ' ], $ categoryLink ->getPosition ());
139
+ }
140
+ }
130
141
}
0 commit comments