-
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
7 changed files
with
226 additions
and
6 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
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\Sorting; | ||
use Lmc\Matej\Model\Command\UserMerge; | ||
use Lmc\Matej\Model\Request; | ||
|
||
class SortingRequestBuilder extends AbstractRequestBuilder | ||
{ | ||
protected const ENDPOINT_PATH = '/sorting'; | ||
|
||
/** @var Interaction|null */ | ||
private $interactionCommand; | ||
/** @var UserMerge|null */ | ||
private $userMergeCommand; | ||
/** @var Sorting */ | ||
private $sortingCommand; | ||
|
||
public function __construct(Sorting $sortingCommand) | ||
{ | ||
$this->sortingCommand = $sortingCommand; | ||
} | ||
|
||
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->sortingCommand] | ||
); | ||
} | ||
} |
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,73 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Matej\IntegrationTests; | ||
|
||
use Lmc\Matej\Model\Command\Interaction; | ||
use Lmc\Matej\Model\Command\Sorting; | ||
use Lmc\Matej\Model\Command\UserMerge; | ||
|
||
/** | ||
* @covers ItemPropertiesSetupRequestBuilder | ||
* @covers EventsRequestBuilder | ||
*/ | ||
class SortingRequestTest extends IntegrationTestCase | ||
{ | ||
/** @test */ | ||
public function shouldExecuteSortingRequestOnly(): void | ||
{ | ||
$response = $this->createMatejInstance() | ||
->request() | ||
->sorting(Sorting::create('integration-test-php-client-user-id-A', ['itemA', 'itemB', 'itemC'])) | ||
->send(); | ||
|
||
$this->assertResponseCommandStatuses($response, 'SKIPPED', 'SKIPPED', 'OK'); | ||
} | ||
|
||
/** @test */ | ||
public function shouldFailOnTooLittleItemIds(): void | ||
{ | ||
$response = $this->createMatejInstance() | ||
->request() | ||
->sorting(Sorting::create('integration-test-php-client-user-id-A', ['itemA'])) | ||
->send(); | ||
|
||
$this->assertResponseCommandStatuses($response, 'SKIPPED', 'SKIPPED', 'OK'); | ||
} | ||
|
||
/** @test */ | ||
public function shouldExecuteSortingRequestWithInteraction(): void | ||
{ | ||
$response = $this->createMatejInstance() | ||
->request() | ||
->sorting(Sorting::create('integration-test-php-client-user-id-A', ['itemA', 'itemB', 'itemC'])) | ||
->addInteraction(Interaction::bookmark('integration-test-php-client-user-id-A', 'itemA')) | ||
->send(); | ||
|
||
$this->assertResponseCommandStatuses($response, 'OK', 'SKIPPED', 'OK'); | ||
} | ||
|
||
/** @test */ | ||
public function shouldExecuteSortingRequestWithUserMerge(): void | ||
{ | ||
$response = $this->createMatejInstance() | ||
->request() | ||
->sorting(Sorting::create('integration-test-php-client-user-id-A', ['itemA', 'itemB', 'itemC'])) | ||
->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 shouldExecuteSortingRequestWithUserMergeAndInteraction(): void | ||
{ | ||
$response = $this->createMatejInstance() | ||
->request() | ||
->sorting(Sorting::create('integration-test-php-client-user-id-A', ['itemA', 'itemB', 'itemC'])) | ||
->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'); | ||
} | ||
} |
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,69 @@ | ||
<?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\Request; | ||
use Lmc\Matej\Model\Response; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Lmc\Matej\RequestBuilder\SortingRequestBuilder | ||
* @covers \Lmc\Matej\RequestBuilder\AbstractRequestBuilder | ||
*/ | ||
class SortingRequestBuilderTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function shouldBuildRequestWithCommands(): void | ||
{ | ||
$sortingCommand = Sorting::create('userId1', ['itemId1', 'itemId2']); | ||
$builder = new SortingRequestBuilder($sortingCommand); | ||
|
||
$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('/sorting', $request->getPath()); | ||
|
||
$requestData = $request->getData(); | ||
$this->assertCount(3, $requestData); | ||
$this->assertSame($interactionCommand, $requestData[0]); | ||
$this->assertSame($userMergeCommand, $requestData[1]); | ||
$this->assertSame($sortingCommand, $requestData[2]); | ||
} | ||
|
||
/** @test */ | ||
public function shouldThrowExceptionWhenSendingCommandsWithoutRequestManager(): void | ||
{ | ||
$builder = new SortingRequestBuilder(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 SortingRequestBuilder(Sorting::create('userId1', ['itemId1', 'itemId2'])); | ||
$builder->setRequestManager($requestManagerMock); | ||
$builder->send(); | ||
} | ||
} |