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
8 changed files
with
238 additions
and
10 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
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
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
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,67 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Matej\RequestBuilder; | ||
|
||
use Fig\Http\Message\RequestMethodInterface; | ||
use Lmc\Matej\Exception\LogicException; | ||
use Lmc\Matej\Model\Command\AbstractCommand; | ||
use Lmc\Matej\Model\Command\Sorting; | ||
use Lmc\Matej\Model\Command\UserRecommendation; | ||
use Lmc\Matej\Model\Request; | ||
|
||
class CampaignRequestBuilder extends AbstractRequestBuilder | ||
{ | ||
protected const ENDPOINT_PATH = '/campaign'; | ||
|
||
/** @var AbstractCommand[] */ | ||
protected $commands = []; | ||
|
||
public function addRecommendation(UserRecommendation $recommendation): self | ||
{ | ||
$this->commands[] = $recommendation; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param UserRecommendation[] $recommendations | ||
* @return self | ||
*/ | ||
public function addRecommendations(array $recommendations): self | ||
{ | ||
foreach ($recommendations as $recommendation) { | ||
$this->addRecommendation($recommendation); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function addSorting(Sorting $sorting): self | ||
{ | ||
$this->commands[] = $sorting; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param Sorting[] $sortings | ||
* @return self | ||
*/ | ||
public function addSortings(array $sortings): self | ||
{ | ||
foreach ($sortings as $sorting) { | ||
$this->addSorting($sorting); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function build(): Request | ||
{ | ||
if (empty($this->commands)) { | ||
throw new LogicException('At least one command must be added to the builder before sending the request'); | ||
} | ||
|
||
return new Request(self::ENDPOINT_PATH, RequestMethodInterface::METHOD_POST, $this->commands); | ||
} | ||
} |
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
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
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,93 @@ | ||
<?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\Sorting; | ||
use Lmc\Matej\Model\Command\UserRecommendation; | ||
use Lmc\Matej\Model\Request; | ||
use Lmc\Matej\Model\Response; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Lmc\Matej\RequestBuilder\CampaignRequestBuilder | ||
* @covers \Lmc\Matej\RequestBuilder\AbstractRequestBuilder | ||
*/ | ||
class CampaignRequestBuilderTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function shouldThrowExceptionWhenBuildingEmptyCommands(): void | ||
{ | ||
$builder = new CampaignRequestBuilder(); | ||
|
||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage('At least one command must be added to the builder'); | ||
$builder->build(); | ||
} | ||
|
||
/** @test */ | ||
public function shouldBuildRequestWithCommands(): void | ||
{ | ||
$builder = new CampaignRequestBuilder(); | ||
|
||
$recommendationCommand1 = UserRecommendation::create('userId1', 1, 'scenario1', 1.0, 600); | ||
$recommendationCommand2 = UserRecommendation::create('userId2', 2, 'scenario2', 0.5, 700); | ||
$recommendationCommand3 = UserRecommendation::create('userId3', 3, 'scenario3', 0.0, 800); | ||
$builder->addRecommendation($recommendationCommand1); | ||
$builder->addRecommendations([$recommendationCommand2, $recommendationCommand3]); | ||
|
||
$sortingCommand1 = Sorting::create('userId1', ['itemId1', 'itemId2']); | ||
$sortingCommand2 = Sorting::create('userId2', ['itemId2', 'itemId3']); | ||
$sortingCommand3 = Sorting::create('userId3', ['itemId3', 'itemId4']); | ||
|
||
$builder->addSorting($sortingCommand1); | ||
$builder->addSortings([$sortingCommand2, $sortingCommand3]); | ||
|
||
$request = $builder->build(); | ||
|
||
$this->assertInstanceOf(Request::class, $request); | ||
$this->assertSame(RequestMethodInterface::METHOD_POST, $request->getMethod()); | ||
$this->assertSame('/campaign', $request->getPath()); | ||
|
||
$requestData = $request->getData(); | ||
$this->assertCount(6, $requestData); | ||
$this->assertContains($recommendationCommand1, $requestData); | ||
$this->assertContains($recommendationCommand2, $requestData); | ||
$this->assertContains($recommendationCommand3, $requestData); | ||
$this->assertContains($sortingCommand1, $requestData); | ||
$this->assertContains($sortingCommand2, $requestData); | ||
$this->assertContains($sortingCommand3, $requestData); | ||
} | ||
|
||
/** @test */ | ||
public function shouldThrowExceptionWhenSendingCommandsWithoutRequestManager(): void | ||
{ | ||
$builder = new CampaignRequestBuilder(); | ||
|
||
$builder->addSorting(Sorting::create('userId1', ['itemId1', 'itemId2'])); | ||
|
||
$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 CampaignRequestBuilder(); | ||
$builder->setRequestManager($requestManagerMock); | ||
|
||
$builder->addRecommendation(UserRecommendation::create('userId1', 1, 'scenario1', 1.0, 3600)); | ||
$builder->addSorting(Sorting::create('userId1', ['itemId1', 'itemId2'])); | ||
|
||
$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