99
1010use Magento \Catalog \Api \Data \ProductInterface ;
1111use Magento \Catalog \Api \ProductRepositoryInterface ;
12- use Magento \Catalog \Model \ResourceModel \Product \WebsiteFactory ;
12+ use Magento \Catalog \Model \ResourceModel \Product \Website \ Link ;
1313use Magento \Framework \Exception \NoSuchEntityException ;
1414use Magento \Framework \ObjectManagerInterface ;
1515use Magento \Store \Api \WebsiteRepositoryInterface ;
@@ -26,8 +26,8 @@ class UpdateProductWebsiteTest extends TestCase
2626 /** @var ObjectManagerInterface */
2727 private $ objectManager ;
2828
29- /** @var WebsiteFactory */
30- private $ websiteProductsResourceFactory ;
29+ /** @var Link */
30+ private $ productWebsiteLink ;
3131
3232 /** @var WebsiteRepositoryInterface */
3333 private $ websiteRepository ;
@@ -43,7 +43,7 @@ protected function setUp()
4343 parent ::setUp ();
4444
4545 $ this ->objectManager = Bootstrap::getObjectManager ();
46- $ this ->websiteProductsResourceFactory = $ this ->objectManager ->get (WebsiteFactory ::class);
46+ $ this ->productWebsiteLink = $ this ->objectManager ->get (Link ::class);
4747 $ this ->websiteRepository = $ this ->objectManager ->get (WebsiteRepositoryInterface::class);
4848 $ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
4949 }
@@ -58,7 +58,10 @@ public function testAssignProductToWebsite(): void
5858 $ defaultWebsiteId = $ this ->websiteRepository ->get ('base ' )->getId ();
5959 $ secondWebsiteId = $ this ->websiteRepository ->get ('test ' )->getId ();
6060 $ product = $ this ->updateProductWebsites ('simple2 ' , [$ defaultWebsiteId , $ secondWebsiteId ]);
61- $ this ->assertProductWebsites ((int )$ product ->getId (), [$ defaultWebsiteId , $ secondWebsiteId ]);
61+ $ this ->assertEquals (
62+ [$ defaultWebsiteId , $ secondWebsiteId ],
63+ $ this ->productWebsiteLink ->getWebsiteIdsByProductId ($ product ->getId ())
64+ );
6265 }
6366
6467 /**
@@ -68,11 +71,9 @@ public function testAssignProductToWebsite(): void
6871 */
6972 public function testUnassignProductFromWebsite (): void
7073 {
71- $ product = $ this ->productRepository ->get ('simple-on-two-websites ' );
7274 $ secondWebsiteId = $ this ->websiteRepository ->get ('test ' )->getId ();
73- $ product ->setWebsiteIds ([$ secondWebsiteId ]);
74- $ product = $ this ->productRepository ->save ($ product );
75- $ this ->assertProductWebsites ((int )$ product ->getId (), [$ secondWebsiteId ]);
75+ $ product = $ this ->updateProductWebsites ('simple-on-two-websites ' , [$ secondWebsiteId ]);
76+ $ this ->assertEquals ([$ secondWebsiteId ], $ this ->productWebsiteLink ->getWebsiteIdsByProductId ($ product ->getId ()));
7677 }
7778
7879 /**
@@ -102,17 +103,4 @@ private function updateProductWebsites(string $productSku, array $websiteIds): P
102103
103104 return $ this ->productRepository ->save ($ product );
104105 }
105-
106- /**
107- * Assert that websites attribute was correctly saved
108- *
109- * @param int $productId
110- * @param array $expectedData
111- * @return void
112- */
113- private function assertProductWebsites (int $ productId , array $ expectedData ): void
114- {
115- $ websiteResource = $ this ->websiteProductsResourceFactory ->create ();
116- $ this ->assertEquals ($ expectedData , $ websiteResource ->getWebsites ([$ productId ])[$ productId ]);
117- }
118106}
0 commit comments