Skip to content

Commit

Permalink
fixup! Replace HTTP Factory class
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubpilaralmamedia committed Jan 15, 2024
1 parent ad69991 commit 1060f68
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 38 deletions.
4 changes: 3 additions & 1 deletion src/Http/RequestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ protected function createConfiguredHttpClient(): ClientInterface

protected function createHttpRequestFromMatejRequest(Request $request): RequestInterface
{
$requestBody = $this->getStreamFactory()->createStream(json_encode($request->getData())); // TODO: use \Safe\json_encode
$requestBody = $this->getStreamFactory()->createStream(
json_encode($request->getData())

Check failure on line 144 in src/Http/RequestManager.php

View workflow job for this annotation

GitHub Actions / Code style and static analysis

Parameter #1 $content of method Psr\Http\Message\StreamFactoryInterface::createStream() expects string, string|false given.
); // TODO: use \Safe\json_encode
$uri = $this->buildBaseUrl() . $request->getPath();

return $this->getMessageFactory()
Expand Down
2 changes: 1 addition & 1 deletion src/Matej.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Lmc\Matej;

use Psr\Http\Client\ClientInterface;
use Lmc\Matej\Http\RequestManager;
use Lmc\Matej\Http\ResponseDecoderInterface;
use Lmc\Matej\RequestBuilder\RequestBuilderFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

Expand Down
41 changes: 5 additions & 36 deletions tests/unit/MatejTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Lmc\Matej;

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

Expand All @@ -25,8 +25,10 @@ public function shouldExecuteRequestViaBuilder(): void
__DIR__ . '/Http/Fixtures/response-one-successful-command.json'
);

$mockClient = new Client();
$mockClient->addResponse($dummyHttpResponse);
$mockClient = $this->createMock(Psr18Client::class);
$mockClient->expects($this->once())
->method('sendRequest')
->willReturn($dummyHttpResponse);

$matej = new Matej('account-id', 'apiKey');
$matej->setHttpClient($mockClient);
Expand All @@ -36,12 +38,6 @@ public function shouldExecuteRequestViaBuilder(): void
->addProperty(ItemPropertySetup::timestamp('valid_from'))
->send();

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

$this->assertSame(1, $response->getNumberOfCommands());
$this->assertSame(1, $response->getNumberOfSuccessfulCommands());
$this->assertSame(0, $response->getNumberOfSkippedCommands());
Expand All @@ -50,31 +46,4 @@ public function shouldExecuteRequestViaBuilder(): void
$this->assertInstanceOf(CommandResponse::class, $response->getCommandResponses()[0]);
$this->assertSame(CommandResponse::STATUS_OK, $response->getCommandResponses()[0]->getStatus());
}

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

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

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

$matej->setBaseUrl('https://nobody.nowhere.com/%s');

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

$this->assertCount(1, $mockClient->getRequests());
$this->assertStringStartsWith(
'https://nobody.nowhere.com/account-id',
$mockClient->getRequests()[0]->getUri()->__toString()
);
}
}

0 comments on commit 1060f68

Please sign in to comment.