mivita/tests/Unit/Services/PayoneTest.php
2026-04-10 17:15:27 +02:00

96 lines
3.2 KiB
PHP

<?php
use App\Services\Payone;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Facades\Mail;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Tests\TestCase;
uses(TestCase::class);
it('parst text/plain Antworten in ein assoziatives Array', function () {
$body = "status=OK\nreference=abc123\nerrormessage=\n";
$response = new Response(200, ['Content-Type' => 'text/plain;charset=UTF-8'], $body);
$parsed = Payone::parseResponse($response);
expect($parsed)->toBeArray()
->and($parsed['status'])->toBe('OK')
->and($parsed['reference'])->toBe('abc123')
->and($parsed['errormessage'])->toBe('');
});
it('parst Werte mit Gleichheitszeichen im Wert', function () {
$body = "status=OK\nfoo=bar=baz\n";
$response = new Response(200, ['Content-Type' => 'text/plain;charset=UTF-8'], $body);
$parsed = Payone::parseResponse($response);
expect($parsed['foo'])->toBe('bar=baz');
});
it('sendRequest liefert das geparste Array bei text/plain;charset=UTF-8', function () {
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'text/plain;charset=UTF-8'], "status=APPROVED\ntxid=999\n"),
]);
$client = new Client(['handler' => HandlerStack::create($mock)]);
$result = Payone::sendRequest(['request' => 'authorization'], '', $client);
expect($result)->toBeArray()
->and($result['status'])->toBe('APPROVED')
->and($result['txid'])->toBe('999');
});
it('sendRequest wandelt Netzwerkfehler ohne HTTP-Antwort in Fehler 1002 um', function () {
Mail::fake();
$mockRequest = new Request('POST', Payone::PAYONE_SERVER_API_URL);
$mock = new MockHandler([
new ConnectException('cURL error 56: Recv failure: Connection reset by peer', $mockRequest),
]);
$client = new Client(['handler' => HandlerStack::create($mock)]);
$thrown = null;
try {
Payone::sendRequest(['request' => 'authorization'], '', $client);
} catch (HttpException $e) {
$thrown = $e;
}
expect($thrown)->toBeInstanceOf(HttpException::class)
->and($thrown->getStatusCode())->toBe(403)
->and($thrown->getMessage())->toContain('Fehlercode: 1002');
});
it('exponiert die erwartete Post-Gateway-URL', function () {
expect(Payone::PAYONE_SERVER_API_URL)->toBe('https://api.pay1.de/post-gateway/');
});
/**
* Manuell auf dem Server ausführen, wenn TLS/Outbound geprüft werden soll:
* PAYONE_CONNECTIVITY_TEST=1 php artisan test --compact tests/Unit/Services/PayoneTest.php
*/
it('optional: Erreichbarkeit von api.pay1.de (TLS/Outbound)', function () {
if (! env('PAYONE_CONNECTIVITY_TEST')) {
$this->markTestSkipped('Setze PAYONE_CONNECTIVITY_TEST=1 für einen echten Netzwerk-Check.');
}
$client = new Client([
'timeout' => 15,
'connect_timeout' => 10,
'http_errors' => false,
]);
$response = $client->get('https://api.pay1.de/', [
'http_errors' => false,
]);
expect($response->getStatusCode())->toBeGreaterThanOrEqual(200)
->and($response->getStatusCode())->toBeLessThan(600);
})->group('payone-connectivity');