Skip to content

Commit

Permalink
CC-22522: Publisher trigger plugins (#9813)
Browse files Browse the repository at this point in the history
CC-22522 PublisherTriggerPluginInterface missed in part of P&S modules - Part 2
  • Loading branch information
artem-png authored Jan 16, 2023
1 parent d06f960 commit 0afd11d
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"spryker/product-category": "^4.0.0",
"spryker/product-list-extension": "^1.1.0",
"spryker/propel-orm": "^1.0.0",
"spryker/transfer": "^3.27.0",
"spryker/util-text": "^1.0.0"
},
"require-dev": {
Expand Down
5 changes: 5 additions & 0 deletions dependency.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"include": {
"spryker/transfer": "Provides transfer objects definition with strict types and `::get*OrFail()` functionality."
}
}
15 changes: 15 additions & 0 deletions src/Spryker/Shared/ProductList/Transfer/product_list.transfer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,19 @@
<property name="whitelistIds" type="int[]" singular="whitelistId"/>
</transfer>

<transfer name="ProductListCollection">
<property name="productLists" type="ProductList[]" singular="productList"/>
<property name="pagination" type="Pagination" strict="true"/>
</transfer>

<transfer name="ProductListCriteria" strict="true">
<property name="pagination" type="Pagination"/>
</transfer>

<transfer name="Pagination">
<property name="offset" type="int"/>
<property name="limit" type="int"/>
<property name="nbResults" type="int"/>
</transfer>

</transfers>
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,11 @@ public function getProductListById(ProductListTransfer $productListTransfer): Pr
public function getProductAbstractIdsByProductListIds(array $productListIds): array
{
return array_unique(
array_merge(
$this->productListRepository->getProductAbstractIdsRelatedToProductConcrete($productListIds),
$this->productListRepository->getProductAbstractIdsRelatedToCategories($productListIds),
array_filter(
array_merge(
$this->productListRepository->getProductAbstractIdsRelatedToProductConcrete($productListIds),
$this->productListRepository->getProductAbstractIdsRelatedToCategories($productListIds),
),
),
);
}
Expand Down
16 changes: 16 additions & 0 deletions src/Spryker/Zed/ProductList/Business/ProductListFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use Generated\Shared\Transfer\CartChangeTransfer;
use Generated\Shared\Transfer\CartPreCheckResponseTransfer;
use Generated\Shared\Transfer\ProductListCollectionTransfer;
use Generated\Shared\Transfer\ProductListCriteriaTransfer;
use Generated\Shared\Transfer\ProductListResponseTransfer;
use Generated\Shared\Transfer\ProductListTransfer;
use Generated\Shared\Transfer\QuoteTransfer;
Expand Down Expand Up @@ -356,4 +358,18 @@ public function getProductConcreteIdsByProductListIds(array $productListIds): ar
->createProductListReader()
->getProductConcreteIdsByProductListIds($productListIds);
}

/**
* {@inheritDoc}
*
* @api
*
* @param \Generated\Shared\Transfer\ProductListCriteriaTransfer $productListCriteriaTransfer
*
* @return \Generated\Shared\Transfer\ProductListCollectionTransfer
*/
public function getProductListCollection(ProductListCriteriaTransfer $productListCriteriaTransfer): ProductListCollectionTransfer
{
return $this->getRepository()->getProductListCollection($productListCriteriaTransfer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use Generated\Shared\Transfer\CartChangeTransfer;
use Generated\Shared\Transfer\CartPreCheckResponseTransfer;
use Generated\Shared\Transfer\ProductListCollectionTransfer;
use Generated\Shared\Transfer\ProductListCriteriaTransfer;
use Generated\Shared\Transfer\ProductListResponseTransfer;
use Generated\Shared\Transfer\ProductListTransfer;
use Generated\Shared\Transfer\QuoteTransfer;
Expand Down Expand Up @@ -311,4 +313,18 @@ public function filterRestrictedItems(QuoteTransfer $quoteTransfer): QuoteTransf
* @return array<int>
*/
public function getProductConcreteIdsByProductListIds(array $productListIds): array;

/**
* Specification:
* - Fetches a collection of product lists from the Persistence.
* - Uses `ProductListCriteriaTransfer.pagination.limit` and `ProductListCriteriaTransfer.pagination.offset` to paginate results with limit and offset.
* - Returns `ProductListCollectionTransfer` filled with found product lists.
*
* @api
*
* @param \Generated\Shared\Transfer\ProductListCriteriaTransfer $productListCriteriaTransfer
*
* @return \Generated\Shared\Transfer\ProductListCollectionTransfer
*/
public function getProductListCollection(ProductListCriteriaTransfer $productListCriteriaTransfer): ProductListCollectionTransfer;
}
2 changes: 2 additions & 0 deletions src/Spryker/Zed/ProductList/Dependency/ProductListEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ interface ProductListEvents
*
* @api
*
* @deprecated Use {@link \Spryker\Shared\ProductListStorage\ProductListStorageConfig::PRODUCT_LIST_PUBLISH} instead.
*
* @var string
*/
public const PRODUCT_LIST_PUBLISH = 'ProductList.spy_product_list.publish';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

namespace Spryker\Zed\ProductList\Persistence\Mapper;

use Generated\Shared\Transfer\ProductListCollectionTransfer;
use Generated\Shared\Transfer\ProductListTransfer;
use Generated\Shared\Transfer\SpyProductListEntityTransfer;
use Orm\Zed\ProductList\Persistence\SpyProductList;
use Propel\Runtime\Collection\ObjectCollection;

class ProductListMapper
{
Expand Down Expand Up @@ -66,4 +68,23 @@ public function mapProductListTransferToEntity(

return $spyProductListEntity;
}

/**
* @param \Propel\Runtime\Collection\ObjectCollection<\Orm\Zed\ProductList\Persistence\SpyProductList> $productListEntities
* @param \Generated\Shared\Transfer\ProductListCollectionTransfer $productListCollectionTransfer
*
* @return \Generated\Shared\Transfer\ProductListCollectionTransfer
*/
public function mapProductListEntitiesToProductListCollectionTransfer(
ObjectCollection $productListEntities,
ProductListCollectionTransfer $productListCollectionTransfer
): ProductListCollectionTransfer {
foreach ($productListEntities as $productListEntity) {
$productListCollectionTransfer->addProductList(
$this->mapEntityToProductListTransfer($productListEntity, new ProductListTransfer()),
);
}

return $productListCollectionTransfer;
}
}
49 changes: 49 additions & 0 deletions src/Spryker/Zed/ProductList/Persistence/ProductListRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

namespace Spryker\Zed\ProductList\Persistence;

use Generated\Shared\Transfer\PaginationTransfer;
use Generated\Shared\Transfer\ProductListCollectionTransfer;
use Generated\Shared\Transfer\ProductListCriteriaTransfer;
use Generated\Shared\Transfer\ProductListTransfer;
use Orm\Zed\Product\Persistence\Map\SpyProductTableMap;
use Orm\Zed\Product\Persistence\SpyProductQuery;
use Orm\Zed\ProductCategory\Persistence\Map\SpyProductCategoryTableMap;
use Orm\Zed\ProductList\Persistence\Map\SpyProductListCategoryTableMap;
use Orm\Zed\ProductList\Persistence\Map\SpyProductListProductConcreteTableMap;
use Orm\Zed\ProductList\Persistence\Map\SpyProductListTableMap;
use Orm\Zed\ProductList\Persistence\SpyProductListQuery;
use Propel\Runtime\ActiveQuery\Join;
use Propel\Runtime\Formatter\SimpleArrayFormatter;
use Spryker\Zed\Kernel\Persistence\AbstractRepository;
Expand Down Expand Up @@ -618,4 +622,49 @@ public function getProductConcreteIdsRelatedToProductListsCategories(array $prod
->find()
->toArray();
}

/**
* @param \Generated\Shared\Transfer\ProductListCriteriaTransfer $productListCriteriaTransfer
*
* @return \Generated\Shared\Transfer\ProductListCollectionTransfer
*/
public function getProductListCollection(ProductListCriteriaTransfer $productListCriteriaTransfer): ProductListCollectionTransfer
{
$productListCollectionTransfer = new ProductListCollectionTransfer();
$productListQuery = $this->getFactory()->createProductListQuery();

$paginationTransfer = $productListCriteriaTransfer->getPagination();
if ($paginationTransfer) {
$productListQuery = $this->applyProductListPagination($productListQuery, $paginationTransfer);
$productListCollectionTransfer->setPagination($paginationTransfer);
}

return $this->getFactory()
->createProductListMapper()
->mapProductListEntitiesToProductListCollectionTransfer(
$productListQuery->find(),
$productListCollectionTransfer,
);
}

/**
* @param \Orm\Zed\ProductList\Persistence\SpyProductListQuery $productListQuery
* @param \Generated\Shared\Transfer\PaginationTransfer $paginationTransfer
*
* @return \Orm\Zed\ProductList\Persistence\SpyProductListQuery
*/
protected function applyProductListPagination(
SpyProductListQuery $productListQuery,
PaginationTransfer $paginationTransfer
): SpyProductListQuery {
$paginationTransfer->setNbResults($productListQuery->count());

if ($paginationTransfer->getLimit() !== null && $paginationTransfer->getOffset() !== null) {
return $productListQuery
->limit($paginationTransfer->getLimit())
->offset($paginationTransfer->getOffset());
}

return $productListQuery;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Spryker\Zed\ProductList\Persistence;

use Generated\Shared\Transfer\ProductListCollectionTransfer;
use Generated\Shared\Transfer\ProductListCriteriaTransfer;
use Generated\Shared\Transfer\ProductListTransfer;

interface ProductListRepositoryInterface
Expand Down Expand Up @@ -149,4 +151,11 @@ public function getProductWhiteListsByProductAbstractIds(array $productAbstractI
* @return array<int>
*/
public function getProductConcreteIdsRelatedToProductListsCategories(array $productListIds): array;

/**
* @param \Generated\Shared\Transfer\ProductListCriteriaTransfer $productListCriteriaTransfer
*
* @return \Generated\Shared\Transfer\ProductListCollectionTransfer
*/
public function getProductListCollection(ProductListCriteriaTransfer $productListCriteriaTransfer): ProductListCollectionTransfer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

use Codeception\Test\Unit;
use Generated\Shared\DataBuilder\ProductListBuilder;
use Generated\Shared\Transfer\PaginationTransfer;
use Generated\Shared\Transfer\ProductListCriteriaTransfer;
use Orm\Zed\ProductList\Persistence\SpyProductListQuery;
use Propel\Runtime\Exception\PropelException;

/**
Expand Down Expand Up @@ -239,6 +242,88 @@ public function testGetProductConcreteIdsByProductListIds(): void
$this->assertEquals([$productTransfer->getIdProductConcrete()], $productConcreteIds);
}

/**
* @return void
*/
public function testGetProductListCollectionReturnsCorrectProductListsWithoutPagination(): void
{
// Arrange
$this->tester->ensureDatabaseTableIsEmpty(SpyProductListQuery::create());
$productListTransfer1 = $this->tester->haveProductList();
$productListTransfer2 = $this->tester->haveProductList();
$productListTransfer3 = $this->tester->haveProductList();
$productListCriteriaTransfer = new ProductListCriteriaTransfer();

// Act
$productListCollectionTransfer = $this->getFacade()->getProductListCollection($productListCriteriaTransfer);

// Assert
$this->assertCount(3, $productListCollectionTransfer->getProductLists());
$this->assertSame(
$productListTransfer1->getIdProductList(),
$productListCollectionTransfer->getProductLists()->offsetGet(0)->getIdProductList(),
);
$this->assertSame(
$productListTransfer2->getIdProductList(),
$productListCollectionTransfer->getProductLists()->offsetGet(1)->getIdProductList(),
);
$this->assertSame(
$productListTransfer3->getIdProductList(),
$productListCollectionTransfer->getProductLists()->offsetGet(2)->getIdProductList(),
);
}

/**
* @return void
*/
public function testGetProductListCollectionReturnsPaginatedProductListsWithLimitAndOffset(): void
{
// Arrange
$this->tester->ensureDatabaseTableIsEmpty(SpyProductListQuery::create());
$this->tester->haveProductList();
$productListTransfer1 = $this->tester->haveProductList();
$productListTransfer2 = $this->tester->haveProductList();
$this->tester->haveProductList();
$productListCriteriaTransfer = (new ProductListCriteriaTransfer())
->setPagination(
(new PaginationTransfer())->setOffset(1)->setLimit(2),
);

// Act
$productListCollectionTransfer = $this->getFacade()->getProductListCollection($productListCriteriaTransfer);

// Assert
$this->assertCount(2, $productListCollectionTransfer->getProductLists());
$this->assertSame(4, $productListCollectionTransfer->getPagination()->getNbResults());
$this->assertSame(
$productListTransfer1->getIdProductList(),
$productListCollectionTransfer->getProductLists()->offsetGet(0)->getIdProductList(),
);
$this->assertSame(
$productListTransfer2->getIdProductList(),
$productListCollectionTransfer->getProductLists()->offsetGet(1)->getIdProductList(),
);
}

/**
* @return void
*/
public function testGetProductAbstractIdsByProductListIdsShouldNotReturnNullIdsWhenCategoryHasNoProducts(): void
{
// Arrange
$this->tester->ensureDatabaseTableIsEmpty(SpyProductListQuery::create());

$productListTransfer = $this->tester->haveProductList();
$categoryTransfer = $this->tester->haveCategory();
$this->tester->haveProductListCategory($productListTransfer, $categoryTransfer);

// Act
$result = $this->getFacade()->getProductAbstractIdsByProductListIds([$productListTransfer->getIdProductList()]);

// Assert
$this->assertCount(0, $result);
}

/**
* @return \Spryker\Zed\ProductList\Business\ProductListFacadeInterface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@

use Codeception\Module;
use Generated\Shared\DataBuilder\ProductListBuilder;
use Generated\Shared\Transfer\CategoryTransfer;
use Generated\Shared\Transfer\ProductListTransfer;
use Orm\Zed\ProductList\Persistence\SpyProductListCategory;
use SprykerTest\Shared\Testify\Helper\DataCleanupHelperTrait;
use SprykerTest\Shared\Testify\Helper\LocatorHelperTrait;

class ProductListHelper extends Module
{
use DataCleanupHelperTrait;
use LocatorHelperTrait;

/**
Expand All @@ -32,4 +36,23 @@ public function haveProductList(array $seed = []): ProductListTransfer

return $productListResponseTransfer->getProductList();
}

/**
* @param \Generated\Shared\Transfer\ProductListTransfer $productListTransfer
* @param \Generated\Shared\Transfer\CategoryTransfer $categoryTransfer
*
* @return void
*/
public function haveProductListCategory(ProductListTransfer $productListTransfer, CategoryTransfer $categoryTransfer): void
{
$productListCategoryEntity = new SpyProductListCategory();
$productListCategoryEntity->setFkProductList($productListTransfer->getIdProductList());
$productListCategoryEntity->setFkCategory($categoryTransfer->getIdCategory());

$productListCategoryEntity->save();

$this->getDataCleanupHelper()->_addCleanup(function () use ($productListCategoryEntity): void {
$productListCategoryEntity->delete();
});
}
}
1 change: 1 addition & 0 deletions tests/SprykerTest/Zed/ProductList/codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ suites:
- \SprykerTest\Zed\ProductList\Helper\ProductListHelper
- \SprykerTest\Zed\Category\Helper\CategoryDataHelper
- \SprykerTest\Shared\Product\Helper\ProductDataHelper
- \SprykerTest\Shared\Testify\Helper\TableRelationsCleanupHelper
Persistence:
path: Persistence
actor: ProductListPersistenceTester
Expand Down

0 comments on commit 0afd11d

Please sign in to comment.