Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductGeneratorTest.php
Go to the documentation of this file.
1 <?php
8 
14 
20 class ProductGeneratorTest extends \PHPUnit\Framework\TestCase
21 {
25  private $productGenerator;
26 
30  private $objectManager;
31 
35  private $productRepository;
36 
40  private $indexersState = [];
41 
45  private $websiteRepository;
46 
50  protected function setUp()
51  {
52  $this->objectManager = Bootstrap::getObjectManager();
53  $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
54  $this->productGenerator = $this->objectManager->get(ProductGenerator::class);
55  $this->websiteRepository = $this->objectManager->get(WebsiteRepository::class);
56  $indexerRegistry = $this->objectManager->get(IndexerRegistry::class);
57  $indexerListIds = $this->objectManager->get(Config::class)->getIndexers();
58 
59  foreach ($indexerListIds as $indexerId) {
60  $indexer = $indexerRegistry->get($indexerId['indexer_id']);
61  $this->indexersState[$indexerId['indexer_id']] = $indexer->isScheduled();
62  $indexer->setScheduled(true);
63  }
64  }
65 
69  protected function tearDown()
70  {
71  $indexerRegistry = $this->objectManager->get(IndexerRegistry::class);
72 
73  foreach ($this->indexersState as $indexerId => $state) {
74  $indexer = $indexerRegistry->get($indexerId);
75  $indexer->setScheduled($state);
76  }
77  }
78 
82  public function testProductGeneration()
83  {
84  $name = 'Simple Product Name';
85  $sku = 'simple_product_sku';
86  $price = 7.99;
87  $url = 'simple-product-url';
88  $categoryId = 333;
89  $secondWebsiteId = $this->websiteRepository->get('test')->getId();
90 
91  $fixtureMap = [
92  'name' => function () use ($name) {
93  return $name;
94  },
95  'sku' => function () use ($sku) {
96  return $sku;
97  },
98  'price' => function () use ($price) {
99  return $price;
100  },
101  'url_key' => function () use ($url) {
102  return $url;
103  },
104  'category_ids' => function () use ($categoryId) {
105  return $categoryId;
106  },
107  'website_ids' => function () use ($secondWebsiteId) {
108  return [1, $secondWebsiteId];
109  }
110  ];
111  $this->productGenerator->generate(1, $fixtureMap);
112 
113  $product = $this->productRepository->get($sku);
114 
115  $this->assertEquals($price, $product->getPrice());
116  $this->assertEquals($name, $product->getName());
117  $this->assertEquals($url, $product->getUrlKey());
118  $this->assertTrue(in_array($categoryId, $product->getCategoryIds()));
119 
120  $this->productRepository->delete($product);
121  }
122 }
$price
if(!isset($_GET['name'])) $name
Definition: log.php:14