Skip to content

Commit

Permalink
馃悰 Swap if and else case for fallback group key (#169)
Browse files Browse the repository at this point in the history
- add better unit test
- ...
  • Loading branch information
daniel-rose committed Dec 11, 2023
1 parent 877be5c commit c59a8e1
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function expandProductPageData(array $productData, ProductPageSearchTrans
if ($productImage['fk_product_image_set'] === $imageSet->getIdProductImageSet()) {
$key = $imageSet->getName();

$regrouped[$key === null || $key === '' ? $key : static::IMAGE_GROUP_NAME_EMPTY][] = $productImage;
$regrouped[$key === null || $key === '' ? static::IMAGE_GROUP_NAME_EMPTY : $key][] = $productImage;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ protected function _before(): void
*/
public function testExpandProductPageData(): void
{
$key = 'front';
$idProductImageSet = 44;

$productData = [
ProductPageSearchConfig::PRODUCT_ABSTRACT_PAGE_LOAD_DATA => $this->productPayloadTransferMock,
'fk_locale' => 1,
Expand All @@ -65,23 +68,92 @@ public function testExpandProductPageData(): void
];

$productImage = [
'fk_product_image_set' => 44,
'fk_product_image_set' => $idProductImageSet,
];

$this->productPayloadTransferMock->expects(static::atLeastOnce())->method('getImages')->willReturn($imageSets);
$this->spyProductImageSetMock->expects(static::atLeastOnce())->method('getIdProductImageSet')->willReturn(44);
$this->spyProductImageSetMock->expects(static::atLeastOnce())->method('getName')->willReturn('front');
$this->pageSearchTransferMock->expects(static::atLeastOnce())->method('setGroupedProductImages');
$this->pageSearchTransferMock->expects(static::atLeastOnce())->method('getProductImages')->willReturn([$productImage]);
$this->productPayloadTransferMock->expects(static::atLeastOnce())
->method('getImages')
->willReturn($imageSets);

$this->pageSearchTransferMock->expects(static::atLeastOnce())
->method('getProductImages')
->willReturn([$productImage]);

$this->spyProductImageSetMock->expects(static::atLeastOnce())
->method('getIdProductImageSet')
->willReturn($idProductImageSet);

$this->spyProductImageSetMock->expects(static::atLeastOnce())
->method('getName')
->willReturn($key);

$this->pageSearchTransferMock->expects(static::atLeastOnce())
->method('setGroupedProductImages')
->with(
static::callback(
static fn (array $groupedProductImages): bool => array_keys($groupedProductImages) == [$key]
),
)->willReturn($this->pageSearchTransferMock);

$this->plugin->expandProductPageData($productData, $this->pageSearchTransferMock);
}

/**
* @return void
*/
public function testExpandProductPageDataWithoutName(): void
public function testExpandProductPageDataWithEmptyImageSetName(): void
{
$key = '';
$idProductImageSet = 44;

$productData = [
ProductPageSearchConfig::PRODUCT_ABSTRACT_PAGE_LOAD_DATA => $this->productPayloadTransferMock,
'fk_locale' => 1,
];

$imageSets = [
1 => [$this->spyProductImageSetMock],
];

$productImage = [
'fk_product_image_set' => $idProductImageSet,
];

$this->productPayloadTransferMock->expects(static::atLeastOnce())
->method('getImages')
->willReturn($imageSets);

$this->pageSearchTransferMock->expects(static::atLeastOnce())
->method('getProductImages')
->willReturn([$productImage]);

$this->spyProductImageSetMock->expects(static::atLeastOnce())
->method('getIdProductImageSet')
->willReturn($idProductImageSet);

$this->spyProductImageSetMock->expects(static::atLeastOnce())
->method('getName')
->willReturn($key);

$this->pageSearchTransferMock->expects(static::atLeastOnce())
->method('setGroupedProductImages')
->with(
static::callback(
static fn (array $groupedProductImages): bool => array_keys($groupedProductImages) == ['*']
),
)->willReturn($this->pageSearchTransferMock);

$this->plugin->expandProductPageData($productData, $this->pageSearchTransferMock);
}

/**
* @return void
*/
public function testExpandProductPageDataWithNullableImageSetName(): void
{
$key = null;
$idProductImageSet = 44;

$productData = [
ProductPageSearchConfig::PRODUCT_ABSTRACT_PAGE_LOAD_DATA => $this->productPayloadTransferMock,
'fk_locale' => 1,
Expand All @@ -92,14 +164,32 @@ public function testExpandProductPageDataWithoutName(): void
];

$productImage = [
'fk_product_image_set' => 44,
'fk_product_image_set' => $idProductImageSet,
];

$this->productPayloadTransferMock->expects(static::atLeastOnce())->method('getImages')->willReturn($imageSets);
$this->spyProductImageSetMock->expects(static::atLeastOnce())->method('getIdProductImageSet')->willReturn(44);
$this->spyProductImageSetMock->expects(static::atLeastOnce())->method('getName')->willReturn('');
$this->pageSearchTransferMock->expects(static::atLeastOnce())->method('setGroupedProductImages');
$this->pageSearchTransferMock->expects(static::atLeastOnce())->method('getProductImages')->willReturn([$productImage]);
$this->productPayloadTransferMock->expects(static::atLeastOnce())
->method('getImages')
->willReturn($imageSets);

$this->pageSearchTransferMock->expects(static::atLeastOnce())
->method('getProductImages')
->willReturn([$productImage]);

$this->spyProductImageSetMock->expects(static::atLeastOnce())
->method('getIdProductImageSet')
->willReturn($idProductImageSet);

$this->spyProductImageSetMock->expects(static::atLeastOnce())
->method('getName')
->willReturn($key);

$this->pageSearchTransferMock->expects(static::atLeastOnce())
->method('setGroupedProductImages')
->with(
static::callback(
static fn (array $groupedProductImages): bool => array_keys($groupedProductImages) == ['*']
),
)->willReturn($this->pageSearchTransferMock);

$this->plugin->expandProductPageData($productData, $this->pageSearchTransferMock);
}
Expand Down

0 comments on commit c59a8e1

Please sign in to comment.