Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WebsiteAttributesSynchronizerTest.php
Go to the documentation of this file.
1 <?php
9 
15 
20 class WebsiteAttributesSynchronizerTest extends \PHPUnit\Framework\TestCase
21 {
22  const PRODUCT_ID = 333;
23  const PRODUCT_NOT_EDIT_MODE = false;
24  const FIRST_STORE_CODE = 'customstoreview1';
25  const SECOND_STORE_CODE = 'customstoreview2';
26  const PRODUCT_FORCE_RELOAD = true;
27 
31  private $objectManager;
32 
36  private $productRepository;
37 
41  private $storeRepository;
42 
46  public function setUp()
47  {
48  $this->objectManager = Bootstrap::getObjectManager();
49  $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
50  $this->storeRepository = $this->objectManager->get(StoreRepositoryInterface::class);
51  }
52 
57  public function testScheduleSynchronization()
58  {
59  $instance = $this->getWebsiteAttributesSynchronizer();
60  $this->assertFalse($instance->isSynchronizationRequired());
61  $instance->scheduleSynchronization();
62  $this->assertTrue($instance->isSynchronizationRequired());
63  }
64 
69  public function testSynchronizeSuccess()
70  {
71  $firstStore = $this->storeRepository->get(self::FIRST_STORE_CODE);
72  $secondStore = $this->storeRepository->get(self::SECOND_STORE_CODE);
73 
74  $firstStoreProduct = $this->productRepository->getById(
75  self::PRODUCT_ID,
76  self::PRODUCT_NOT_EDIT_MODE,
77  $firstStore->getId()
78  );
79 
80  $secondStoreProduct = $this->productRepository->getById(
81  self::PRODUCT_ID,
82  self::PRODUCT_NOT_EDIT_MODE,
83  $secondStore->getId()
84  );
85 
86  $instance = $this->getWebsiteAttributesSynchronizer();
87 
88  $this->assertNotEquals($firstStoreProduct->getStatus(), $secondStoreProduct->getStatus());
89  $this->assertEquals(AttributeStatus::STATUS_DISABLED, $firstStoreProduct->getStatus());
90  $this->assertEquals(AttributeStatus::STATUS_ENABLED, $secondStoreProduct->getStatus());
91  $this->assertTrue($instance->isSynchronizationRequired());
92 
93  $instance->synchronize();
94 
95  $firstStoreProductAfterSync = $this->productRepository->getById(
96  self::PRODUCT_ID,
97  self::PRODUCT_NOT_EDIT_MODE,
98  $firstStore->getId(),
100  );
101 
102  $secondStoreProductAfterSync = $this->productRepository->getById(
103  self::PRODUCT_ID,
104  self::PRODUCT_NOT_EDIT_MODE,
105  $secondStore->getId(),
107  );
108 
109  $this->assertEquals($firstStoreProductAfterSync->getStatus(), $secondStoreProductAfterSync->getStatus());
110  $this->assertFalse($instance->isSynchronizationRequired());
111  }
112 
116  private function getWebsiteAttributesSynchronizer()
117  {
118  return $this->objectManager->get(WebsiteAttributesSynchronizer::class);
119  }
120 }
$secondStore