Skip to content

Commit

Permalink
Merge f56981a into 6820eca
Browse files Browse the repository at this point in the history
  • Loading branch information
foglcz committed Nov 27, 2017
2 parents 6820eca + f56981a commit 1009ee3
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 6 deletions.
9 changes: 9 additions & 0 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 @@ -46,6 +47,14 @@ public function events(): EventsRequestBuilder
return $this->createConfiguredBuilder(EventsRequestBuilder::class);
}

/**
* @return SortingRequestBuilder
*/
public function sorting(Sorting $sorting): SortingRequestBuilder
{
return $this->createConfiguredBuilder(SortingRequestBuilder::class, $sorting);
}

// TODO: builders for other endpoints

/**
Expand Down
49 changes: 49 additions & 0 deletions src/RequestBuilder/SortingRequestBuilder.php
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]
);
}
}
14 changes: 14 additions & 0 deletions tests/IntegrationTests/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lmc\Matej\IntegrationTests;

use Lmc\Matej\Matej;
use Lmc\Matej\Model\Response;
use Lmc\Matej\TestCase;

class IntegrationTestCase extends TestCase
Expand All @@ -29,4 +30,17 @@ protected function createMatejInstance(): Matej

return $instance;
}

protected function assertResponseCommandStatuses(Response $response, ...$expectedCommandStatuses): void
{
$this->assertSame(count($expectedCommandStatuses), $response->getNumberOfCommands());
$this->assertSame(count(array_intersect($expectedCommandStatuses, ['OK'])), $response->getNumberOfSuccessfulCommands());
$this->assertSame(count(array_intersect($expectedCommandStatuses, ['ERROR'])), $response->getNumberOfFailedCommands());
$this->assertSame(count(array_intersect($expectedCommandStatuses, ['SKIPPED'])), $response->getNumberOfSkippedCommands());

$commandResponses = $response->getCommandResponses();
foreach ($expectedCommandStatuses as $key => $expectedStatus) {
$this->assertSame($expectedStatus, $commandResponses[$key]->getStatus());
}
}
}
8 changes: 4 additions & 4 deletions tests/IntegrationTests/ItemPropertiesSetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ItemPropertiesSetupTest extends IntegrationTestCase
*
* @test
*/
public function shouldIssueEventWithUnknownPropertyButPassOk(): void
public function shouldExecuteEventWithUnknownPropertyButPassOk(): void
{
$response = $this->createMatejInstance()
->request()
Expand All @@ -42,7 +42,7 @@ public function shouldIssueEventWithUnknownPropertyButPassOk(): void

/**
* @test
* @depends shouldIssueEventWithUnknownPropertyButPassOk
* @depends shouldExecuteEventWithUnknownPropertyButPassOk
*/
public function shouldCreateNewPropertiesInMatej(): void
{
Expand All @@ -69,7 +69,7 @@ public function shouldCreateNewPropertiesInMatej(): void
* @test
* @depends shouldCreateNewPropertiesInMatej
*/
public function shouldIssueEventWithJustCreatedProperties(): void
public function shouldExecuteEventWithJustCreatedProperties(): void
{
$response = $this->createMatejInstance()
->request()
Expand Down Expand Up @@ -97,7 +97,7 @@ public function shouldIssueEventWithJustCreatedProperties(): void

/**
* @test
* @depends shouldIssueEventWithJustCreatedProperties
* @depends shouldExecuteEventWithJustCreatedProperties
*/
public function shouldDeleteCreatedPropertiesFromMatej(): void
{
Expand Down
73 changes: 73 additions & 0 deletions tests/IntegrationTests/SortingRequestTest.php
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');
}
}
10 changes: 8 additions & 2 deletions tests/RequestBuilder/RequestBuilderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Lmc\Matej\Http\RequestManager;
use Lmc\Matej\Model\Command\ItemProperty;
use Lmc\Matej\Model\Command\ItemPropertySetup;
use Lmc\Matej\Model\Command\Sorting;
use Lmc\Matej\Model\Request;
use Lmc\Matej\Model\Response;
use PHPUnit\Framework\TestCase;
Expand All @@ -21,7 +22,8 @@ class RequestBuilderFactoryTest extends TestCase
public function shouldInstantiateBuilderToBuildAndSendRequest(
string $factoryMethod,
string $expectedBuilderClass,
\Closure $minimalBuilderInit
\Closure $minimalBuilderInit,
...$factoryArguments
): void {
$requestManagerMock = $this->createMock(RequestManager::class);
$requestManagerMock->expects($this->once())
Expand All @@ -32,7 +34,7 @@ public function shouldInstantiateBuilderToBuildAndSendRequest(
$factory = new RequestBuilderFactory($requestManagerMock);

/** @var AbstractRequestBuilder $builder */
$builder = $factory->$factoryMethod();
$builder = $factory->$factoryMethod(...$factoryArguments);

// Builders may require some minimal setup to be able to execute the build() method
$minimalBuilderInit($builder);
Expand All @@ -57,10 +59,14 @@ public function provideBuilderMethods(): array
$builder->addItemProperty(ItemProperty::create('item-id', []));
};

$sortingInit = function (SortingRequestBuilder $builder): void {
};

return [
['setupItemProperties', ItemPropertiesSetupRequestBuilder::class, $itemPropertiesSetupInit],
['deleteItemProperties', ItemPropertiesSetupRequestBuilder::class, $itemPropertiesSetupInit],
['events', EventsRequestBuilder::class, $eventInit],
['sorting', SortingRequestBuilder::class, $sortingInit, Sorting::create('user-a', ['item-a', 'item-b', 'item-c'])],
];
}
}
69 changes: 69 additions & 0 deletions tests/RequestBuilder/SortingRequestBuilderTest.php
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();
}
}

0 comments on commit 1009ee3

Please sign in to comment.