Skip to content

Commit

Permalink
Merge 390dc1e into 7255ca7
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraM committed Nov 21, 2017
2 parents 7255ca7 + 390dc1e commit 7ab61e0
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/RequestBuilder/EventsRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\ItemProperty;
use Lmc\Matej\Model\Request;

class EventsRequestBuilder extends AbstractRequestBuilder
{
protected const ENDPOINT_PATH = '/events';

/** @var AbstractCommand[] */
protected $commands = [];

public function addItemProperty(ItemProperty $itemProperty): self
{
$this->commands[] = $itemProperty;

return $this;
}

/**
* @param ItemProperty[] $itemProperties
* @return self
*/
public function addItemProperties(array $itemProperties): self
{
foreach ($itemProperties as $itemProperty) {
$this->addItemProperty($itemProperty);
}

return $this;
}

// TODO: methods to addInteraction(s) and addUserMerge(s)

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);
}
}
5 changes: 5 additions & 0 deletions src/RequestBuilder/RequestBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public function itemPropertiesSetup(): ItemPropertiesSetupRequestBuilder
return $this->createConfiguredBuilder(ItemPropertiesSetupRequestBuilder::class);
}

public function events(): EventsRequestBuilder
{
return $this->createConfiguredBuilder(EventsRequestBuilder::class);
}

// TODO: builders for other endpoints

/**
Expand Down
76 changes: 76 additions & 0 deletions tests/RequestBuilder/EventsRequestBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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\ItemProperty;
use Lmc\Matej\Model\Request;
use Lmc\Matej\Model\Response;
use PHPUnit\Framework\TestCase;

class EventsRequestBuilderTest extends TestCase
{
/** @test */
public function shouldThrowExceptionWhenBuildingEmptyCommands(): void
{
$builder = new EventsRequestBuilder();

$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 EventsRequestBuilder();

$command1 = ItemProperty::create('id-1', ['key1' => 'value1']);
$command2 = ItemProperty::create('id-2', ['key1' => 'value3']);
$command3 = ItemProperty::create('id-3', ['key1' => 'value3']);

$builder->addItemProperty($command1);
$builder->addItemProperties([$command2, $command3]);

$request = $builder->build();

$this->assertInstanceOf(Request::class, $request);
$this->assertSame(RequestMethodInterface::METHOD_POST, $request->getMethod());
$this->assertSame('/events', $request->getPath());
$this->assertContainsOnlyInstancesOf(ItemProperty::class, $request->getData());
$this->assertSame($command1, $request->getData()[0]);
$this->assertSame($command2, $request->getData()[1]);
$this->assertSame($command3, $request->getData()[2]);
}

/** @test */
public function shouldThrowExceptionWhenSendingCommandsWithoutRequestManager(): void
{
$builder = new EventsRequestBuilder();

$builder->addItemProperty(ItemProperty::create('id-1', ['key1' => 'value1']));

$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 EventsRequestBuilder();
$builder->setRequestManager($requestManagerMock);

$builder->addItemProperty(ItemProperty::create('id-1', ['key1' => 'value1']));

$builder->send();
}
}
6 changes: 6 additions & 0 deletions tests/RequestBuilder/RequestBuilderFactoryTest.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\ItemProperty;
use Lmc\Matej\Model\Command\ItemPropertySetup;
use Lmc\Matej\Model\Request;
use Lmc\Matej\Model\Response;
Expand Down Expand Up @@ -52,8 +53,13 @@ public function provideBuilderMethods(): array
$builder->addProperty(ItemPropertySetup::timestamp('valid_from'));
};

$eventInit = function (EventsRequestBuilder $builder): void {
$builder->addItemProperty(ItemProperty::create('item-id', []));
};

return [
['itemPropertiesSetup', ItemPropertiesSetupRequestBuilder::class, $itemPropertiesSetupInit],
['events', EventsRequestBuilder::class, $eventInit],
];
}
}

0 comments on commit 7ab61e0

Please sign in to comment.