Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions spec/DataGenerator/Factory/Entity/ProductFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class ProductFactorySpec extends ObjectBehavior
Expand All @@ -36,6 +37,7 @@ public function it_creates_product(
ProductVariantInterface $variant,
ChannelInterface $channel,
ProductInterface $product,
TaxonInterface $taxon,
): void {
$productFactory->createNew()->willReturn($product);

Expand All @@ -48,6 +50,7 @@ public function it_creates_product(
$variant,
$channel,
new DateTime(),
$taxon,
)
->shouldReturn($product);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function it_generates_product_taxon_collection(
$offset = 0;
$taxons = [$taxon1, $taxon2];

$context->getQuantity()->willReturn(100);
$context->getIO()->willReturn($io);
$taxonRepository->getEntityCount()->willReturn($entityCount);

Expand All @@ -70,6 +71,7 @@ public function it_does_nothing_if_no_taxons_found(
$limit = 100;
$offset = 0;

$context->getQuantity()->willReturn(100);
$context->getIO()->willReturn($io);
$taxonRepository->getEntityCount()->willReturn($entityCount);

Expand All @@ -78,6 +80,13 @@ public function it_does_nothing_if_no_taxons_found(
$this->generate($context);
}

public function it_does_nothing_if_quantity_equals_to_zero(ProductTaxonGeneratorContextInterface $context,): void
{
$context->getQuantity()->willReturn(0)->shouldBeCalled();

$this->generate($context);
}

public function it_throws_exception_on_invalid_context(ContextInterface $context): void
{
$this->shouldThrow(InvalidContextException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function it_generates_wishlist_product_collection(
$offset = 0;
$wishlists = [$wishlist1, $wishlist2];

$context->getQuantity()->willReturn(100);
$context->getIO()->willReturn($io);
$context->getChannel()->willReturn($channel);
$wishlistRepository->getEntityCount($channel)->willReturn($entityCount);
Expand All @@ -74,6 +75,7 @@ public function it_does_nothing_if_no_wishlists_found(
$limit = 100;
$offset = 0;

$context->getQuantity()->willReturn(100);
$context->getIO()->willReturn($io);
$context->getChannel()->willReturn($channel);
$wishlistRepository->getEntityCount($channel)->willReturn($entityCount);
Expand All @@ -83,6 +85,13 @@ public function it_does_nothing_if_no_wishlists_found(
$this->generate($context);
}

public function it_does_nothing_if_quantity_equals_to_zero(WishlistProductGeneratorContextInterface $context): void
{
$context->getQuantity()->willReturn(0)->shouldBeCalled();

$this->generate($context);
}

public function it_throws_exception_on_invalid_context(ContextInterface $context): void
{
$this->shouldThrow(InvalidContextException::class)
Expand Down
17 changes: 12 additions & 5 deletions spec/DataGenerator/Generator/Entity/ProductGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\GeneratorContextInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\ProductGeneratorContextInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\Doctrine\Repository\TaxonRepositoryInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\Exception\InvalidContextException;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\Factory\Entity\ChannelPricingFactoryInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\Factory\Entity\ProductFactoryInterface;
Expand All @@ -24,15 +25,17 @@
use Sylius\Component\Core\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Model\TaxonInterface;

final class ProductGeneratorSpec extends ObjectBehavior
{
public function let(
ProductFactoryInterface $productFactory,
ProductVariantFactoryInterface $productVariantFactory,
ChannelPricingFactoryInterface $channelPricingFactory,
TaxonRepositoryInterface $taxonRepository,
): void {
$this->beConstructedWith($productFactory, $productVariantFactory, $channelPricingFactory);
$this->beConstructedWith($productFactory, $productVariantFactory, $channelPricingFactory, $taxonRepository);
}

public function it_is_initializable(): void
Expand All @@ -49,6 +52,8 @@ public function it_generates_product(
ProductVariantInterface $productVariant,
ProductGeneratorContextInterface $context,
ProductInterface $product,
TaxonRepositoryInterface $taxonRepository,
TaxonInterface $taxon,
): void {
$context->getChannel()->willReturn($channel);
$channel->getCode()->willReturn(Argument::type('string'));
Expand All @@ -61,21 +66,23 @@ public function it_generates_product(
->create(
Argument::type('string'),
Argument::type('string'),
$channelPricing->getWrappedObject()
$channelPricing
)
->willReturn($productVariant);

$context->getChannel()->willReturn($channel->getWrappedObject());
$context->getChannel()->willReturn($channel);
$taxonRepository->getRandomTaxon()->willReturn($taxon);

$productFactory
->create(
Argument::type('string'),
Argument::type('string'),
Argument::type('string'),
Argument::type('string'),
$productVariant->getWrappedObject(),
$channel->getWrappedObject(),
$productVariant,
$channel,
Argument::type(DateTime::class),
$taxon,
)
->willReturn($product);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface BulkDataGeneratorInterface
{
const DEFAULT_TAXONS_QTY = 5000;

const DEFAULT_MAX_TAXON_LEVEL = 20;
const DEFAULT_MAX_TAXON_LEVEL = 14;

const DEFAULT_MAX_CHILDREN_PER_TAXON_LEVEL = 5;

Expand Down
20 changes: 20 additions & 0 deletions src/DataGenerator/Doctrine/Repository/TaxonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace BitBag\SyliusVueStorefront2Plugin\DataGenerator\Doctrine\Repository;

use BitBag\SyliusVueStorefront2Plugin\DataGenerator\Exception\NoTaxonFoundException;
use Sylius\Bundle\TaxonomyBundle\Doctrine\ORM\TaxonRepository as BaseTaxonRepository;
use Sylius\Component\Core\Model\TaxonInterface;

Expand Down Expand Up @@ -63,8 +64,27 @@ public function findBatch(
public function getEntityCount(): int
{
$queryBuilder = $this->createQueryBuilder('taxon')
->andWhere('taxon.enabled = true')
->select('COUNT(taxon)');

return (int)$queryBuilder->getQuery()->getSingleScalarResult();
}

public function getRandomTaxon(): TaxonInterface
{
$randomOffset = max(0, rand(0, $this->getEntityCount() - 1));

$result = $this->createQueryBuilder('taxon')
->where('taxon.enabled = true')
->setFirstResult($randomOffset)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();

if ($result instanceof TaxonInterface) {
return $result;
}

throw new NoTaxonFoundException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ public function findBatch(
): array;

public function getEntityCount(): int;

public function getRandomTaxon(): TaxonInterface;
}
17 changes: 17 additions & 0 deletions src/DataGenerator/Exception/NoTaxonFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusVueStorefront2Plugin\DataGenerator\Exception;

use ApiPlatform\Core\Exception\RuntimeException;

final class NoTaxonFoundException extends RuntimeException
{
}
12 changes: 8 additions & 4 deletions src/DataGenerator/Factory/Context/GeneratorContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\DataGeneratorCommandContextInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\GeneratorContextInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\ProductGeneratorContext;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\ProductGeneratorContextInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\ProductTaxonGeneratorContext;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\ProductTaxonGeneratorContextInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\TaxonGeneratorContext;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\TaxonGeneratorContextInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\WishlistGeneratorContext;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\WishlistProductGeneratorContext;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\WishlistProductGeneratorContextInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\Exception\UnknownBulkDataGeneratorException;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\Generator\Bulk\BulkGeneratorInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\Generator\Bulk\Collection\ProductTaxonCollectionBulkGeneratorInterface;
Expand Down Expand Up @@ -53,7 +57,7 @@ public function fromCommandContext(

private function productGeneratorContext(
DataGeneratorCommandContextInterface $commandContext,
): ProductGeneratorContext {
): ProductGeneratorContextInterface {
return new ProductGeneratorContext(
$commandContext->getIO(),
$commandContext->getProductsQty(),
Expand All @@ -63,7 +67,7 @@ private function productGeneratorContext(

private function taxonGeneratorContext(
DataGeneratorCommandContextInterface $commandContext,
): TaxonGeneratorContext {
): TaxonGeneratorContextInterface {
return new TaxonGeneratorContext(
$commandContext->getIO(),
$commandContext->getTaxonsQty(),
Expand All @@ -84,7 +88,7 @@ private function wishlistGeneratorContext(

private function productTaxonGeneratorContext(
DataGeneratorCommandContextInterface $commandContext,
): ProductTaxonGeneratorContext {
): ProductTaxonGeneratorContextInterface {
return new ProductTaxonGeneratorContext(
$commandContext->getIO(),
$commandContext->getProductsPerTaxonQty(),
Expand All @@ -95,7 +99,7 @@ private function productTaxonGeneratorContext(

private function wishlistProductGeneratorContext(
DataGeneratorCommandContextInterface $commandContext,
): WishlistProductGeneratorContext {
): WishlistProductGeneratorContextInterface {
return new WishlistProductGeneratorContext(
$commandContext->getIO(),
$commandContext->getProductsPerWishlistQty(),
Expand Down
3 changes: 3 additions & 0 deletions src/DataGenerator/Factory/Entity/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class ProductFactory implements ProductFactoryInterface
Expand All @@ -34,6 +35,7 @@ public function create(
ProductVariantInterface $variant,
ChannelInterface $channel,
DateTimeInterface $createdAt,
TaxonInterface $mainTaxon,
): ProductInterface {
/** @var ProductInterface $product */
$product = $this->productFactory->createNew();
Expand All @@ -46,6 +48,7 @@ public function create(
$product->setCreatedAt($createdAt);
$product->addChannel($channel);
$product->addVariant($variant);
$product->setMainTaxon($mainTaxon);

return $product;
}
Expand Down
2 changes: 2 additions & 0 deletions src/DataGenerator/Factory/Entity/ProductFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Model\TaxonInterface;

interface ProductFactoryInterface
{
Expand All @@ -25,5 +26,6 @@ public function create(
ProductVariantInterface $variant,
ChannelInterface $channel,
DateTimeInterface $createdAt,
TaxonInterface $mainTaxon,
): ProductInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function generate(ContextInterface $context): void
throw new InvalidContextException();
}

if ($context->getQuantity() === 0) {
return;
}

$io = $context->getIO();

$io->info(sprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function generate(ContextInterface $context): void
throw new InvalidContextException();
}

if ($context->getQuantity() === 0) {
return;
}

$io = $context->getIO();

$io->info(sprintf(
Expand Down
13 changes: 10 additions & 3 deletions src/DataGenerator/Generator/Entity/ProductGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\GeneratorContextInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\ContextModel\Generator\ProductGeneratorContextInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\Doctrine\Repository\TaxonRepositoryInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\Exception\InvalidContextException;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\Factory\Entity\ChannelPricingFactoryInterface;
use BitBag\SyliusVueStorefront2Plugin\DataGenerator\Factory\Entity\ProductFactoryInterface;
Expand All @@ -28,16 +29,20 @@ final class ProductGenerator implements GeneratorInterface

private ChannelPricingFactoryInterface $channelPricingFactory;

private TaxonRepositoryInterface $taxonRepository;

private Generator $faker;

public function __construct(
ProductFactoryInterface $productFactory,
ProductVariantFactoryInterface $productVariantFactory,
ChannelPricingFactoryInterface $channelPricingFactory,
TaxonRepositoryInterface $taxonRepository,
) {
$this->productFactory = $productFactory;
$this->productVariantFactory = $productVariantFactory;
$this->channelPricingFactory = $channelPricingFactory;
$this->taxonRepository = $taxonRepository;
$this->faker = Factory::create();
}

Expand All @@ -47,9 +52,10 @@ public function generate(GeneratorContextInterface $context): ProductInterface
throw new InvalidContextException();
}

$channel = $context->getChannel();
$channelPricing = $this->channelPricingFactory->create(
$this->faker->randomNumber(),
$context->getChannel(),
$channel,
);

$uuid = $this->faker->uuid;
Expand All @@ -65,8 +71,9 @@ public function generate(GeneratorContextInterface $context): ProductInterface
$this->faker->sentence(15),
$this->faker->sentence(),
$variant,
$context->getChannel(),
$this->faker->dateTimeBetween('-1 year')
$channel,
$this->faker->dateTimeBetween('-1 year'),
$this->taxonRepository->getRandomTaxon(),
);
}
}
2 changes: 1 addition & 1 deletion src/DataGenerator/Generator/Entity/TaxonGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function generate(GeneratorContextInterface $context): TaxonInterface
}

$translation = TaxonTranslationFactory::create(
$this->faker->sentence(3),
sprintf('%s %s', $this->faker->uuid, $this->faker->sentence(3)),
$context::DEFAULT_LOCALE,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public function generateBiased(
return $this->rand->rand($topValuesThreshold, $max);
}

return $this->rand->rand($min, $topValuesThreshold - 1);
return $this->rand->rand($min, max(0, $topValuesThreshold - 1));
}
}
Loading