From 0afd11d060f4dcf5fd3984bbb15fba7b4337c100 Mon Sep 17 00:00:00 2001 From: Artem Siminenko Date: Mon, 16 Jan 2023 14:42:24 +0100 Subject: [PATCH] CC-22522: Publisher trigger plugins (#9813) CC-22522 PublisherTriggerPluginInterface missed in part of P&S modules - Part 2 --- composer.json | 1 + dependency.json | 5 ++ .../Transfer/product_list.transfer.xml | 15 ++++ .../ProductList/ProductListReader.php | 8 +- .../Business/ProductListFacade.php | 16 ++++ .../Business/ProductListFacadeInterface.php | 16 ++++ .../Dependency/ProductListEvents.php | 2 + .../Persistence/Mapper/ProductListMapper.php | 21 +++++ .../Persistence/ProductListRepository.php | 49 +++++++++++ .../ProductListRepositoryInterface.php | 9 ++ .../Business/ProductListFacadeTest.php | 85 +++++++++++++++++++ .../_support/Helper/ProductListHelper.php | 23 +++++ .../Zed/ProductList/codeception.yml | 1 + 13 files changed, 248 insertions(+), 3 deletions(-) create mode 100644 dependency.json diff --git a/composer.json b/composer.json index f297a4c..0ffbced 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/dependency.json b/dependency.json new file mode 100644 index 0000000..4045c43 --- /dev/null +++ b/dependency.json @@ -0,0 +1,5 @@ +{ + "include": { + "spryker/transfer": "Provides transfer objects definition with strict types and `::get*OrFail()` functionality." + } +} diff --git a/src/Spryker/Shared/ProductList/Transfer/product_list.transfer.xml b/src/Spryker/Shared/ProductList/Transfer/product_list.transfer.xml index 4cc2827..7859bcd 100644 --- a/src/Spryker/Shared/ProductList/Transfer/product_list.transfer.xml +++ b/src/Spryker/Shared/ProductList/Transfer/product_list.transfer.xml @@ -59,4 +59,19 @@ + + + + + + + + + + + + + + + diff --git a/src/Spryker/Zed/ProductList/Business/ProductList/ProductListReader.php b/src/Spryker/Zed/ProductList/Business/ProductList/ProductListReader.php index 17b6f16..568a821 100644 --- a/src/Spryker/Zed/ProductList/Business/ProductList/ProductListReader.php +++ b/src/Spryker/Zed/ProductList/Business/ProductList/ProductListReader.php @@ -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), + ), ), ); } diff --git a/src/Spryker/Zed/ProductList/Business/ProductListFacade.php b/src/Spryker/Zed/ProductList/Business/ProductListFacade.php index 16c52f6..760c681 100644 --- a/src/Spryker/Zed/ProductList/Business/ProductListFacade.php +++ b/src/Spryker/Zed/ProductList/Business/ProductListFacade.php @@ -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; @@ -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); + } } diff --git a/src/Spryker/Zed/ProductList/Business/ProductListFacadeInterface.php b/src/Spryker/Zed/ProductList/Business/ProductListFacadeInterface.php index da634b4..1338388 100644 --- a/src/Spryker/Zed/ProductList/Business/ProductListFacadeInterface.php +++ b/src/Spryker/Zed/ProductList/Business/ProductListFacadeInterface.php @@ -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; @@ -311,4 +313,18 @@ public function filterRestrictedItems(QuoteTransfer $quoteTransfer): QuoteTransf * @return array */ 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; } diff --git a/src/Spryker/Zed/ProductList/Dependency/ProductListEvents.php b/src/Spryker/Zed/ProductList/Dependency/ProductListEvents.php index 9173f31..ff03c7d 100644 --- a/src/Spryker/Zed/ProductList/Dependency/ProductListEvents.php +++ b/src/Spryker/Zed/ProductList/Dependency/ProductListEvents.php @@ -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'; diff --git a/src/Spryker/Zed/ProductList/Persistence/Mapper/ProductListMapper.php b/src/Spryker/Zed/ProductList/Persistence/Mapper/ProductListMapper.php index 77f05fa..6a7c982 100644 --- a/src/Spryker/Zed/ProductList/Persistence/Mapper/ProductListMapper.php +++ b/src/Spryker/Zed/ProductList/Persistence/Mapper/ProductListMapper.php @@ -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 { @@ -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; + } } diff --git a/src/Spryker/Zed/ProductList/Persistence/ProductListRepository.php b/src/Spryker/Zed/ProductList/Persistence/ProductListRepository.php index 06aeabe..4327980 100644 --- a/src/Spryker/Zed/ProductList/Persistence/ProductListRepository.php +++ b/src/Spryker/Zed/ProductList/Persistence/ProductListRepository.php @@ -7,6 +7,9 @@ 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; @@ -14,6 +17,7 @@ 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; @@ -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; + } } diff --git a/src/Spryker/Zed/ProductList/Persistence/ProductListRepositoryInterface.php b/src/Spryker/Zed/ProductList/Persistence/ProductListRepositoryInterface.php index c88cdba..7bace67 100644 --- a/src/Spryker/Zed/ProductList/Persistence/ProductListRepositoryInterface.php +++ b/src/Spryker/Zed/ProductList/Persistence/ProductListRepositoryInterface.php @@ -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 @@ -149,4 +151,11 @@ public function getProductWhiteListsByProductAbstractIds(array $productAbstractI * @return array */ public function getProductConcreteIdsRelatedToProductListsCategories(array $productListIds): array; + + /** + * @param \Generated\Shared\Transfer\ProductListCriteriaTransfer $productListCriteriaTransfer + * + * @return \Generated\Shared\Transfer\ProductListCollectionTransfer + */ + public function getProductListCollection(ProductListCriteriaTransfer $productListCriteriaTransfer): ProductListCollectionTransfer; } diff --git a/tests/SprykerTest/Zed/ProductList/Business/ProductListFacadeTest.php b/tests/SprykerTest/Zed/ProductList/Business/ProductListFacadeTest.php index 2c4d5b0..96d1e3a 100644 --- a/tests/SprykerTest/Zed/ProductList/Business/ProductListFacadeTest.php +++ b/tests/SprykerTest/Zed/ProductList/Business/ProductListFacadeTest.php @@ -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; /** @@ -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 */ diff --git a/tests/SprykerTest/Zed/ProductList/_support/Helper/ProductListHelper.php b/tests/SprykerTest/Zed/ProductList/_support/Helper/ProductListHelper.php index 7dd6d25..ba050f6 100644 --- a/tests/SprykerTest/Zed/ProductList/_support/Helper/ProductListHelper.php +++ b/tests/SprykerTest/Zed/ProductList/_support/Helper/ProductListHelper.php @@ -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; /** @@ -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(); + }); + } } diff --git a/tests/SprykerTest/Zed/ProductList/codeception.yml b/tests/SprykerTest/Zed/ProductList/codeception.yml index 280a933..d55ec01 100644 --- a/tests/SprykerTest/Zed/ProductList/codeception.yml +++ b/tests/SprykerTest/Zed/ProductList/codeception.yml @@ -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