-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
204 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Matej\RequestBuilder; | ||
|
||
use Fig\Http\Message\RequestMethodInterface; | ||
use Lmc\Matej\Model\Command\Interaction; | ||
use Lmc\Matej\Model\Command\UserMerge; | ||
use Lmc\Matej\Model\Command\UserRecommendation; | ||
use Lmc\Matej\Model\Request; | ||
|
||
class RecommendationsRequestBuilder extends AbstractRequestBuilder | ||
{ | ||
protected const ENDPOINT_PATH = '/recommendations'; | ||
|
||
/** @var Interaction|null */ | ||
private $interactionCommand; | ||
/** @var UserMerge|null */ | ||
private $userMergeCommand; | ||
/** @var UserRecommendation */ | ||
private $userRecommendationCommand; | ||
|
||
public function __construct(UserRecommendation $userRecommendationCommand) | ||
{ | ||
$this->userRecommendationCommand = $userRecommendationCommand; | ||
} | ||
|
||
public function addUserMerge(UserMerge $merge): self | ||
{ | ||
$this->userMergeCommand = $merge; | ||
|
||
return $this; | ||
} | ||
|
||
public function addInteraction(Interaction $interaction): self | ||
{ | ||
$this->interactionCommand = $interaction; | ||
|
||
return $this; | ||
} | ||
|
||
public function build(): Request | ||
{ | ||
return new Request( | ||
self::ENDPOINT_PATH, | ||
RequestMethodInterface::METHOD_POST, | ||
[$this->interactionCommand, $this->userMergeCommand, $this->userRecommendationCommand] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
tests/IntegrationTests/RecommendationsRequestBuilderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Matej\IntegrationTests; | ||
|
||
use Lmc\Matej\Model\Command\Interaction; | ||
use Lmc\Matej\Model\Command\UserMerge; | ||
use Lmc\Matej\Model\Command\UserRecommendation; | ||
use Lmc\Matej\RequestBuilder\RecommendationsRequestBuilder; | ||
|
||
/** | ||
* @covers ItemPropertiesSetupRequestBuilder | ||
* @covers EventsRequestBuilder | ||
*/ | ||
class RecommendationsRequestBuilderTest extends IntegrationTestCase | ||
{ | ||
/** @test */ | ||
public function shouldExecuteRecommendationRequestOnly(): void | ||
{ | ||
$response = $this->createRecommendationRequestBuilder() | ||
->send(); | ||
|
||
$this->assertResponseCommandStatuses($response, 'SKIPPED', 'SKIPPED', 'OK'); | ||
} | ||
|
||
/** @test */ | ||
public function shouldExecuteRecommendationRequestWithInteraction(): void | ||
{ | ||
$response = $this->createRecommendationRequestBuilder() | ||
->addInteraction(Interaction::bookmark('integration-test-php-client-user-id-A', 'itemA')) | ||
->send(); | ||
|
||
$this->assertResponseCommandStatuses($response, 'OK', 'SKIPPED', 'OK'); | ||
} | ||
|
||
/** @test */ | ||
public function shouldExecuteRecommendationRequestWithUserMerge(): void | ||
{ | ||
$response = $this->createRecommendationRequestBuilder() | ||
->addUserMerge(UserMerge::mergeInto('integration-test-php-client-user-id-A', 'integration-test-php-client-user-id-B')) | ||
->send(); | ||
|
||
$this->assertResponseCommandStatuses($response, 'SKIPPED', 'OK', 'OK'); | ||
} | ||
|
||
/** @test */ | ||
public function shouldExecuteRecommendationRequestWithUserMergeAndInteraction(): void | ||
{ | ||
$response = $this->createRecommendationRequestBuilder() | ||
->addUserMerge(UserMerge::mergeInto('integration-test-php-client-user-id-A', 'integration-test-php-client-user-id-B')) | ||
->addInteraction(Interaction::bookmark('integration-test-php-client-user-id-A', 'itemA')) | ||
->send(); | ||
|
||
$this->assertResponseCommandStatuses($response, 'OK', 'OK', 'OK'); | ||
} | ||
|
||
private function createRecommendationRequestBuilder(): RecommendationsRequestBuilder | ||
{ | ||
return $this->createMatejInstance() | ||
->request() | ||
->recommendations(UserRecommendation::create( | ||
'integration-test-php-client-user-id-A', | ||
5, | ||
'integration-test-scenario', | ||
0.50, | ||
3600 | ||
)) | ||
; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
tests/RequestBuilder/RecommendationsRequestBuilderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Matej\RequestBuilder; | ||
|
||
use Fig\Http\Message\RequestMethodInterface; | ||
use Lmc\Matej\Exception\LogicException; | ||
use Lmc\Matej\Http\RequestManager; | ||
use Lmc\Matej\Model\Command\Interaction; | ||
use Lmc\Matej\Model\Command\Sorting; | ||
use Lmc\Matej\Model\Command\UserMerge; | ||
use Lmc\Matej\Model\Command\UserRecommendation; | ||
use Lmc\Matej\Model\Request; | ||
use Lmc\Matej\Model\Response; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Lmc\Matej\RequestBuilder\RecommendationsRequestBuilder | ||
* @covers \Lmc\Matej\RequestBuilder\AbstractRequestBuilder | ||
*/ | ||
class RecommendationsRequestBuilderTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function shouldBuildRequestWithCommands(): void | ||
{ | ||
$recommendationsCommand = UserRecommendation::create('userId1', 5, 'test-scenario', 0.5, 3600); | ||
$builder = new RecommendationsRequestBuilder($recommendationsCommand); | ||
|
||
$interactionCommand = Interaction::detailView('userId1', 'itemId1'); | ||
$builder->addInteraction($interactionCommand); | ||
|
||
$userMergeCommand = UserMerge::mergeFromSourceToTargetUser('sourceId1', 'targetId1'); | ||
$builder->addUserMerge($userMergeCommand); | ||
|
||
$request = $builder->build(); | ||
|
||
$this->assertInstanceOf(Request::class, $request); | ||
$this->assertSame(RequestMethodInterface::METHOD_POST, $request->getMethod()); | ||
$this->assertSame('/recommendations', $request->getPath()); | ||
|
||
$requestData = $request->getData(); | ||
$this->assertCount(3, $requestData); | ||
$this->assertSame($interactionCommand, $requestData[0]); | ||
$this->assertSame($userMergeCommand, $requestData[1]); | ||
$this->assertSame($recommendationsCommand, $requestData[2]); | ||
} | ||
|
||
/** @test */ | ||
public function shouldThrowExceptionWhenSendingCommandsWithoutRequestManager(): void | ||
{ | ||
$recommendationsCommand = UserRecommendation::create('userId1', 5, 'test-scenario', 0.5, 3600); | ||
$builder = new RecommendationsRequestBuilder($recommendationsCommand); | ||
|
||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage('Instance of RequestManager must be set to request builder'); | ||
$builder->send(); | ||
} | ||
|
||
/** @test */ | ||
public function shouldSendRequestViaRequestManager(): void | ||
{ | ||
$requestManagerMock = $this->createMock(RequestManager::class); | ||
$requestManagerMock->expects($this->once()) | ||
->method('sendRequest') | ||
->with($this->isInstanceOf(Request::class)) | ||
->willReturn(new Response(0, 0, 0, 0)); | ||
|
||
$builder = new SortingRequestBuilder(Sorting::create('userId1', ['itemId1', 'itemId2'])); | ||
$builder->setRequestManager($requestManagerMock); | ||
$builder->send(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters