symfony_example_app/tests/Service/Remote/UsetrenoQRCodeProviderTest.php

119 lines
4.9 KiB
PHP

<?php
namespace App\Tests\Service\Remote;
use App\Entity\DTO\QRCode\QRCode;
use App\Entity\DTO\QRCode\QRCodeMoney;
use App\Entity\DTO\QRCode\QRCodePaymentIdentification;
use App\Entity\DTO\QRCode\QRCodeQROptions;
use App\Entity\Remote\Usetreno\Edge\EdgeQRCode;
use App\Entity\Remote\Usetreno\Edge\EdgeQRCodeMoney;
use App\Entity\Remote\Usetreno\Edge\EdgeQRCodePaymentIdentification;
use App\Entity\Remote\Usetreno\Edge\EdgeQRCodeQROptions;
use App\Service\Exception\QRCodeGeneratorException;
use App\Service\Remote\Edge\QRCodeEntityConverter;
use App\Service\Remote\UsetrenoHttpClient;
use App\Service\Remote\UsetrenoQRCodeProvider;
use App\Tests\Common\LoggerTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\HttpClient\ResponseInterface;
class UsetrenoQRCodeProviderTest extends TestCase
{
use LoggerTrait;
public function testSuccessRequest() {
$base64Image = "iVBORw0KGgoAAAANSUhEUgAAAEkAAABJCAIAAAD+EZyLAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACzklEQVRoge1a0Y6DMAwLp/v/X+49IFWenXaQZNt1qh8mKCnEJHUC7Git2Zfi59MOvBCb25rY3NbE5rYmNrc1sbmtic1tTWxua2JzWxO/sWnHcehga+04Dvw9B3HWZHx+5oCTQW56ve7xuaEEcGK3OQ3IzD1zAHFueFW96+cIEkaPKWg25pN5nZPiNgdmoIG7Ggc3UfOo15LWGlLq7o42dLwKqbipN5SKeIjCiINqXMIzzs1d4ugraqaO4HSyDIsHO/OKd6/qq8rJGxBcbwegj9ijYJx8UO6fBgSn49yYk3Et0RVClQ2z0RXMvusqZ15jCnSS7i4yIT5UBk9WruuTgnEdcW4kcfjr6iTGAbcpja1OS4LcVN9HnYfGCm2oUzMhdmWVjpBab8gHlcOtbLjrisSkyscQ1GXSd/MkRO1dMu6paCOGODcbrC7KOnWR8pCaySpilqzdqI3KlkAhdQNOZsmngWzc6PLaixA9PQPujixjTgb7SVrupCLUQ9KUUbQpVq4s3UK2druuu8JN67Dnnnae9pgXYXo1z90mmYOud4OntQujnalsJ1LPOJNird0GWqqWkrEbzLvI9sq9I3HDYrJgtOLbsybr3TnZ10Nnhf3xxVqHxK40NHeR0hKM1eje9yXnVmrz+KjqBt1L1n7njJdfrboFuqdA3rHUMw7pJN1gSlE1QFDPbY+5GkP8nQKFoq89HCSnO0ySU3U1H7eynJzHRNvlUbeFsywnJ2XvXjEg+KvNlz3GiooklfuMS8XfcYgDtRdu72JSDKoqQap20xLq47hN3TCVDb0do1MFUPwdh3aJhvb42nPiyZOiUvytQysvHiJflaq2LB/rS1xgNdMbT7EaRaakdhd/x8FDE3rmvY1Ey3mhv4hsX4JJSEddextIP010tee2h1W1+x/im/+Dsbmtic1tTWxua2JzWxOb25rY3NbE5rYmNrc18QcV0HKSoAc9PQAAAABJRU5ErkJggg==";
$successRequest = [
"uri" => "/api/v1/qr-code/create-for-bank-account-payment",
"timestamp" => "2024-01-16T14:05:42+01:00",
"data" => [
"base64Data" => "data:image/png;base64,$base64Image"
]
];
[$edgeEntity, $qrCodeEntity] = $this->createQRCodeEntityPair();
$qrCodeProvider = $this->createQRCodeProvider($successRequest, 200, $edgeEntity, $qrCodeEntity);
$data = $qrCodeProvider->generateQRCodeFromEntity($qrCodeEntity);
$this->assertEquals($base64Image, $data);
}
public function testFailureRequest() {
$this->expectException(QRCodeGeneratorException::class);
$failureRequest = [
"error" => "internal server error",
];
[$edgeEntity, $qrCodeEntity] = $this->createQRCodeEntityPair();
$qrCodeProvider = $this->createQRCodeProvider($failureRequest, 500, $edgeEntity, $qrCodeEntity);
$data = $qrCodeProvider->generateQRCodeFromEntity($qrCodeEntity);
$this->assertEquals($failureRequest["data"]["base64Data"], $data);
}
public function createQRCodeProvider(array $response, int $responseCode, EdgeQRCode $edgeEntity, QRCode $entity): UsetrenoQRCodeProvider
{
$responseMock = $this->createMock(ResponseInterface::class);
$responseMock->expects($this->any())
->method('getContent')
->will($this->returnValue(json_encode($response)));
$responseMock->expects($this->any())
->method('getStatusCode')
->will($this->returnValue($responseCode));
$mock = $this->createMock(UsetrenoHttpClient::class);
$mock->expects($this->once())
->method('request')
->will($this->returnValue($responseMock));
$converterMock = $this->createMock(QRCodeEntityConverter::class);
$converterMock->expects($this->once())
->method('convert')
->will($this->returnValue($edgeEntity));
return new UsetrenoQRCodeProvider($this->getLogger(), $mock, $converterMock);
}
protected function createQRCodeEntityPair() {
$edgeEntity = new EdgeQRCode(
"CZ0000",
(new \DateTime("now"))->format('Y-m-d'),
"foo",
new EdgeQRCodeMoney(
100,
"CZK"
),
new EdgeQRCodeQROptions(
1,
1
),
new EdgeQRCodePaymentIdentification(
"0", "0", "0"
)
);
$qrCodeEntity = new QRCode(
"CZ0000",
new \DateTimeImmutable("now"),
"foo",
new QRCodeMoney(
100,
"CZK"
),
new QRCodeQROptions(
1,
1
),
new QRCodePaymentIdentification(
"0", "0", "0"
)
);
return [$edgeEntity, $qrCodeEntity];
}
}