Skip to content

Commit

Permalink
Use sorting command directly in the builder creation (fixes 41)
Browse files Browse the repository at this point in the history
  • Loading branch information
foglcz committed Nov 27, 2017
1 parent 5fa8565 commit f7f9843
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/RequestBuilder/RequestBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lmc\Matej\RequestBuilder;

use Lmc\Matej\Http\RequestManager;
use Lmc\Matej\Model\Command\Sorting;

/**
* Factory to create concrete RequestBuilder which helps you to create request for each Matej API
Expand Down Expand Up @@ -47,12 +48,11 @@ public function events(): EventsRequestBuilder
}

/**
* @param string[] $itemIds
* @return SortingRequestBuilder
*/
public function sorting(string $userId, array $itemIds): SortingRequestBuilder
public function sorting(Sorting $sorting): SortingRequestBuilder
{
return $this->createConfiguredBuilder(SortingRequestBuilder::class, $userId, $itemIds);
return $this->createConfiguredBuilder(SortingRequestBuilder::class, $sorting);
}

// TODO: builders for other endpoints
Expand Down
4 changes: 2 additions & 2 deletions src/RequestBuilder/SortingRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class SortingRequestBuilder extends AbstractRequestBuilder
/** @var Sorting */
private $sortingCommand;

public function __construct(string $userId, array $itemIds)
public function __construct(Sorting $sortingCommand)
{
$this->sortingCommand = Sorting::create($userId, $itemIds);
$this->sortingCommand = $sortingCommand;
}

public function addUserMerge(UserMerge $merge): self
Expand Down
11 changes: 6 additions & 5 deletions tests/IntegrationTests/SortingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lmc\Matej\IntegrationTests;

use Lmc\Matej\Model\Command\Interaction;
use Lmc\Matej\Model\Command\Sorting;
use Lmc\Matej\Model\Command\UserMerge;

/**
Expand All @@ -16,7 +17,7 @@ public function shouldIssueSortingRequestOnly(): void
{
$response = $this->createMatejInstance()
->request()
->sorting('integration-test-php-client-user-id-A', ['itemA', 'itemB', 'itemC'])
->sorting(Sorting::create('integration-test-php-client-user-id-A', ['itemA', 'itemB', 'itemC']))
->send();

$this->assertResponseCommandStatuses($response, 'SKIPPED', 'SKIPPED', 'OK');
Expand All @@ -27,7 +28,7 @@ public function shouldFailOnTooLittleItemIds(): void
{
$response = $this->createMatejInstance()
->request()
->sorting('integration-test-php-client-user-id-A', ['itemA'])
->sorting(Sorting::create('integration-test-php-client-user-id-A', ['itemA']))
->send();

$this->assertResponseCommandStatuses($response, 'SKIPPED', 'SKIPPED', 'OK');
Expand All @@ -38,7 +39,7 @@ public function shouldIssueSortingRequestWithInteraction(): void
{
$response = $this->createMatejInstance()
->request()
->sorting('integration-test-php-client-user-id-A', ['itemA', 'itemB', 'itemC'])
->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();

Expand All @@ -50,7 +51,7 @@ public function shouldIssueSortingRequestWithUserMerge(): void
{
$response = $this->createMatejInstance()
->request()
->sorting('integration-test-php-client-user-id-A', ['itemA', 'itemB', 'itemC'])
->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();

Expand All @@ -62,7 +63,7 @@ public function shouldIssueSortingRequestWithUserMergeAndInteraction(): void
{
$response = $this->createMatejInstance()
->request()
->sorting('integration-test-php-client-user-id-A', ['itemA', 'itemB', 'itemC'])
->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();
Expand Down
3 changes: 2 additions & 1 deletion tests/RequestBuilder/RequestBuilderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Lmc\Matej\Model\Command\Interaction;
use Lmc\Matej\Model\Command\ItemProperty;
use Lmc\Matej\Model\Command\ItemPropertySetup;
use Lmc\Matej\Model\Command\Sorting;
use Lmc\Matej\Model\Command\UserMerge;
use Lmc\Matej\Model\Request;
use Lmc\Matej\Model\Response;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function provideBuilderMethods(): array
['setupItemProperties', ItemPropertiesSetupRequestBuilder::class, $itemPropertiesSetupInit],
['deleteItemProperties', ItemPropertiesSetupRequestBuilder::class, $itemPropertiesSetupInit],
['events', EventsRequestBuilder::class, $eventInit],
['sorting', SortingRequestBuilder::class, $sortingInit, 'user-a', ['item-a', 'item-b', 'item-c']],
['sorting', SortingRequestBuilder::class, $sortingInit, Sorting::create('user-a', ['item-a', 'item-b', 'item-c'])],
];
}
}
6 changes: 3 additions & 3 deletions tests/RequestBuilder/SortingRequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SortingRequestBuilderTest extends TestCase
/** @test */
public function shouldBuildRequestWithCommands(): void
{
$builder = new SortingRequestBuilder('userId1', ['itemId1', 'itemId2']);
$builder = new SortingRequestBuilder(Sorting::create('userId1', ['itemId1', 'itemId2']));

$interactionCommand = Interaction::detailView('userId1', 'itemId1');
$builder->addInteraction($interactionCommand);
Expand All @@ -48,7 +48,7 @@ public function shouldBuildRequestWithCommands(): void
/** @test */
public function shouldThrowExceptionWhenSendingCommandsWithoutRequestManager(): void
{
$builder = new SortingRequestBuilder('userId1', ['itemId1', 'itemId2']);
$builder = new SortingRequestBuilder(Sorting::create('userId1', ['itemId1', 'itemId2']));

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Instance of RequestManager must be set to request builder');
Expand All @@ -64,7 +64,7 @@ public function shouldSendRequestViaRequestManager(): void
->with($this->isInstanceOf(Request::class))
->willReturn(new Response(0, 0, 0, 0));

$builder = new SortingRequestBuilder('userId1', ['itemId1', 'itemId2']);
$builder = new SortingRequestBuilder(Sorting::create('userId1', ['itemId1', 'itemId2']));
$builder->setRequestManager($requestManagerMock);

$builder->addInteraction(Interaction::detailView('userId1', 'itemId1'));
Expand Down

0 comments on commit f7f9843

Please sign in to comment.