Skip to content

Commit

Permalink
Allow schema (B, A -> B, B) for recommendation request (interaction u…
Browse files Browse the repository at this point in the history
…ser is same as target user on merge)
  • Loading branch information
Jakub Simon committed Apr 17, 2018
1 parent 005a37f commit 9d2b8bf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/RequestBuilder/RecommendationRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public function build(): Request

/**
* Assert that interaction user ids are ok:
* ([interaction], [user merge (source -> target)], [recommendation]):
* - (A, null, A)
* - (A, A -> ?, ?)
* - (B, A -> B, B)
*/
private function assertInteractionUserId(): void
{
Expand All @@ -77,6 +79,13 @@ private function assertInteractionUserId(): void
throw LogicException::forInconsistentUserId($this->userRecommendationCommand, $this->interactionCommand);
}

// allow (B, A -> B, B)
if ($this->userMergeCommand !== null
&& $interactionUserId === $this->userMergeCommand->getUserId()
&& $interactionUserId === $this->userRecommendationCommand->getUserId()) {
return;
}

// (A, A -> ?, ?)
if ($this->userMergeCommand !== null && $interactionUserId !== $this->userMergeCommand->getSourceUserId()) {
throw LogicException::forInconsistentUserMergeAndInteractionCommand(
Expand Down
34 changes: 27 additions & 7 deletions tests/unit/RequestBuilder/RecommendationRequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,21 @@ public function shouldThrowExceptionWhenMergeIsForUnrelatedUser(): void
* ([interaction], [user merge], [recommendation]): (A, A -> B, B)
*
* @test
* @dataProvider provideCorrectSequenceOfUsers
* @param string $interactionUser
* @param string $sourceUserToBeDeleted
* @param string $targetUserId
* @param string $recommendationUser
*/
public function shouldPassOnCorrectSequenceOfUsersWhenMerging(): void
{
$interactionCommand = Interaction::purchase('test-user-a', 'test-item-id');
$userMergeCommand = UserMerge::mergeFromSourceToTargetUser('test-user-a', 'test-user-b');
$recommendationsCommand = UserRecommendation::create('test-user-b', 5, 'scenario', 0.5, 3600);
public function shouldPassOnCorrectSequenceOfUsersWhenMerging(
string $interactionUser,
string $sourceUserToBeDeleted,
string $targetUserId,
string $recommendationUser
): void {
$interactionCommand = Interaction::purchase($interactionUser, 'test-item-id');
$userMergeCommand = UserMerge::mergeFromSourceToTargetUser($sourceUserToBeDeleted, $targetUserId);
$recommendationsCommand = UserRecommendation::create($recommendationUser, 5, 'scenario', 0.5, 3600);

$builder = new RecommendationRequestBuilder($recommendationsCommand);
$builder->setUserMerge($userMergeCommand);
Expand All @@ -127,15 +136,15 @@ public function shouldPassOnCorrectSequenceOfUsersWhenMerging(): void
}

/**
* ([interaction], [user merge], [recommendation]): (A, B -> A, A)
* ([interaction], [user merge], [recommendation]): (A, B -> A, B)
*
* @test
*/
public function shouldFailOnIncorrectSequenceOfUsersWhenMerging(): void
{
$interactionCommand = Interaction::purchase('test-user-a', 'test-item-id');
$userMergeCommand = UserMerge::mergeFromSourceToTargetUser('test-user-b', 'test-user-a');
$recommendationsCommand = UserRecommendation::create('test-user-a', 5, 'scenario', 0.5, 3600);
$recommendationsCommand = UserRecommendation::create('test-user-b', 5, 'scenario', 0.5, 3600);

$this->expectException(LogicException::class);
$this->expectExceptionMessage(
Expand All @@ -147,4 +156,15 @@ public function shouldFailOnIncorrectSequenceOfUsersWhenMerging(): void
$builder->setInteraction($interactionCommand);
$builder->build();
}

/**
* @return array[]
*/
public function provideCorrectSequenceOfUsers(): array
{
return [
'(A, A -> B, B)' => ['test-user-a', 'test-user-a', 'test-user-b', 'test-user-b'],
'(B, A -> B, B)' => ['test-user-b', 'test-user-a', 'test-user-b', 'test-user-b'],
];
}
}

0 comments on commit 9d2b8bf

Please sign in to comment.