Files
SGF/tests/Unit/ProtectedProvisioningInputTest.php

48 lines
2.1 KiB
PHP

<?php
namespace Tests\Unit;
use App\Modules\Identity\Services\ProtectedProvisioningInput;
use PHPUnit\Framework\TestCase;
use RuntimeException;
final class ProtectedProvisioningInputTest extends TestCase
{
private function stream(string $payload)
{
$stream = fopen('php://memory', 'r+');
fwrite($stream, $payload); rewind($stream);
return $stream;
}
public function test_protected_input_preserves_unicode_and_spaces_without_echoing(): void
{
$secret = ' '.bin2hex(random_bytes(12)).'çã! ';
$stream = $this->stream(json_encode(['confirmed' => true, 'email' => 'synthetic@example.test', 'password' => $secret, 'confirmation' => $secret]));
try { $values = ProtectedProvisioningInput::read($stream, 'synthetic@example.test'); }
finally { fclose($stream); }
$this->assertTrue($values[0] === $secret && $values[1] === $secret);
}
public function test_unconfirmed_or_wrong_recipient_is_refused_without_revealing_payload(): void
{
$secret = bin2hex(random_bytes(12));
foreach ([['confirmed' => false, 'email' => 'synthetic@example.test'], ['confirmed' => true, 'email' => 'another@example.test']] as $case) {
$stream = $this->stream(json_encode($case + ['password' => $secret, 'confirmation' => $secret]));
try { ProtectedProvisioningInput::read($stream, 'synthetic@example.test'); $this->fail('Unconfirmed input accepted.'); }
catch (RuntimeException $exception) { $this->assertStringNotContainsString($secret, $exception->getMessage()); }
finally { fclose($stream); }
}
}
public function test_malformed_payload_is_refused_without_including_its_contents(): void
{
$stream = $this->stream('{private-invalid-payload');
try {
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Entrada protegida inválida ou não confirmada. Nada alterado.');
ProtectedProvisioningInput::read($stream, 'synthetic@example.test');
} finally { fclose($stream); }
}
}