Skip to content

Commit

Permalink
TE-7546 Add tests for group by usages
Browse files Browse the repository at this point in the history
  • Loading branch information
AsonUnique committed Oct 19, 2020
1 parent c4fd526 commit d6d67b4
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public function getProductListIdsByProductIds(array $productIds): array
->filterByFkProduct_In($productIds)
->innerJoinSpyProductList()
->groupByFkProductList()
->groupByFkProduct()
->find()
->toArray();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerTest\Zed\ProductList\Persistence;

use Codeception\Test\Unit;
use Generated\Shared\Transfer\ProductListProductConcreteRelationTransfer;
use Generated\Shared\Transfer\ProductListTransfer;
use Orm\Zed\ProductList\Persistence\Map\SpyProductListProductConcreteTableMap;
use Orm\Zed\ProductList\Persistence\Map\SpyProductListTableMap;
use Orm\Zed\ProductList\Persistence\SpyProductListProductConcreteQuery;
use Spryker\Zed\ProductList\Persistence\ProductListRepository;

/**
* Auto-generated group annotations
*
* @group SprykerTest
* @group Zed
* @group ProductList
* @group Persistence
* @group Repository
* @group ProductListRepositoryTest
* Add your own group annotations below this line
*/
class ProductListRepositoryTest extends Unit
{
/**
* @var \SprykerTest\Zed\ProductList\ProductListPersistenceTester
*/
protected $tester;

/**
* @var \Spryker\Zed\ProductList\Persistence\ProductListRepositoryInterface
*/
protected $productListRepository;

/**
* @var \Generated\Shared\Transfer\ProductConcreteTransfer
*/
protected $productConcreteTransfer1;

/**
* @var \Generated\Shared\Transfer\ProductConcreteTransfer
*/
protected $productConcreteTransfer2;

/**
* @var \Generated\Shared\Transfer\ProductListProductConcreteRelationTransfer
*/
protected $productListProductConcreteRelation;

/**
* @return void
*/
protected function setUp(): void
{
parent::setUp();

$this->productListRepository = new ProductListRepository();
$this->productConcreteTransfer1 = $this->tester->haveProduct();
$this->productConcreteTransfer2 = $this->tester->haveProduct();

$this->productListProductConcreteRelation = (new ProductListProductConcreteRelationTransfer())
->setProductIds([
$this->productConcreteTransfer1->getIdProductConcrete(),
$this->productConcreteTransfer2->getIdProductConcrete()
]);
}

/**
* @return void
*/
public function testGetProductListIdsByProductIdsReturnsCorrectData(): void
{
//Assert
$productConcreteIds = [
$this->productConcreteTransfer1->getIdProductConcrete(),
$this->productConcreteTransfer2->getIdProductConcrete()
];
$this->tester->haveProductList([
ProductListTransfer::PRODUCT_LIST_PRODUCT_CONCRETE_RELATION => $this->productListProductConcreteRelation->toArray(),
]);

//Act
$result = $this->productListRepository->getProductListIdsByProductIds($productConcreteIds);

//Arrange
$this->assertCount(2, $result);
$resultProductConcreteIds = array_column($result, SpyProductListProductConcreteTableMap::COL_FK_PRODUCT);
$this->assertContains($productConcreteIds[0], $resultProductConcreteIds);
$this->assertContains($productConcreteIds[1], $resultProductConcreteIds);
}

/**
* @return void
*/
public function testGetProductConcreteProductListIdsForTypeReturnsCorrectProductListId(): void
{
//Assert

$productListTransfer = $this->tester->haveProductList([
ProductListTransfer::PRODUCT_LIST_PRODUCT_CONCRETE_RELATION => $this->productListProductConcreteRelation->toArray(),
]);

//Act
$result = $this->productListRepository->getProductConcreteProductListIdsForType(
$this->productConcreteTransfer1->getIdProductConcrete(),
$productListTransfer->getType()
);

//Arrange
$this->assertCount(1, $result);
$this->assertSame($productListTransfer->getIdProductList(), $result[0]);
}

/**
* @return void
*/
public function testGetProductBlacklistIdsByIdProductAbstractReturnsCorrectProductListId(): void
{
//Assert
$productListTransfer = $this->tester->haveProductList([
ProductListTransfer::TYPE => SpyProductListTableMap::COL_TYPE_BLACKLIST,
ProductListTransfer::PRODUCT_LIST_PRODUCT_CONCRETE_RELATION => $this->productListProductConcreteRelation->toArray(),
]);

//Act
$result = $this->productListRepository->getProductBlacklistIdsByIdProductAbstract(
$this->productConcreteTransfer1->getFkProductAbstract()
);

//Arrange
$this->assertCount(1, $result);
$this->assertSame($productListTransfer->getIdProductList(), $result[0]);
}

/**
* @return void
*/
public function testGetAbstractProductWhitelistIdsReturnsCorrectProductListId(): void
{
//Assert
$productListTransfer = $this->tester->haveProductList([
ProductListTransfer::TYPE => SpyProductListTableMap::COL_TYPE_WHITELIST,
ProductListTransfer::PRODUCT_LIST_PRODUCT_CONCRETE_RELATION => $this->productListProductConcreteRelation->toArray(),
]);

//Act
$result = $this->productListRepository->getAbstractProductWhitelistIds(
$this->productConcreteTransfer1->getFkProductAbstract()
);

//Arrange
$this->assertCount(1, $result);
$this->assertSame($productListTransfer->getIdProductList(), $result[0]);
}

/**
* @return void
*/
public function testGetProductListByProductAbstractIdsThroughCategoryReturnsCorrectProductAbstractIds(): void
{
//Assert
$categoryTransfer = $this->tester->haveCategory();
$this->tester->assignProductToCategory($categoryTransfer->getIdCategory(), $this->productConcreteTransfer1->getFkProductAbstract());
$this->tester->assignProductToCategory($categoryTransfer->getIdCategory(), $this->productConcreteTransfer2->getFkProductAbstract());

$productListTransfer = $this->tester->haveProductList([
ProductListTransfer::TYPE => SpyProductListTableMap::COL_TYPE_WHITELIST,
ProductListTransfer::PRODUCT_LIST_PRODUCT_CONCRETE_RELATION => $this->productListProductConcreteRelation->toArray(),
]);
$this->tester->haveProductListCategory($productListTransfer->getIdProductList(), $categoryTransfer->getIdCategory());

$productAbstractIds = [
$this->productConcreteTransfer1->getFkProductAbstract(),
$this->productConcreteTransfer2->getFkProductAbstract(),
];

//Act
$result = $this->productListRepository->getProductListByProductAbstractIdsThroughCategory(
$productAbstractIds
);

//Arrange
$this->assertCount(2, $result);
$resultProductAbstractIds = array_column($result, ProductListRepository::COL_ID_PRODUCT_ABSTRACT);
$this->assertContains($productAbstractIds[0], $resultProductAbstractIds);
$this->assertContains($productAbstractIds[1], $resultProductAbstractIds);
}

/**
* @return void
*/
public function testGetProductBlacklistsByProductAbstractIdsReturnsCorrectData(): void
{
//Assert
$this->tester->haveProductList([
ProductListTransfer::TYPE => SpyProductListTableMap::COL_TYPE_BLACKLIST,
ProductListTransfer::PRODUCT_LIST_PRODUCT_CONCRETE_RELATION => $this->productListProductConcreteRelation->toArray(),
]);

$productAbstractIds = [
$this->productConcreteTransfer1->getFkProductAbstract(),
$this->productConcreteTransfer2->getFkProductAbstract(),
];

//Act
$result = $this->productListRepository->getProductBlacklistsByProductAbstractIds($productAbstractIds);

//Arrange
$this->assertCount(2, $result);
$resultProductAbstractIds = array_column($result, ProductListRepository::COL_ID_PRODUCT_ABSTRACT);
$this->assertContains($productAbstractIds[0], $resultProductAbstractIds);
$this->assertContains($productAbstractIds[1], $resultProductAbstractIds);
}

/**
* @return void
*/
public function testGetProductWhitelistsByProductAbstractIdsReturnsCorrectData(): void
{
//Assert
$this->tester->haveProductList([
ProductListTransfer::TYPE => SpyProductListTableMap::COL_TYPE_WHITELIST,
ProductListTransfer::PRODUCT_LIST_PRODUCT_CONCRETE_RELATION => $this->productListProductConcreteRelation->toArray(),
]);

$productAbstractIds = [
$this->productConcreteTransfer1->getFkProductAbstract(),
$this->productConcreteTransfer2->getFkProductAbstract(),
];

//Act
$result = $this->productListRepository->getProductWhiteListsByProductAbstractIds($productAbstractIds);

//Arrange
$this->assertCount(2, $result);
$resultProductAbstractIds = array_column($result, ProductListRepository::COL_ID_PRODUCT_ABSTRACT);
$this->assertContains($productAbstractIds[0], $resultProductAbstractIds);
$this->assertContains($productAbstractIds[1], $resultProductAbstractIds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace SprykerTest\Zed\ProductList;

use Codeception\Actor;
use Generated\Shared\Transfer\ProductListCategoryRelationTransfer;
use Orm\Zed\ProductList\Persistence\SpyProductListCategoryQuery;

/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause()
*
* @SuppressWarnings(PHPMD)
*/
class ProductListPersistenceTester extends Actor
{
use _generated\ProductListPersistenceTesterActions;

/**
* @param int $idProductList
* @param int $idCategory
*
* @return \Generated\Shared\Transfer\ProductListCategoryRelationTransfer
*/
public function haveProductListCategory(int $idProductList, int $idCategory): ProductListCategoryRelationTransfer
{
$productListProductCategoryEntity = SpyProductListCategoryQuery::create()
->filterByFkProductList($idProductList)
->filterByFkCategory($idCategory)
->findOneOrCreate();

if ($productListProductCategoryEntity->isNew()) {
$productListProductCategoryEntity->save();
}

return (new ProductListCategoryRelationTransfer())
->setCategoryIds([$productListProductCategoryEntity->getFkCategory()])
->setIdProductList($productListProductCategoryEntity->getFkProductList());
}
}
15 changes: 15 additions & 0 deletions tests/SprykerTest/Zed/ProductList/codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ suites:
- \SprykerTest\Zed\ProductList\Helper\ProductListHelper
- \SprykerTest\Zed\Category\Helper\CategoryDataHelper
- \SprykerTest\Shared\Product\Helper\ProductDataHelper
Persistence:
path: Persistence
class_name: ProductListPersistenceTester
modules:
enabled:
- Asserts
- \SprykerTest\Shared\Testify\Helper\Environment
- \SprykerTest\Shared\Testify\Helper\ConfigHelper
- \SprykerTest\Shared\Testify\Helper\LocatorHelper
- \SprykerTest\Shared\Propel\Helper\TransactionHelper
- \SprykerTest\Shared\Testify\Helper\DataCleanupHelper
- \SprykerTest\Zed\ProductList\Helper\ProductListHelper
- \SprykerTest\Zed\Category\Helper\CategoryDataHelper
- \SprykerTest\Zed\ProductCategory\Helper\AssignHelper
- \SprykerTest\Shared\Product\Helper\ProductDataHelper

0 comments on commit d6d67b4

Please sign in to comment.