Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
agranjeon committed May 2, 2019
1 parent e571464 commit 6a8b494
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 34 deletions.
25 changes: 13 additions & 12 deletions Console/Command/Fake.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ protected function configure()
[
new InputArgument(
self::CODE_ARGUMENT,
InputArgument::REQUIRED,
'Code of the fake data to generate (All to generate all fake data)'
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
'Code of the fake data to generate ("all" to generate all fake data)'
),
]
);
Expand Down Expand Up @@ -112,21 +112,22 @@ public function execute(InputInterface $input, OutputInterface $output)
return;
}

$code = $input->getArgument(self::CODE_ARGUMENT);
$requestedCodes = $input->getArgument(self::CODE_ARGUMENT);
$requestedCodes = array_filter(array_map('trim', $requestedCodes), 'strlen');

if ($code !== 'all') {
foreach ($requestedCodes as $code) {
if ($code === 'all') {
$fakers = $this->fakerProvider->getFakers();
foreach ($fakers as $code => $faker) {
$faker->generateFakeData($output);
}

return;
}
$faker = $this->fakerProvider->getFaker($code);
$faker->generateFakeData($output);

$io->success('Fake data has been successfully generated for ' . $code);

return;
}

$fakers = $this->fakerProvider->getFakers();
foreach ($fakers as $code => $faker) {
$faker->generateFakeData($output);
}
$io->success('Fake data has been successfully generated');
}
}
15 changes: 7 additions & 8 deletions Model/Faker/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,24 @@ protected function createCategory(int $parentId, int $depth): void
}

$category = $this->categoryFactory->create();
$category->setName(substr($this->faker->realText(20, 3), 0, -1));
$name = '';
while (strlen($name) < 1) {
$name = substr($this->faker->realText(20, 3), 0, -1);
}
$category->setName($name);
$category->setUrlKey($this->faker->uuid);
$category->setParentId($parentId);
$category->setIsActive($this->faker->boolean(85));
$category->setCustomAttributes(
[
'description' => $this->faker->realText(100, 5),
'meta_tile' => $this->faker->word,
'meta_title' => $this->faker->realText(20, 3),
'meta_keywords' => $this->faker->realText(50, 5),
'meta_description' => $this->faker->realText(100, 5),
]
);

try {
$categoryId = $this->categoryRepository->save($category)->getId();
} catch (\Exception $e) {
var_dump($category->debug());
var_dump($e->getMessage());die;
}
$categoryId = $this->categoryRepository->save($category)->getId();
$depth--;

if ($depth == 0) {
Expand Down
30 changes: 18 additions & 12 deletions Model/Faker/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,17 @@ public function generateFakeData(OutputInterface $output): void
);

for ($i = 0; $i < $numberOfItems; $i++) {
$product = $this->productFactory->create()->load($productIds[array_rand($productIds)]);
$quote->addProduct(
$product,
rand(1, 3) // qty
);
try {
$product = $this->productFactory->create()->load($productIds[array_rand($productIds)]);
$quote->addProduct(
$product,
rand(1, 3) // qty
);
} catch(\Exception $exception) {
}
}
if (count($quote->getItemsCollection()->getItems()) == 0) {
continue;
}

$quote->getBillingAddress()->importCustomerAddressData(reset($shippingAddress));
Expand All @@ -161,13 +167,13 @@ public function generateFakeData(OutputInterface $output): void

$quote->setPaymentMethod($paymentMethod);
$quote->setInventoryProcessed(false);
$quote->save();

$quote->getPayment()->importData(['method' => $paymentMethod]);

$quote->collectTotals()->save();

$this->quoteManagement->submit($quote);
try {
$quote->save();
$quote->getPayment()->importData(['method' => $paymentMethod]);
$quote->collectTotals()->save();
$this->quoteManagement->submit($quote);
} catch(\Exception $exception) {
}
}
$progressBar->advance();
}
Expand Down
12 changes: 10 additions & 2 deletions Model/Faker/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ public function generateFakeData(OutputInterface $output): void
$product = $this->productFactory->create();
$product->setSku(uniqid());
$product->setStatus($faker->boolean(90));
$product->setName(substr($faker->realText($faker->numberBetween(15, 25)), 0, -1));
$name = '';
while (strlen($name) < 1) {
$name = substr($faker->realText($faker->numberBetween(15, 25), 3), 0, -1);
}
$product->setName($name);
$product->setUrlKey($faker->uuid);
$product->setWebsiteIds($websiteIds);
$product->setTypeId('simple');
Expand All @@ -116,8 +120,12 @@ public function generateFakeData(OutputInterface $output): void
)
);

if (!is_array($productCategoryIds)) {
$productCategoryIds = [$productCategoryIds];
}

$productCategories = [];
foreach ((array)$productCategoryIds as $categoryId) {
foreach ($productCategoryIds as $categoryId) {
$productCategories[] = $categoryIds[$categoryId];
}

Expand Down

0 comments on commit 6a8b494

Please sign in to comment.