Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductAttributesCleanUpTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Symfony\Component\Console\Tester\CommandTester;
9 
10 class ProductAttributesCleanUpTest extends \PHPUnit\Framework\TestCase
11 {
15  private $tester;
16 
20  private $command;
21 
25  private $objectManager;
26 
30  private $attributeResource;
31 
32  public function setUp()
33  {
35  $this->command = $this->objectManager->create(\Magento\Catalog\Console\Command\ProductAttributesCleanUp::class);
36  $this->attributeResource = $this->objectManager->create(\Magento\Catalog\Model\ResourceModel\Attribute::class);
37  $this->tester = new CommandTester($this->command);
38 
39  // Prepare data fixtures for test
40  $store = $this->prepareAdditionalStore();
42  $productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
43  $product = $productRepository->get('simple');
44  $product->setName('Simple fixture store');
45  $product->setStoreId($store->getId());
46  $product->save();
47  }
48 
55  public function testExecute()
56  {
57  // Verify that unused attribute was created
58  $attribute = $this->getUnusedProductAttribute();
59 
60  $this->assertArrayHasKey('value', $attribute);
61  $this->assertArrayHasKey('value_id', $attribute);
62  $this->assertEquals($attribute['value'], 'Simple fixture store');
63 
64  // Execute command
65  $this->tester->execute([]);
66 
67  // Verify that unused attribute was removed
68  $this->assertContains('Unused product attributes successfully cleaned up', $this->tester->getDisplay());
69  $attribute = $this->getUnusedProductAttribute();
70  $this->assertFalse($attribute);
71  }
72 
76  private function getUnusedProductAttribute()
77  {
78  $connection = $this->attributeResource->getConnection();
79  $select = $connection->select();
80  $select->from($this->attributeResource->getTable('catalog_product_entity_varchar'));
81  $select->where('value = ?', 'Simple fixture store');
82  return $connection->fetchRow($select);
83  }
84 
88  private function prepareAdditionalStore()
89  {
91  $website = $this->objectManager->create(\Magento\Store\Model\Website::class);
92  $website->load('test');
93 
95  $store = $this->objectManager->create(\Magento\Store\Model\Store::class);
96  $store->load('fixturestore');
97 
99  $storeGroup = $this->objectManager->create(\Magento\Store\Model\Group::class);
100  $storeGroup->setWebsiteId($website->getId());
101  $storeGroup->setName('Fixture Store Group');
102  $storeGroup->setRootCategoryId(2);
103  $storeGroup->setDefaultStoreId($store->getId());
104  $storeGroup->save();
105 
106  $store->setWebsiteId($website->getId())
107  ->setGroupId($storeGroup->getId())
108  ->save();
109 
110  return $store;
111  }
112 }
$connection
Definition: bulk.php:13