Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SW-613-exported-main-variant-config #233

Merged
merged 17 commits into from
Jan 17, 2022
Prev Previous commit
Next Next commit
wip
  • Loading branch information
zaifastafa committed Dec 21, 2021
commit 14c0a2f13c79f6ca51dea0bd3a1bc2c8af50a24d
8 changes: 2 additions & 6 deletions src/Export/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ProductService
{
public const CONTAINER_ID = 'fin_search.product_service';

/** @var Config|null */
/** @var Config */
private $config;
zaifastafa marked this conversation as resolved.
Show resolved Hide resolved

/** @var ContainerInterface */
Expand Down Expand Up @@ -63,10 +63,6 @@ public static function getInstance(
$productService->setSalesChannelContext($salesChannelContext);
}

if ($config && !$productService->getConfig()) {
$productService->setConfig($config);
}

return $productService;
}

Expand Down Expand Up @@ -360,7 +356,7 @@ private function getParentByMainProduct(ProductEntity $product): ProductEntity

private function getParentByCheapestVariant(ProductEntity $product): ProductEntity
{
$parent = null;
$parent = $product;
$cheapestPrice = 0;
$currencyId = $this->salesChannelContext->getSalesChannel()->getCurrencyId();
$children = $product->getChildren();
Expand Down
48 changes: 17 additions & 31 deletions src/Struct/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class Config extends Struct
private $initialized = false;

/** @var string */
private $filterPosition;
private $filterPosition = FilterPosition::TOP;

/** @var string */
private $mainVariant;
private $mainVariant = MainVariant::SHOPWARE_DEFAULT;

/** @var array */
private $crossSellingCategories = [];
Expand Down Expand Up @@ -126,6 +126,21 @@ public function isInitialized(): bool
return $this->initialized;
}

public function getFilterPosition(): string
{
return $this->filterPosition;
}

public function getMainVariant(): string
{
return $this->mainVariant;
}

public function getCrossSellingCategories(): array
{
return $this->crossSellingCategories;
}

/**
* @throws InvalidArgumentException
*/
Expand Down Expand Up @@ -179,11 +194,6 @@ public function initializeBySalesChannel(SalesChannelContext $salesChannelContex
$this->initialized = true;
}

public function getCrossSellingCategories(): array
{
return $this->crossSellingCategories;
}

/**
* @throws InvalidArgumentException
*/
Expand Down Expand Up @@ -236,28 +246,4 @@ private function getConfig(?string $salesChannelId, ?string $languageId, string

return $configValue;
}

/**
* @return string
*/
public function getFilterPosition(): string
{
return $this->filterPosition;
}

/**
* @return string
*/
public function getMainVariant(): string
{
return $this->mainVariant;
}

/**
* @param string $mainVariant
*/
public function setMainVariant(string $mainVariant): void
{
$this->mainVariant = $mainVariant;
}
}
37 changes: 28 additions & 9 deletions tests/Export/ProductServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,31 +433,31 @@ public function mainVariantDefaultConfigProvider(): array
'config' => 'default',
'parentId' => $parentId,
'parentPrice' => 15,
'cheapest' => 2
'cheapestPrice' => 2
],
'export main parent' => [
'config' => 'parent',
'parentId' => $parentId,
'parentPrice' => 15,
'cheapest' => 2
'cheapestPrice' => 2
],
'export cheapest variant' => [
zaifastafa marked this conversation as resolved.
Show resolved Hide resolved
'config' => 'parent',
'config' => 'cheapest',
'parentId' => $parentId,
'parentPrice' => 15,
'cheapest' => 2
'cheapestPrice' => 2
],
'export cheapest variant with parent price being cheaper' => [
'config' => 'parent',
'config' => 'cheapest',
'parentId' => $parentId,
'parentPrice' => 3,
'cheapest' => 5
],
'export cheapest variant with all same prices' => [
'config' => 'parent',
'config' => 'cheapest',
'parentId' => $parentId,
'parentPrice' => 4,
'cheapest' => 4
'cheapestPrice' => 4
],
];
}
Expand Down Expand Up @@ -606,7 +606,6 @@ public function testProductWithMultipleVariantsBasedOnExportConfig(
$result = $this->defaultProductService->searchVisibleProducts(20, 0);
$elements = $result->getElements();

$this->assertNotEmpty($elements);
$this->assertCount(1, $elements);
$mainVariant = current($elements);

Expand All @@ -622,6 +621,27 @@ public function testProductWithMultipleVariantsBasedOnExportConfig(
}
}

/**
* @dataProvider mainVariantDefaultConfigProvider
*/
public function testProductWithMultipleVariantsIfExportConfigIsCheapest(string $config, string $parentId): void
{
$this->createVisibleTestProduct(['id' => $parentId]);
$mockedConfig = $this->getFindologicConfig(['mainVariant' => $config]);
$mockedConfig->initializeBySalesChannel($this->salesChannelContext);

$this->defaultProductService->setConfig($mockedConfig);
$result = $this->defaultProductService->searchVisibleProducts(20, 0);
$elements = $result->getElements();

$this->assertCount(1, $elements);
$mainVariant = current($elements);

// If there are no variants, the main product will always be exported as the main variant, irrespective
// of the export configuration.
$this->assertSame($parentId, $mainVariant->getId());
}

/**
* @dataProvider mainVariantDefaultConfigProvider
*/
Expand All @@ -635,7 +655,6 @@ public function testProductWithoutVariantsBasedOnExportConfig(string $config, st
$result = $this->defaultProductService->searchVisibleProducts(20, 0);
$elements = $result->getElements();

$this->assertNotEmpty($elements);
$this->assertCount(1, $elements);
$mainVariant = current($elements);

Expand Down
1 change: 0 additions & 1 deletion tests/Traits/DataHelpers/ConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use FINDOLOGIC\FinSearch\Findologic\Resource\ServiceConfigResource;
use FINDOLOGIC\FinSearch\Struct\Config;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Defaults;

trait ConfigHelper
Expand Down