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) (fixes #63)
  • Loading branch information
Jakub Simon authored and OndraM committed Apr 17, 2018
1 parent 88ed4ab commit 51f465a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
### Added
- Type hints and type assertions relevant only for PHP 5 version removed from codebase and are now added only to PHP 5 version of the library (in `RequestBuilderFactory`, `Command\Interaction`, `Command\UserRecommendation`).

### Changed
- Allow users of commands passed to `$matej->request()->recommendation()` to be: (B [interaction], A -> B [user merge], B [recommendation]) in accordance with new Matej API behavior.

## 1.4.0 - 2018-02-23
### Added
- Endpoint to reset Matej test-databases (`$matej->request()->resetDatabase()`).
Expand Down
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 51f465a

Please sign in to comment.