Skip to content

Commit

Permalink
Simplify testing of recommendation request.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubTesarek committed Sep 24, 2020
1 parent a3612e0 commit 7ebb21b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/Model/CommandResponse.php
Expand Up @@ -67,4 +67,4 @@ public function isSuccessful(): bool
{
return $this->getStatus() === static::STATUS_OK || $this->getStatus() === static::STATUS_SKIPPED;
}
}
}
9 changes: 8 additions & 1 deletion src/Model/RecommendationCommandResponse.php
Expand Up @@ -4,9 +4,16 @@

class RecommendationCommandResponse extends CommandResponse
{
/**
* Return recommendations data.
*/
public function getData(): array
{
$data = [];
/* If Matej returns flat structure response `['id1', 'id2']`, convert it
to complex structure `[['item_id': 'id1'], ['item_id': 'id2']]`. This
ensures `RecommendationCommandResponse::getData` returns data in
consistent format. */
foreach ($this->data as $key => $val) {
if (is_string($val)) {
$val = (object) ['item-id' => $val];
Expand All @@ -16,4 +23,4 @@ public function getData(): array

return $data;
}
}
}
2 changes: 1 addition & 1 deletion src/Model/Response/RecommendationsResponse.php
Expand Up @@ -31,7 +31,7 @@ protected function decodeRawCommandResponses(array $commandResponses): array
{
$decodedResponses = [];
foreach ($commandResponses as $index => $rawCommandResponse) {
if ($index == static::RECOMMENDATION_INDEX) {
if ($index === static::RECOMMENDATION_INDEX) {
$decodedResponses[] = RecommendationCommandResponse::createFromRawCommandResponseObject($rawCommandResponse);
} else {
$decodedResponses[] = CommandResponse::createFromRawCommandResponseObject($rawCommandResponse);
Expand Down
62 changes: 14 additions & 48 deletions tests/unit/Model/Response/RecommendationsResponseTest.php
Expand Up @@ -7,8 +7,11 @@

class RecommendationsResponseTest extends UnitTestCase
{
/** @test */
public function shouldBeInstantiable(): void
/**
* @test
* @dataProvider provideRecommendationResponseData
*/
public function shouldBeInstantiable(array $recommendationResponseData): void
{
$interactionCommandResponse = (object) [
'status' => CommandResponse::STATUS_OK,
Expand All @@ -23,7 +26,7 @@ public function shouldBeInstantiable(): void
$recommendationCommandResponse = (object) [
'status' => CommandResponse::STATUS_OK,
'message' => 'MOCK_RECOMMENDATION_MESSAGE',
'data' => ['MOCK' => 'RECOMMENDATION'],
'data' => $recommendationResponseData,
];

$response = new RecommendationsResponse(3, 3, 0, 0, [$interactionCommandResponse, $userMergeCommandResponse, $recommendationCommandResponse]);
Expand All @@ -42,57 +45,20 @@ public function shouldBeInstantiable(): void
$this->assertSame('MOCK_USER_MERGE_MESSAGE', $response->getUserMerge()->getMessage());
$this->assertSame(['MOCK' => 'USER_MERGE'], $response->getUserMerge()->getData());

$this->assertSame('MOCK_RECOMMENDATION_MESSAGE', $response->getRecommendation()->getMessage());
$this->assertEquals(['MOCK' => (object) ['item-id' => 'RECOMMENDATION']], $response->getRecommendation()->getData());
}

/** @test */
public function shouldReturnListOfObjectsWithComplexResponseTest(): void
{
$interactionCommandResponse = (object) [
'status' => CommandResponse::STATUS_OK,
'message' => 'MOCK_INTERACTION_MESSAGE',
'data' => ['MOCK' => 'INTERACTION'],
];
$userMergeCommandResponse = (object) [
'status' => CommandResponse::STATUS_OK,
'message' => 'MOCK_USER_MERGE_MESSAGE',
'data' => ['MOCK' => 'USER_MERGE'],
];
$recommendationCommandResponse = (object) [
'status' => CommandResponse::STATUS_OK,
'message' => 'MOCK_RECOMMENDATION_MESSAGE',
'data' => [(object) ['item-id' => 'MOCK_ITEM_ID']],
];

$response = new RecommendationsResponse(3, 3, 0, 0, [$interactionCommandResponse, $userMergeCommandResponse, $recommendationCommandResponse]);
$this->assertTrue($response->getRecommendation()->isSuccessful());
$this->assertSame('MOCK_RECOMMENDATION_MESSAGE', $response->getRecommendation()->getMessage());
$this->assertEquals([(object) ['item-id' => 'MOCK_ITEM_ID']], $response->getRecommendation()->getData());
}

/** @test */
public function shouldReturnListOfObjectsWithFlatResponseTest(): void
public function provideRecommendationResponseData(): array
{
$interactionCommandResponse = (object) [
'status' => CommandResponse::STATUS_OK,
'message' => 'MOCK_INTERACTION_MESSAGE',
'data' => ['MOCK' => 'INTERACTION'],
];
$userMergeCommandResponse = (object) [
'status' => CommandResponse::STATUS_OK,
'message' => 'MOCK_USER_MERGE_MESSAGE',
'data' => ['MOCK' => 'USER_MERGE'],
return [
'complex response data' => [
[(object) ['item-id' => 'MOCK_ITEM_ID']],
],
'flat response data' => [
['MOCK_ITEM_ID'],
],
];
$recommendationCommandResponse = (object) [
'status' => CommandResponse::STATUS_OK,
'message' => 'MOCK_RECOMMENDATION_MESSAGE',
'data' => ['MOCK_ITEM_ID'],
];

$response = new RecommendationsResponse(3, 3, 0, 0, [$interactionCommandResponse, $userMergeCommandResponse, $recommendationCommandResponse]);
$this->assertTrue($response->getRecommendation()->isSuccessful());
$this->assertSame('MOCK_RECOMMENDATION_MESSAGE', $response->getRecommendation()->getMessage());
$this->assertEquals([(object) ['item-id' => 'MOCK_ITEM_ID']], $response->getRecommendation()->getData());
}
}

0 comments on commit 7ebb21b

Please sign in to comment.