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 4211cc4
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 6 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
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
113 changes: 109 additions & 4 deletions tests/src/E2E/FioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,34 @@

namespace h4kuna\Fio\Tests\E2E;

/**
* composer
* "strictphp/http-clients": "^0.1.3", // php 8.2+
* "symfony/http-client": "^7.1",
*/

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use h4kuna\Dir\Dir;
use h4kuna\Fio;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use StrictPhp\HttpClients\Actions\FindExtensionFromHeadersAction;
use StrictPhp\HttpClients\Actions\StreamAction;
use StrictPhp\HttpClients\Clients\Event\Actions\MakePathAction;
use StrictPhp\HttpClients\Clients\Event\Entities\FileInfoEntity;
use StrictPhp\HttpClients\Clients\Event\Entities\HttpStateEntity;
use StrictPhp\HttpClients\Clients\Event\Events\FailedRequestEvent;
use StrictPhp\HttpClients\Clients\Event\Events\SuccessRequestEvent;
use StrictPhp\HttpClients\Contracts\ConfigContract;
use StrictPhp\HttpClients\Filesystem\Contracts\FileContract;
use StrictPhp\HttpClients\Filesystem\Contracts\FileFactoryContract;
use StrictPhp\HttpClients\Filesystem\Wrappers\File;
use StrictPhp\HttpClients\Managers\ConfigManager;
use StrictPhp\HttpClients\Requests\SaveForPhpstormRequest;
use StrictPhp\HttpClients\Responses\SaveResponse;
use Symfony\Component\HttpClient\Psr18Client;
use Tester;

require __DIR__ . '/../bootstrap.php';
Expand All @@ -17,13 +44,91 @@
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');


class Filesystem implements FileFactoryContract

Check failure on line 50 in tests/src/E2E/FioTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Class h4kuna\Fio\Tests\E2E\Filesystem implements unknown interface StrictPhp\HttpClients\Filesystem\Contracts\FileFactoryContract.
{
public function __construct(
private Dir $dir
) {
}

public function create(FileInfoEntity $file, string $suffix = ''): FileContract

Check failure on line 57 in tests/src/E2E/FioTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method h4kuna\Fio\Tests\E2E\Filesystem::create() has invalid return type StrictPhp\HttpClients\Filesystem\Contracts\FileContract.

Check failure on line 57 in tests/src/E2E/FioTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter $file of method h4kuna\Fio\Tests\E2E\Filesystem::create() has invalid type StrictPhp\HttpClients\Clients\Event\Entities\FileInfoEntity.
{
$dir = $this->dir->dir($file->path);

Check failure on line 59 in tests/src/E2E/FioTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to property $path on an unknown class StrictPhp\HttpClients\Clients\Event\Entities\FileInfoEntity.
\Nette\Utils\FileSystem::createDir($dir->getDir());

return new File($dir->filename($file->name . $suffix));

Check failure on line 62 in tests/src/E2E/FioTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to property $name on an unknown class StrictPhp\HttpClients\Clients\Event\Entities\FileInfoEntity.

Check failure on line 62 in tests/src/E2E/FioTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Instantiated class StrictPhp\HttpClients\Filesystem\Wrappers\File not found.

Check failure on line 62 in tests/src/E2E/FioTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method h4kuna\Fio\Tests\E2E\Filesystem::create() should return StrictPhp\HttpClients\Filesystem\Contracts\FileContract but returns StrictPhp\HttpClients\Filesystem\Wrappers\File.
}

}

class StoreConfig implements ConfigContract

Check failure on line 67 in tests/src/E2E/FioTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Class h4kuna\Fio\Tests\E2E\StoreConfig implements unknown interface StrictPhp\HttpClients\Contracts\ConfigContract.
{
public function __construct(public bool $enabled = false)
{
}

public function initFromDefaultConfig(ConfigContract $object): void

Check failure on line 73 in tests/src/E2E/FioTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter $object of method h4kuna\Fio\Tests\E2E\StoreConfig::initFromDefaultConfig() has invalid type StrictPhp\HttpClients\Contracts\ConfigContract.
{
// TODO: Implement initFromDefaultConfig() method.
}

}

final class StoreResponseClient implements ClientInterface
{
public function __construct(
private SaveForPhpstormRequest $saveForPhpstormRequest,

Check failure on line 83 in tests/src/E2E/FioTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter $saveForPhpstormRequest of method h4kuna\Fio\Tests\E2E\StoreResponseClient::__construct() has invalid type StrictPhp\HttpClients\Requests\SaveForPhpstormRequest.
private ConfigManager $configManager,
private ClientInterface $client,
) {
}

public function sendRequest(RequestInterface $request): ResponseInterface
{
$config = $this->configManager->get(StoreConfig::class, $request->getUri()->getHost());

if ($config->enabled === false) {
return $this->client->sendRequest($request);
}

$state = new HttpStateEntity($request);
try {
$response = $this->client->sendRequest($request);
} catch (\Throwable $e) {
$failed = new FailedRequestEvent($state->finish(), $e);
$this->saveForPhpstormRequest->save($failed);
throw $e;
}

$success = new SuccessRequestEvent($state->finish(), $response);
$this->saveForPhpstormRequest->save($success, $response);

return $response;
}

}

$psrFactory = new HttpFactory();
$fioRequest = new Fio\Utils\FioRequestFactory($psrFactory, $psrFactory);
$psrClient = new Psr18Client(responseFactory: $psrFactory, streamFactory: $psrFactory); // symfony
$psrClient = new Client(); // guzzle
$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);

$fioFactory = new Fio\FioFactory($accounts);
$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 +142,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 4211cc4

Please sign in to comment.