-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace HTTP Factory class #141
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,10 @@ | |||||
## Unreleased | ||||||
### Changed | ||||||
- Require PHP ^7.3 | ||||||
- Drop old version php-http/client-common ^1.6 | ||||||
|
||||||
### Added | ||||||
- Replace abandoned package php-http/message-factory. Using psr/http-factory instead. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## 4.1.0 - 2021-08-19 | ||||||
### Added | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,11 @@ | |
namespace Lmc\Matej\Http; | ||
|
||
use Fig\Http\Message\RequestMethodInterface; | ||
use Http\Mock\Client; | ||
use Http\Discovery\Psr18Client; | ||
use Lmc\Matej\Matej; | ||
use Lmc\Matej\Model\Request; | ||
use Lmc\Matej\Model\Response; | ||
use Lmc\Matej\UnitTestCase; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* @covers \Lmc\Matej\Http\RequestManager | ||
|
@@ -26,8 +26,29 @@ public function shouldSendAndDecodeRequest(): void | |
__DIR__ . '/Fixtures/response-one-successful-command.json' | ||
); | ||
|
||
$mockClient = new Client(); | ||
$mockClient->addResponse($dummyHttpResponse); | ||
$mockClient = $this->createMock(Psr18Client::class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the Mock client, which is implementation specifically made for unit tests, should be used instead. It is also already part of require-dev. The assertions are then more readable and understandable then defining mocks like this. See https://docs.php-http.org/en/latest/clients/mock-client.html |
||
$mockClient->expects($this->once()) | ||
->method('sendRequest') | ||
->with($this->callback(function (RequestInterface $request) { | ||
$this->assertMatchesRegularExpression( | ||
'~https\://account\-id\.matej\.lmc\.cz/foo/endpoint\?hmac_timestamp\=[0-9]+&hmac_sign\=[[:alnum:]]~', | ||
$request->getUri()->__toString() | ||
); | ||
$this->assertSame(RequestMethodInterface::METHOD_PUT, $request->getMethod()); | ||
$this->assertJsonStringEqualsJsonString( | ||
'{"foo":"bar","list":{"lorem":"ipsum","dolor":333}}', | ||
$request->getBody()->__toString() | ||
); | ||
$this->assertSame(['application/json'], $request->getHeader('Content-Type')); | ||
$this->assertSame(['custom-request-id'], $request->getHeader(RequestManager::REQUEST_ID_HEADER)); | ||
$this->assertSame( | ||
Matej::CLIENT_ID . '/' . Matej::VERSION, | ||
$request->getHeader(RequestManager::CLIENT_VERSION_HEADER)[0] | ||
); | ||
|
||
return true; | ||
})) | ||
->willReturn($dummyHttpResponse); | ||
|
||
$requestManager = new RequestManager('account-id', 'api-key'); | ||
$requestManager->setHttpClient($mockClient); | ||
|
@@ -41,24 +62,5 @@ public function shouldSendAndDecodeRequest(): void | |
|
||
// Response decoding is comprehensively tested in ResponseDecoderTest | ||
$requestManager->sendRequest($request); | ||
|
||
// Assert properties of the send request | ||
$recordedRequests = $mockClient->getRequests(); | ||
$this->assertCount(1, $recordedRequests); | ||
$this->assertMatchesRegularExpression( | ||
'~https\://account\-id\.matej\.lmc\.cz/foo/endpoint\?hmac_timestamp\=[0-9]+&hmac_sign\=[[:alnum:]]~', | ||
$recordedRequests[0]->getUri()->__toString() | ||
); | ||
$this->assertSame(RequestMethodInterface::METHOD_PUT, $recordedRequests[0]->getMethod()); | ||
$this->assertJsonStringEqualsJsonString( | ||
'{"foo":"bar","list":{"lorem":"ipsum","dolor":333}}', | ||
$recordedRequests[0]->getBody()->__toString() | ||
); | ||
$this->assertSame(['application/json'], $recordedRequests[0]->getHeader('Content-Type')); | ||
$this->assertSame(['custom-request-id'], $recordedRequests[0]->getHeader(RequestManager::REQUEST_ID_HEADER)); | ||
$this->assertSame( | ||
Matej::CLIENT_ID . '/' . Matej::VERSION, | ||
$recordedRequests[0]->getHeader(RequestManager::CLIENT_VERSION_HEADER)[0] | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
|
||
namespace Lmc\Matej; | ||
|
||
use Http\Mock\Client; | ||
use GuzzleHttp\Psr7\Request; | ||
use Http\Discovery\Psr18Client; | ||
use Lmc\Matej\Model\Command\ItemPropertySetup; | ||
use Lmc\Matej\Model\CommandResponse; | ||
|
||
|
@@ -25,8 +26,10 @@ public function shouldExecuteRequestViaBuilder(): void | |
__DIR__ . '/Http/Fixtures/response-one-successful-command.json' | ||
); | ||
|
||
$mockClient = new Client(); | ||
$mockClient->addResponse($dummyHttpResponse); | ||
$mockClient = $this->createMock(Psr18Client::class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dtto, use directly the mock client. |
||
$mockClient->expects($this->once()) | ||
->method('sendRequest') | ||
->willReturn($dummyHttpResponse); | ||
|
||
$matej = new Matej('account-id', 'apiKey'); | ||
$matej->setHttpClient($mockClient); | ||
|
@@ -36,12 +39,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()); | ||
|
@@ -58,8 +55,18 @@ public function shouldOverwriteBaseUrl(): 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') | ||
->with($this->callback(function (Request $request) { | ||
$this->assertStringStartsWith( | ||
'https://nobody.nowhere.com/account-id', | ||
$request->getUri()->__toString() | ||
); | ||
|
||
return true; | ||
})) | ||
->willReturn($dummyHttpResponse); | ||
|
||
$matej = new Matej('account-id', 'apiKey'); | ||
$matej->setHttpClient($mockClient); | ||
|
@@ -70,11 +77,5 @@ public function shouldOverwriteBaseUrl(): void | |
->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() | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.