Skip to content

Commit

Permalink
Merge branch 'feature/cc-34041/dev-quick-sight-mvp' into feature/cc-3…
Browse files Browse the repository at this point in the history
…4041/cc-34104-embed-qs-iframe-in-backoffice
  • Loading branch information
ilyakubanov committed Aug 8, 2024
2 parents 38e639f + 20f2e87 commit bce9951
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 41 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ parameters:
- '#Parameter .+ of method .+ has invalid typehint type .+Transfer#'
- '#Return typehint of method .+ has invalid type .+Transfer#'
- '#Instantiated class .+Transfer not found#'
- '#Instantiated class Orm\\Zed\\AmazonQuicksight\\Persistence\\.+ 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.#'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpanderInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatter;
use SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatterInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\AmazonQuicksightMapper;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\AmazonQuicksightMapperInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapper;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapperInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightEmbedUrlMapper;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightEmbedUrlMapperInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Provider\AnalyticsEmbedUrlProvider;
Expand Down Expand Up @@ -60,19 +60,19 @@ public function createAmazonQuicksightApiClient(): AmazonQuicksightApiClientInte
{
return new AmazonQuicksightApiClient(
$this->getConfig(),
$this->createAmazonQuicksightMapper(),
$this->createQuicksightUserMapper(),
$this->createQuicksightEmbedUrlMapper(),
$this->createAmazonQuicksightRequestDataFormatter(),
$this->getAwsQuicksightClient(),
);
}

/**
* @return \SprykerEco\Zed\AmazonQuicksight\Business\Mapper\AmazonQuicksightMapperInterface
* @return \SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapperInterface
*/
public function createAmazonQuicksightMapper(): AmazonQuicksightMapperInterface
public function createQuicksightUserMapper(): QuicksightUserMapperInterface
{
return new AmazonQuicksightMapper();
return new QuicksightUserMapper();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public function expandUserCollectionWithQuicksightUsers(
*
* @return \Generated\Shared\Transfer\UserCollectionResponseTransfer
*/
public function createQuicksightUsersForUserTransfers(
public function createQuicksightUsersForUserCollectionResponse(
UserCollectionResponseTransfer $userCollectionResponseTransfer
): UserCollectionResponseTransfer {
return $this->getFactory()
->createQuicksightUserCreator()
->createQuicksightUsersForUserTransfers($userCollectionResponseTransfer);
->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function expandUserCollectionWithQuicksightUsers(
/**
* Specification:
* - Expects `UserCollectionResponseTransfer.users.quicksightUser.role` to be set.
* - Does nothing if `UserTransfer.quicksightUser.role` is not set.
* - Sends request to AWS API to register Quicksight users. For more information see {@link https://docs.aws.amazon.com/quicksight/latest/APIReference/API_RegisterUser.html}.
* - Adds errors to `UserCollectionResponseTransfer.errors` if Quicksight user registration failed.
* - Persists successfully registered Quicksight users in the database.
Expand All @@ -45,7 +46,7 @@ public function expandUserCollectionWithQuicksightUsers(
*
* @return \Generated\Shared\Transfer\UserCollectionResponseTransfer
*/
public function createQuicksightUsersForUserTransfers(
public function createQuicksightUsersForUserCollectionResponse(
UserCollectionResponseTransfer $userCollectionResponseTransfer
): UserCollectionResponseTransfer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Generated\Shared\Transfer\UserTransfer;
use SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig;
use SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatterInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\AmazonQuicksightMapperInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapperInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightEmbedUrlMapperInterface;
use SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface;

Expand Down Expand Up @@ -59,15 +59,18 @@ class AmazonQuicksightApiClient implements AmazonQuicksightApiClientInterface
protected AmazonQuicksightConfig $amazonQuicksightConfig;

/**
* @var \SprykerEco\Zed\AmazonQuicksight\Business\Mapper\AmazonQuicksightMapperInterface
* @var \SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapperInterface
*/
protected AmazonQuicksightMapperInterface $amazonQuicksightMapper;
protected QuicksightUserMapperInterface $quicksightUserMapper;

/**
* @var \SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightEmbedUrlMapperInterface
*/
protected QuicksightEmbedUrlMapperInterface $quicksightEmbedUrlMapper;

/**
* @var \SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatterInterface
*/
protected AmazonQuicksightRequestDataFormatterInterface $amazonQuicksightRequestDataFormatter;

/**
Expand All @@ -77,20 +80,20 @@ class AmazonQuicksightApiClient implements AmazonQuicksightApiClientInterface

/**
* @param \SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig $amazonQuicksightConfig
* @param \SprykerEco\Zed\AmazonQuicksight\Business\Mapper\AmazonQuicksightMapperInterface $amazonQuicksightMapper
* @param \SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapperInterface $quicksightUserMapper
* @param \SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightEmbedUrlMapperInterface $quicksightEmbedUrlMapper
* @param \SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatterInterface $amazonQuicksightRequestDataFormatter
* @param \SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface $amazonQuicksightToAwsQuicksightClient
*/
public function __construct(
AmazonQuicksightConfig $amazonQuicksightConfig,
AmazonQuicksightMapperInterface $amazonQuicksightMapper,
QuicksightUserMapperInterface $quicksightUserMapper,
QuicksightEmbedUrlMapperInterface $quicksightEmbedUrlMapper,
AmazonQuicksightRequestDataFormatterInterface $amazonQuicksightRequestDataFormatter,
AmazonQuicksightToAwsQuicksightClientInterface $amazonQuicksightToAwsQuicksightClient
) {
$this->amazonQuicksightConfig = $amazonQuicksightConfig;
$this->amazonQuicksightMapper = $amazonQuicksightMapper;
$this->quicksightUserMapper = $quicksightUserMapper;
$this->quicksightEmbedUrlMapper = $quicksightEmbedUrlMapper;
$this->amazonQuicksightRequestDataFormatter = $amazonQuicksightRequestDataFormatter;
$this->amazonQuicksightToAwsQuicksightClient = $amazonQuicksightToAwsQuicksightClient;
Expand All @@ -104,7 +107,7 @@ public function __construct(
public function registerUser(UserTransfer $userTransfer): QuicksightUserRegisterResponseTransfer
{
$quicksightUserRegisterRequestTransfer = $this->createQuicksightUserRegisterRequestTransfer();
$quicksightUserRegisterRequestTransfer = $this->amazonQuicksightMapper->mapUserTransferToQuicksightUserRegisterRequestTransfer(
$quicksightUserRegisterRequestTransfer = $this->quicksightUserMapper->mapUserTransferToQuicksightUserRegisterRequestTransfer(
$userTransfer,
$quicksightUserRegisterRequestTransfer,
);
Expand All @@ -128,7 +131,7 @@ public function registerUser(UserTransfer $userTransfer): QuicksightUserRegister
);
}

$quicksightUserTransfer = $this->amazonQuicksightMapper->mapQuicksightUserDataToQuicksightUserTransfer(
$quicksightUserTransfer = $this->quicksightUserMapper->mapQuicksightUserDataToQuicksightUserTransfer(
$response->get(static::RESPONSE_KEY_USER),
$userTransfer->getQuicksightUserOrFail(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(
*
* @return \Generated\Shared\Transfer\UserCollectionResponseTransfer
*/
public function createQuicksightUsersForUserTransfers(
public function createQuicksightUsersForUserCollectionResponse(
UserCollectionResponseTransfer $userCollectionResponseTransfer
): UserCollectionResponseTransfer {
$userTransfersWithQuicksightUserRole = $this->extractUserTransfersWithQuicksightUserRole(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface QuicksightUserCreatorInterface
*
* @return \Generated\Shared\Transfer\UserCollectionResponseTransfer
*/
public function createQuicksightUsersForUserTransfers(
public function createQuicksightUsersForUserCollectionResponse(
UserCollectionResponseTransfer $userCollectionResponseTransfer
): UserCollectionResponseTransfer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Generated\Shared\Transfer\QuicksightUserTransfer;
use Generated\Shared\Transfer\UserTransfer;

class AmazonQuicksightMapper implements AmazonQuicksightMapperInterface
class QuicksightUserMapper implements QuicksightUserMapperInterface
{
/**
* @param \Generated\Shared\Transfer\UserTransfer $userTransfer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Generated\Shared\Transfer\QuicksightUserTransfer;
use Generated\Shared\Transfer\UserTransfer;

interface AmazonQuicksightMapperInterface
interface QuicksightUserMapperInterface
{
/**
* @param \Generated\Shared\Transfer\UserTransfer $userTransfer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class QuicksightUserPostCreatePlugin extends AbstractPlugin implements UserPostC
/**
* {@inheritDoc}
* - Expects `UserCollectionResponseTransfer.users.quicksightUser.role` to be set.
* - Does nothing if `UserTransfer.quicksightUser.role` is not set.
* - Sends request to AWS API to register Quicksight users. For more information see {@link https://docs.aws.amazon.com/quicksight/latest/APIReference/API_RegisterUser.html}.
* - Adds errors to `UserCollectionResponseTransfer.errors` if Quicksight user registration failed.
* - Persists successfully registered Quicksight users in the database.
Expand All @@ -34,6 +35,6 @@ class QuicksightUserPostCreatePlugin extends AbstractPlugin implements UserPostC
*/
public function postCreate(UserCollectionResponseTransfer $userCollectionResponseTransfer): UserCollectionResponseTransfer
{
return $this->getFacade()->createQuicksightUsersForUserTransfers($userCollectionResponseTransfer);
return $this->getFacade()->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface;
use SprykerEcoTest\Zed\AmazonQuicksight\AmazonQuicksightBusinessTester;

class CreateQuicksightUsersForUserTransfersTest extends Unit
class CreateQuicksightUsersForUserCollectionResponseTest extends Unit
{
/**
* @uses \SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig::QUICKSIGHT_USER_ROLE_READER
Expand Down Expand Up @@ -51,24 +51,26 @@ public function testShouldPersistQuicksightUserWhenUserSuccessfullyRegisteredInQ
// Arrange
$userTransfer = $this->tester->haveUserWithNotPersistedQuicksightUserRole(static::QUICKSIGHT_USER_ROLE_READER);

$awsQuicksightClientMock = $this->getAwsQuicksightClientMock();
$awsQuicksightClientMock->method('registerUser')
->willReturn($this->createRegisterUserSuccessfulResponse($userTransfer));
$this->tester->setDependency(
AmazonQuicksightDependencyProvider::AWS_QUICKSIGHT_CLIENT,
$awsQuicksightClientMock,
$this->getAwsQuicksightClientMockWithSuccessfulResponse(
$this->createRegisterUserSuccessfulResponse($userTransfer),
),
);

$userCollectionResponseTransfer = (new UserCollectionResponseTransfer())->addUser($userTransfer);

// Act
$userCollectionResponseTransfer = $this->tester->getFacade()
->createQuicksightUsersForUserTransfers($userCollectionResponseTransfer);
->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer);

// Assert
$this->assertCount(0, $userCollectionResponseTransfer->getErrors());
$this->assertCount(1, $userCollectionResponseTransfer->getUsers());

$quicksightUserEntity = $this->tester->findQuicksightUserByIdUser($userTransfer->getIdUserOrFail());
$this->assertNotNull($quicksightUserEntity);

/** @var \Generated\Shared\Transfer\UserTransfer $userTransfer */
$userTransfer = $userCollectionResponseTransfer->getUsers()->getIterator()->current();
$this->assertNotNull($userTransfer->getQuicksightUser());
Expand All @@ -85,19 +87,18 @@ public function testShouldReturnErrorWhenUserQuicksightClientThrowsException():
// Arrange
$userTransfer = $this->tester->haveUserWithNotPersistedQuicksightUserRole(static::QUICKSIGHT_USER_ROLE_READER);

$awsQuicksightClientMock = $this->getAwsQuicksightClientMock();
$awsQuicksightClientMock->method('registerUser')
->willThrowException($this->getQuicksightExceptionMock(static::ERROR_MESSAGE_QUICKSIGHT_USER_REGISTER_FAILURE));
$this->tester->setDependency(
AmazonQuicksightDependencyProvider::AWS_QUICKSIGHT_CLIENT,
$awsQuicksightClientMock,
$this->getAwsQuicksightClientMockWithErrorResponse(
$this->getQuicksightExceptionMock(static::ERROR_MESSAGE_QUICKSIGHT_USER_REGISTER_FAILURE),
),
);

$userCollectionResponseTransfer = (new UserCollectionResponseTransfer())->addUser($userTransfer);

// Act
$userCollectionResponseTransfer = $this->tester->getFacade()
->createQuicksightUsersForUserTransfers($userCollectionResponseTransfer);
->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer);

// Assert
$this->assertCount(1, $userCollectionResponseTransfer->getErrors());
Expand All @@ -124,19 +125,16 @@ public function testShouldReturnErrorWhenQuicksightUserRegisterResponseDoesNotCo
// Arrange
$userTransfer = $this->tester->haveUserWithNotPersistedQuicksightUserRole(static::QUICKSIGHT_USER_ROLE_READER);

$awsQuicksightClientMock = $this->getAwsQuicksightClientMock();
$awsQuicksightClientMock->method('registerUser')
->willReturn(new Result());
$this->tester->setDependency(
AmazonQuicksightDependencyProvider::AWS_QUICKSIGHT_CLIENT,
$awsQuicksightClientMock,
$this->getAwsQuicksightClientMockWithSuccessfulResponse(new Result()),
);

$userCollectionResponseTransfer = (new UserCollectionResponseTransfer())->addUser($userTransfer);

// Act
$userCollectionResponseTransfer = $this->tester->getFacade()
->createQuicksightUsersForUserTransfers($userCollectionResponseTransfer);
->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer);

// Assert
$this->assertCount(1, $userCollectionResponseTransfer->getErrors());
Expand All @@ -156,11 +154,31 @@ public function testShouldReturnErrorWhenQuicksightUserRegisterResponseDoesNotCo
}

/**
* @return \SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface|\PHPUnit\Framework\MockObject\MockObject
* @return void
*/
protected function getAwsQuicksightClientMock(): AmazonQuicksightToAwsQuicksightClientInterface
public function testShouldDoNothingWhenQuicksightUserRoleIsNotProvidedForUser(): void
{
return $this->getMockBuilder(AmazonQuicksightToAwsQuicksightClientInterface::class)->getMock();
// Arrange
$userTransfer = $this->tester->haveUser();

$awsQuicksightClientMock = $this->getAwsQuicksightClientMock();
$awsQuicksightClientMock->expects($this->never())->method('registerUser');
$this->tester->setDependency(
AmazonQuicksightDependencyProvider::AWS_QUICKSIGHT_CLIENT,
$awsQuicksightClientMock,
);

// Act
$userCollectionResponseTransfer = (new UserCollectionResponseTransfer())->addUser($userTransfer);

// Act
$userCollectionResponseTransfer = $this->tester->getFacade()
->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer);

// Assert
$this->assertCount(0, $userCollectionResponseTransfer->getErrors());
$this->assertCount(1, $userCollectionResponseTransfer->getUsers());
$this->assertNull($userCollectionResponseTransfer->getUsers()->getIterator()->current()->getQuicksightUser());
}

/**
Expand All @@ -182,6 +200,42 @@ protected function createRegisterUserSuccessfulResponse(UserTransfer $userTransf
return new Result($responseData);
}

/**
* @param \Aws\ResultInterface $result
*
* @return \SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected function getAwsQuicksightClientMockWithSuccessfulResponse(ResultInterface $result): AmazonQuicksightToAwsQuicksightClientInterface
{
$awsQuicksightClientMock = $this->getAwsQuicksightClientMock();
$awsQuicksightClientMock->method('registerUser')
->willReturn($result);

return $awsQuicksightClientMock;
}

/**
* @param \Aws\QuickSight\Exception\QuickSightException $quickSightException
*
* @return \SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected function getAwsQuicksightClientMockWithErrorResponse(QuickSightException $quickSightException): AmazonQuicksightToAwsQuicksightClientInterface
{
$awsQuicksightClientMock = $this->getAwsQuicksightClientMock();
$awsQuicksightClientMock->method('registerUser')
->willThrowException($quickSightException);

return $awsQuicksightClientMock;
}

/**
* @return \SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected function getAwsQuicksightClientMock(): AmazonQuicksightToAwsQuicksightClientInterface
{
return $this->getMockBuilder(AmazonQuicksightToAwsQuicksightClientInterface::class)->getMock();
}

/**
* @param string $message
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Generated\Shared\Transfer\QuicksightUserTransfer;
use Generated\Shared\Transfer\UserTransfer;
use Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUser;
use Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUserQuery;

/**
* Inherited Methods
Expand Down Expand Up @@ -65,4 +66,24 @@ public function haveUserWithNotPersistedQuicksightUserRole(string $quicksightUse
]))->build(),
]);
}

/**
* @param int $idUser
*
* @return \Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUser|null
*/
public function findQuicksightUserByIdUser(int $idUser): ?SpyQuicksightUser
{
return $this->getQuicksightUserQuery()
->filterByFkUser($idUser)
->findOne();
}

/**
* @return \Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUserQuery
*/
protected function getQuicksightUserQuery(): SpyQuicksightUserQuery
{
return SpyQuicksightUserQuery::create();
}
}

0 comments on commit bce9951

Please sign in to comment.