Skip to content

Commit

Permalink
feat(FioRequestFactory): more guzzle independent
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Jun 21, 2024
1 parent a2dcd9c commit 1bd3ab7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ composer.lock
/tests/**/output/
tests/src/E2E/account.ini
coverage.html
/tests/src/E2E/test.http
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"ext-simplexml": "*",
"ext-xmlwriter": "*",
"guzzlehttp/psr7": "^2.4",
"h4kuna/dir": "^0.1.2",
"h4kuna/dir": "^0.1.3",
"h4kuna/memoize": "^0.1.3",
"nette/safe-stream": "^3.0",
"nette/utils": "^3.0 || ^4.0",
Expand All @@ -31,6 +31,7 @@
"nette/tester": "^2.4",
"phpstan/phpstan": "^1.8",
"phpstan/phpstan-strict-rules": "^1.4",
"symfony/http-client": "^6.0",
"tracy/tracy": "^2.9"
},
"autoload": {
Expand Down
7 changes: 5 additions & 2 deletions src/Utils/FioRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@ public function post(string $uri, array $params, string $content): RequestInterf
);
}

return $request->withBody($this->createMultiPart($filename, $stream, $params));
$multipart = $this->createMultiPart($filename, $stream, $params);

return $request->withHeader('Content-Type', 'multipart/form-data; boundary=' . $multipart->getBoundary())
->withBody($multipart);
}


/**
* @param array{token: string, type: string, lng?: string} $params
*/
protected function createMultiPart(string $filename, StreamInterface $file, array $params): StreamInterface
private function createMultiPart(string $filename, StreamInterface $file, array $params): MultipartStream
{
$newPost = [
[
Expand Down
45 changes: 41 additions & 4 deletions tests/src/E2E/FioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace h4kuna\Fio\Tests\E2E;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use h4kuna\Dir\Dir;
use h4kuna\Fio;
use Symfony\Component\HttpClient\Psr18Client;
use Tester;

require __DIR__ . '/../bootstrap.php';
Expand All @@ -17,13 +21,46 @@
if ($accounts === false) {
throw new Fio\Exceptions\InvalidState('You have bad format for ini file. Let\'s see account.example.ini.');
}
$dir = new Dir(__DIR__ . '/../../temp');

$fioFactory = new Fio\FioFactory($accounts);
// "strictphp/http-clients": "^0.1.3", // php 8.2+
//class Filesystem implements FileFactoryContract
//{
// public function __construct(
// private Dir $dir
// ) {
// }
//
// public function create(FileInfoEntity $file, string $suffix = ''): FileContract
// {
// $dir = $this->dir->dir($file->path);
// \Nette\Utils\FileSystem::createDir($dir->getDir());
//
// return new File($dir->filename($file->name . $suffix));
// }
//
//}
//$configManager = new ConfigManager();
//$configManager->addDefault(new StoreConfig(true));
//$filesystemFactory = new Filesystem($dir);
//$makePathAction = new MakePathAction();
//$streamAction = new StreamAction();
//$saveResponse = new SaveResponse($filesystemFactory, $makePathAction, new FindExtensionFromHeadersAction(), $streamAction, serialized: false);
//$saveForPhpstormRequest = new SaveForPhpstormRequest($filesystemFactory, $makePathAction, $saveResponse, $streamAction);
//$client = new StoreResponseClient($saveForPhpstormRequest, $configManager, $psrClient);

$psrFactory = new HttpFactory();
$fioRequest = new Fio\Utils\FioRequestFactory($psrFactory, $psrFactory);
$psrClient = new Psr18Client(responseFactory: $psrFactory, streamFactory: $psrFactory); // symfony
$psrClient = new Client(); // guzzle

$client = $psrClient;

$fioFactory = new Fio\FioFactory($accounts, $dir->dir('fio'), client: $client, fioRequestFactory: $fioRequest);
$fioRead = $fioFactory->createFioRead();
Tester\Assert::same($accounts['my-fio-account']['account'], $fioRead->getAccount()->getAccount());

$movements = $fioRead->movements('2023-09-15', '2023-09-21');
Tester\Assert::same(5, count($movements));
$movements = $fioRead->movements('-14 days');

Tester\Assert::type(\stdClass::class, $movements->getInfo());
Tester\Assert::same($accounts['my-fio-account']['account'], $movements->getInfo()->accountId);
Expand All @@ -37,6 +74,6 @@

$fioPay->createNational(100, '2600267402/2010');
$response = $fioPay->send();
Tester\Assert::false($response->isOk());
Tester\Assert::true($response->isOk());
Tester\Assert::same(1, $response->code());
Tester\Assert::same([108 => 'Číslo účtu příjemce je identické s číslem účtu plátce.'], $response->errorMessages());

0 comments on commit 1bd3ab7

Please sign in to comment.