From 1bd3ab7a0b0a54ebc48d03272ca6c08bf5507133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Fri, 21 Jun 2024 11:14:54 +0200 Subject: [PATCH] feat(FioRequestFactory): more guzzle independent --- .gitignore | 1 + composer.json | 3 ++- src/Utils/FioRequestFactory.php | 7 +++-- tests/src/E2E/FioTest.php | 45 ++++++++++++++++++++++++++++++--- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index a9bb9c9..0e3b7e4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ composer.lock /tests/**/output/ tests/src/E2E/account.ini coverage.html +/tests/src/E2E/test.http diff --git a/composer.json b/composer.json index 41d794a..ffe3158 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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": { diff --git a/src/Utils/FioRequestFactory.php b/src/Utils/FioRequestFactory.php index 61d696d..88c736b 100644 --- a/src/Utils/FioRequestFactory.php +++ b/src/Utils/FioRequestFactory.php @@ -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 = [ [ diff --git a/tests/src/E2E/FioTest.php b/tests/src/E2E/FioTest.php index 8e02f8c..b01bc9f 100644 --- a/tests/src/E2E/FioTest.php +++ b/tests/src/E2E/FioTest.php @@ -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'; @@ -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); @@ -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());