diff --git a/phpstan.neon b/phpstan.neon index 3260382..42eea45 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,3 +4,12 @@ parameters: - src/ excludePaths: - src/SprykerEco/Zed/AmazonQuicksight/Persistence/Propel/Abstract*.php + ignoreErrors: + - '#Call to method .+ on an unknown class .+Transfer#' + - '#Parameter .+ of method .+ has invalid typehint type .+Transfer#' + - '#Return typehint of method .+ has invalid type .+Transfer#' + - '#Instantiated class .+Transfer not found#' + - '#Return typehint of method .+ has invalid type Orm\\.+#' + - '#Call to method .+\(\) on an unknown class Orm\\.+#' + - '#Call to static method create\(\) on an unknown class .+Query.#' + - '#Parameter \$.+ of method .+ has invalid typehint type Orm\\.+#' diff --git a/src/SprykerEco/Shared/AmazonQuicksight/Transfer/amazon_quicksight.transfer.xml b/src/SprykerEco/Shared/AmazonQuicksight/Transfer/amazon_quicksight.transfer.xml index dc33cc7..a06f23b 100644 --- a/src/SprykerEco/Shared/AmazonQuicksight/Transfer/amazon_quicksight.transfer.xml +++ b/src/SprykerEco/Shared/AmazonQuicksight/Transfer/amazon_quicksight.transfer.xml @@ -13,7 +13,12 @@ + + + + + diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightBusinessFactory.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightBusinessFactory.php index 07adc4a..683b2b9 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightBusinessFactory.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightBusinessFactory.php @@ -8,6 +8,8 @@ namespace SprykerEco\Zed\AmazonQuicksight\Business; use Spryker\Zed\Kernel\Business\AbstractBusinessFactory; +use SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpander; +use SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpanderInterface; /** * @method \SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig getConfig() @@ -16,4 +18,11 @@ */ class AmazonQuicksightBusinessFactory extends AbstractBusinessFactory { + /** + * @return \SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpanderInterface + */ + public function createUserExpander(): UserExpanderInterface + { + return new UserExpander($this->getRepository()); + } } diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacade.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacade.php index c8b7f3a..10ea988 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacade.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacade.php @@ -7,6 +7,7 @@ namespace SprykerEco\Zed\AmazonQuicksight\Business; +use Generated\Shared\Transfer\UserCollectionTransfer; use Spryker\Zed\Kernel\Business\AbstractFacade; /** @@ -16,4 +17,18 @@ */ class AmazonQuicksightFacade extends AbstractFacade implements AmazonQuicksightFacadeInterface { + /** + * {@inheritDoc} + * + * @api + * + * @param \Generated\Shared\Transfer\UserCollectionTransfer $userCollectionTransfer + * + * @return \Generated\Shared\Transfer\UserCollectionTransfer + */ + public function expandUserCollectionWithQuicksightUsers( + UserCollectionTransfer $userCollectionTransfer + ): UserCollectionTransfer { + return $this->getFactory()->createUserExpander()->expandUserCollectionWithQuicksightUser($userCollectionTransfer); + } } diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacadeInterface.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacadeInterface.php index 30abb24..86069e2 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacadeInterface.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacadeInterface.php @@ -7,6 +7,24 @@ namespace SprykerEco\Zed\AmazonQuicksight\Business; +use Generated\Shared\Transfer\UserCollectionTransfer; + interface AmazonQuicksightFacadeInterface { + /** + * Specification: + * - Requires `UserTransfer.idUser` for each user in `UserCollectionTransfer` to be set. + * - Iterates over `UserCollectionTransfer.users`. + * - Finds Quicksight users by `UserTransfer.idUser` in DB. + * - Populates `UserTransfer.quicksightUser` in collection with found Quicksight users. + * + * @api + * + * @param \Generated\Shared\Transfer\UserCollectionTransfer $userCollectionTransfer + * + * @return \Generated\Shared\Transfer\UserCollectionTransfer + */ + public function expandUserCollectionWithQuicksightUsers( + UserCollectionTransfer $userCollectionTransfer + ): UserCollectionTransfer; } diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/Expander/UserExpander.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/Expander/UserExpander.php new file mode 100644 index 0000000..c0ef717 --- /dev/null +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/Expander/UserExpander.php @@ -0,0 +1,84 @@ +amazonQuicksightRepository = $amazonQuicksightRepository; + } + + /** + * @param \Generated\Shared\Transfer\UserCollectionTransfer $userCollectionTransfer + * + * @return \Generated\Shared\Transfer\UserCollectionTransfer + */ + public function expandUserCollectionWithQuicksightUser( + UserCollectionTransfer $userCollectionTransfer + ): UserCollectionTransfer { + $userIds = $this->extractUserIds($userCollectionTransfer); + $quicksightUserTransfers = $this->amazonQuicksightRepository->getQuicksightUsersByUserIds($userIds); + + if (!$quicksightUserTransfers) { + return $userCollectionTransfer; + } + + $quicksightUserTransfersIndexedByIdUser = $this->getQuicksightUserTransfersIndexedByIdUser($quicksightUserTransfers); + + foreach ($userCollectionTransfer->getUsers() as $userTransfer) { + $idUser = $userTransfer->getIdUserOrFail(); + if (isset($quicksightUserTransfersIndexedByIdUser[$idUser])) { + $userTransfer->setQuicksightUser($quicksightUserTransfersIndexedByIdUser[$idUser]); + } + } + + return $userCollectionTransfer; + } + + /** + * @param \Generated\Shared\Transfer\UserCollectionTransfer $userCollectionTransfer + * + * @return list + */ + protected function extractUserIds(UserCollectionTransfer $userCollectionTransfer): array + { + $userIds = []; + foreach ($userCollectionTransfer->getUsers() as $userTransfer) { + $userIds[] = $userTransfer->getIdUserOrFail(); + } + + return $userIds; + } + + /** + * @param list<\Generated\Shared\Transfer\QuicksightUserTransfer> $quicksightUserTransfers + * + * @return array + */ + protected function getQuicksightUserTransfersIndexedByIdUser(array $quicksightUserTransfers): array + { + $quicksightUserTransfersIndexedByIdUser = []; + foreach ($quicksightUserTransfers as $quicksightUserTransfer) { + $quicksightUserTransfersIndexedByIdUser[$quicksightUserTransfer->getFkUserOrFail()] = $quicksightUserTransfer; + } + + return $quicksightUserTransfersIndexedByIdUser; + } +} diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/Expander/UserExpanderInterface.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/Expander/UserExpanderInterface.php new file mode 100644 index 0000000..8ea11b5 --- /dev/null +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/Expander/UserExpanderInterface.php @@ -0,0 +1,22 @@ +getFacade()->expandUserCollectionWithQuicksightUsers($userCollectionTransfer); + } +} diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightPersistenceFactory.php b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightPersistenceFactory.php index 2c52f7c..da0fc03 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightPersistenceFactory.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightPersistenceFactory.php @@ -7,7 +7,9 @@ namespace SprykerEco\Zed\AmazonQuicksight\Persistence; +use Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUserQuery; use Spryker\Zed\Kernel\Persistence\AbstractPersistenceFactory; +use SprykerEco\Zed\AmazonQuicksight\Persistence\Propel\Mapper\QuicksightUserMapper; /** * @method \SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig getConfig() @@ -16,4 +18,19 @@ */ class AmazonQuicksightPersistenceFactory extends AbstractPersistenceFactory { + /** + * @return \SprykerEco\Zed\AmazonQuicksight\Persistence\Propel\Mapper\QuicksightUserMapper + */ + public function createQuicksightUserMapper(): QuicksightUserMapper + { + return new QuicksightUserMapper(); + } + + /** + * @return \Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUserQuery + */ + public function getQuicksightUserQuery(): SpyQuicksightUserQuery + { + return SpyQuicksightUserQuery::create(); + } } diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightRepository.php b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightRepository.php index 7bdf106..9c3bebb 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightRepository.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightRepository.php @@ -14,4 +14,24 @@ */ class AmazonQuicksightRepository extends AbstractRepository implements AmazonQuicksightRepositoryInterface { + /** + * @param list $userIds + * + * @return list<\Generated\Shared\Transfer\QuicksightUserTransfer> + */ + public function getQuicksightUsersByUserIds(array $userIds): array + { + $quicksightUserEntities = $this->getFactory() + ->getQuicksightUserQuery() + ->filterByFkUser_In($userIds) + ->find(); + + if (!$quicksightUserEntities->count()) { + return []; + } + + return $this->getFactory() + ->createQuicksightUserMapper() + ->mapQuicksightUserEntitiesToQuicksightUserTransfers($quicksightUserEntities, []); + } } diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightRepositoryInterface.php b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightRepositoryInterface.php index 174fe0b..4aaecb0 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightRepositoryInterface.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightRepositoryInterface.php @@ -9,4 +9,10 @@ interface AmazonQuicksightRepositoryInterface { + /** + * @param list $userIds + * + * @return list<\Generated\Shared\Transfer\QuicksightUserTransfer> + */ + public function getQuicksightUsersByUserIds(array $userIds): array; } diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/Propel/Mapper/QuicksightUserMapper.php b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/Propel/Mapper/QuicksightUserMapper.php new file mode 100644 index 0000000..904cce3 --- /dev/null +++ b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/Propel/Mapper/QuicksightUserMapper.php @@ -0,0 +1,48 @@ + $quicksightUserEntities + * @param list<\Generated\Shared\Transfer\QuicksightUserTransfer> $quicksightUserTransfers + * + * @return list<\Generated\Shared\Transfer\QuicksightUserTransfer> + */ + public function mapQuicksightUserEntitiesToQuicksightUserTransfers( + Collection $quicksightUserEntities, + array $quicksightUserTransfers + ): array { + foreach ($quicksightUserEntities as $quicksightUserEntity) { + $quicksightUserTransfers[] = $this->mapQuicksightUserEntityToQuicksightUserTransfer( + $quicksightUserEntity, + new QuicksightUserTransfer(), + ); + } + + return $quicksightUserTransfers; + } + + /** + * @param \Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUser $quicksightUserEntity + * @param \Generated\Shared\Transfer\QuicksightUserTransfer $quicksightUserTransfer + * + * @return \Generated\Shared\Transfer\QuicksightUserTransfer + */ + protected function mapQuicksightUserEntityToQuicksightUserTransfer( + SpyQuicksightUser $quicksightUserEntity, + QuicksightUserTransfer $quicksightUserTransfer + ): QuicksightUserTransfer { + return $quicksightUserTransfer->fromArray($quicksightUserEntity->toArray(), true); + } +} diff --git a/tests/SprykerEcoTest/Zed/AmazonQuicksight/Business/Facade/ExpandUserCollectionWithQuicksightUsersTest.php b/tests/SprykerEcoTest/Zed/AmazonQuicksight/Business/Facade/ExpandUserCollectionWithQuicksightUsersTest.php new file mode 100644 index 0000000..bfbc433 --- /dev/null +++ b/tests/SprykerEcoTest/Zed/AmazonQuicksight/Business/Facade/ExpandUserCollectionWithQuicksightUsersTest.php @@ -0,0 +1,90 @@ +tester->haveUser(); + $userTransfer2 = $this->tester->haveUser(); + $this->tester->haveQuicksightUser($userTransfer2); + $userCollectionTransfer = (new UserCollectionTransfer())->addUser($userTransfer1); + + // Act + $this->tester->getFacade()->expandUserCollectionWithQuicksightUsers($userCollectionTransfer); + + // Assert + $this->assertNull($userCollectionTransfer->getUsers()->offsetGet(0)->getQuicksightUser()); + } + + /** + * @return void + */ + public function testExpandsAllUsersInCollectionWhenQuicksightUsersFound(): void + { + // Arrange + $userTransfer1 = $this->tester->haveUser(); + $userTransfer2 = $this->tester->haveUser(); + $quicksightUserTransfer1 = $this->tester->haveQuicksightUser($userTransfer1); + $quicksightUserTransfer2 = $this->tester->haveQuicksightUser($userTransfer2); + $userCollectionTransfer = (new UserCollectionTransfer()) + ->addUser($userTransfer1) + ->addUser($userTransfer2); + + // Act + $this->tester->getFacade()->expandUserCollectionWithQuicksightUsers($userCollectionTransfer); + + // Assert + $this->assertSame( + $quicksightUserTransfer1->getFkUser(), + $userCollectionTransfer->getUsers()->offsetGet(0)->getQuicksightUser()->getFkUser(), + ); + $this->assertSame( + $quicksightUserTransfer2->getFkUser(), + $userCollectionTransfer->getUsers()->offsetGet(1)->getQuicksightUser()->getFkUser(), + ); + } + + /** + * @return void + */ + public function testExpandsOneUserInCollectionWhenOneQuicksightUserFound(): void + { + // Arrange + $userTransfer1 = $this->tester->haveUser(); + $userTransfer2 = $this->tester->haveUser(); + $quicksightUserTransfer = $this->tester->haveQuicksightUser($userTransfer2); + $userCollectionTransfer = (new UserCollectionTransfer()) + ->addUser($userTransfer1) + ->addUser($userTransfer2); + + // Act + $this->tester->getFacade()->expandUserCollectionWithQuicksightUsers($userCollectionTransfer); + + // Assert + $this->assertNull($userCollectionTransfer->getUsers()->offsetGet(0)->getQuicksightUser()); + $this->assertSame( + $quicksightUserTransfer->getFkUser(), + $userCollectionTransfer->getUsers()->offsetGet(1)->getQuicksightUser()->getFkUser(), + ); + } +} diff --git a/tests/SprykerEcoTest/Zed/AmazonQuicksight/_support/AmazonQuicksightTester.php b/tests/SprykerEcoTest/Zed/AmazonQuicksight/_support/AmazonQuicksightTester.php new file mode 100644 index 0000000..a3fe1fe --- /dev/null +++ b/tests/SprykerEcoTest/Zed/AmazonQuicksight/_support/AmazonQuicksightTester.php @@ -0,0 +1,54 @@ +build(); + $quicksightUserTransfer->setFkUser($userTransfer->getIdUserOrFail()); + $quicksightUserEntity = (new SpyQuicksightUser()) + ->fromArray($quicksightUserTransfer->toArray()); + $quicksightUserEntity->save(); + + $quicksightUserTransfer->setFkUser($quicksightUserEntity->getFkUser()); + + return $quicksightUserTransfer; + } +} diff --git a/tests/SprykerEcoTest/Zed/AmazonQuicksight/codeception.yml b/tests/SprykerEcoTest/Zed/AmazonQuicksight/codeception.yml index e3d070b..5f849d0 100644 --- a/tests/SprykerEcoTest/Zed/AmazonQuicksight/codeception.yml +++ b/tests/SprykerEcoTest/Zed/AmazonQuicksight/codeception.yml @@ -7,6 +7,18 @@ paths: coverage: enabled: true remote: false - whitelist: - include: - - '../../../../src/*' + whitelist: { include: ['../../../../src/*'] } + +suites: + Business: + path: . + actor: AmazonQuicksightTester + modules: + enabled: + - Asserts + - \SprykerTest\Zed\Testify\Helper\Business\BusinessHelper + - \SprykerTest\Shared\Testify\Helper\DataCleanupHelper + - \SprykerTest\Shared\Testify\Helper\Environment + - \SprykerTest\Shared\Testify\Helper\LocatorHelper + - \SprykerTest\Shared\Propel\Helper\TransactionHelper + - \SprykerTest\Shared\User\Helper\UserDataHelper diff --git a/tests/_data/amazon_quicksight.databuilder.xml b/tests/_data/amazon_quicksight.databuilder.xml new file mode 100644 index 0000000..d3a446b --- /dev/null +++ b/tests/_data/amazon_quicksight.databuilder.xml @@ -0,0 +1,15 @@ + + + + + + + + + + +