Skip to content

Commit

Permalink
Merge 6dc9181 into 76e24ba
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraM authored Nov 22, 2017
2 parents 76e24ba + 6dc9181 commit ab66ae1
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Http/ResponseDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function decode(ResponseInterface $httpResponse): Response
{
$responseData = json_decode($httpResponse->getBody()->getContents());

if (JSON_ERROR_NONE !== json_last_error()) {
if (json_last_error() !== JSON_ERROR_NONE) {
throw ResponseDecodingException::forJsonError(json_last_error_msg(), $httpResponse);
}

Expand Down
48 changes: 45 additions & 3 deletions src/Matej.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,61 @@

namespace Lmc\Matej;

use Http\Client\HttpClient;
use Http\Message\MessageFactory;
use Lmc\Matej\Http\RequestManager;
use Lmc\Matej\Http\ResponseDecoderInterface;
use Lmc\Matej\RequestBuilder\RequestBuilderFactory;

class Matej
{
public const CLIENT_ID = 'php-client';
public const VERSION = '0.0.0';

/** @var string */
private $clientId;
private $accountId;
/** @var string */
private $apiKey;
/** @var RequestManager */
protected $requestManager;

public function __construct(string $clientId, string $apiKey)
public function __construct(string $accountId, string $apiKey)
{
$this->clientId = $clientId;
$this->accountId = $accountId;
$this->apiKey = $apiKey;
$this->requestManager = new RequestManager($accountId, $apiKey);
}

public function request(): RequestBuilderFactory
{
return new RequestBuilderFactory($this->getRequestManager());
}

public function setHttpClient(HttpClient $client): self
{
$this->getRequestManager()->setHttpClient($client);

return $this;
}

/** @codeCoverageIgnore */
public function setHttpMessageFactory(MessageFactory $messageFactory): self
{
$this->getRequestManager()->setMessageFactory($messageFactory);

return $this;
}

/** @codeCoverageIgnore */
public function setHttpResponseDecoder(ResponseDecoderInterface $responseDecoder): self
{
$this->getRequestManager()->setResponseDecoder($responseDecoder);

return $this;
}

protected function getRequestManager(): RequestManager
{
return $this->requestManager;
}
}
51 changes: 51 additions & 0 deletions tests/MatejTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

namespace Lmc\Matej;

use Http\Mock\Client;
use Lmc\Matej\Model\Command\ItemPropertySetup;
use Lmc\Matej\Model\CommandResponse;

/**
* @covers \Lmc\Matej\Matej
*/
class MatejTest extends TestCase
{
/** @test */
public function shouldBeInstantiable(): void
{
$matej = new Matej('accountId', 'apiKey');
$this->assertInstanceOf(Matej::class, $matej);
}

/** @test */
public function shouldExecuteRequestViaBuilder(): void
{
$dummyHttpResponse = $this->createJsonResponseFromFile(__DIR__ . '/Http/Fixtures/response-one-successful-command.json');

$mockClient = new Client();
$mockClient->addResponse($dummyHttpResponse);

$matej = new Matej('accountId', 'apiKey');
$matej->setHttpClient($mockClient);

$response = $matej->request()
->setupItemProperties()
->addProperty(ItemPropertySetup::timestamp('valid_from'))
->send();

$this->assertCount(1, $mockClient->getRequests());
$this->assertStringStartsWith(
'https://accountid.matej.lmc.cz/',
$mockClient->getRequests()[0]->getUri()->__toString()
);

$this->assertSame(1, $response->getNumberOfCommands());
$this->assertSame(1, $response->getNumberOfSuccessfulCommands());
$this->assertSame(0, $response->getNumberOfSkippedCommands());
$this->assertSame(0, $response->getNumberOfFailedCommands());
$this->assertCount(1, $response->getCommandResponses());
$this->assertInstanceOf(CommandResponse::class, $response->getCommandResponses()[0]);
$this->assertSame(CommandResponse::STATUS_OK, $response->getCommandResponses()[0]->getStatus());
}
}

0 comments on commit ab66ae1

Please sign in to comment.