|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of the Nexus framework. |
| 7 | + * |
| 8 | + * (c) John Paul E. Balandan, CPA <paulbalandan@gmail.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Nexus\Tests\Encryption; |
| 15 | + |
| 16 | +use Nexus\Encryption\Encoding\HexEncoder; |
| 17 | +use Nexus\Encryption\Secret; |
| 18 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 19 | +use PHPUnit\Framework\Attributes\Group; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +#[CoversClass(Secret::class)] |
| 26 | +#[Group('unit-test')] |
| 27 | +final class SecretTest extends TestCase |
| 28 | +{ |
| 29 | + private string $data; |
| 30 | + |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $this->data = (new HexEncoder())->encode(random_bytes(32)); |
| 34 | + } |
| 35 | + |
| 36 | + public function testCannotAcceptNullOnConstruct(): void |
| 37 | + { |
| 38 | + $this->expectException(\RuntimeException::class); |
| 39 | + $this->expectExceptionMessage('Secret cannot accept null.'); |
| 40 | + |
| 41 | + new Secret(null); |
| 42 | + } |
| 43 | + |
| 44 | + public function testCannotSerialise(): void |
| 45 | + { |
| 46 | + $this->expectException(\BadMethodCallException::class); |
| 47 | + $this->expectExceptionMessage('Cannot serialise a Secret object.'); |
| 48 | + |
| 49 | + serialize(new Secret($this->data)); |
| 50 | + } |
| 51 | + |
| 52 | + public function testCannotDumpString(): void |
| 53 | + { |
| 54 | + $plaintext = new Secret($this->data); |
| 55 | + |
| 56 | + ob_start(); |
| 57 | + var_dump($plaintext); |
| 58 | + $dump = (string) ob_get_clean(); |
| 59 | + $dump = preg_replace('/(\033\[[0-9;]+m)|(\035\[[0-9;]+m)/u', '', $dump) ?? $dump; |
| 60 | + $print = print_r($plaintext, true); |
| 61 | + |
| 62 | + self::assertStringNotContainsString($this->data, $dump); |
| 63 | + self::assertStringContainsString('[redacted]', $dump); |
| 64 | + self::assertStringNotContainsString($this->data, $print); |
| 65 | + self::assertStringContainsString('[redacted]', $print); |
| 66 | + } |
| 67 | + |
| 68 | + public function testEquals(): void |
| 69 | + { |
| 70 | + $one = new Secret($this->data); |
| 71 | + $two = new Secret($this->data); |
| 72 | + $three = new Secret((new HexEncoder())->encode(random_bytes(32))); |
| 73 | + |
| 74 | + self::assertTrue($one->equals($two)); |
| 75 | + self::assertFalse($one->equals($three)); |
| 76 | + self::assertTrue($two->equals($one)); |
| 77 | + self::assertFalse($two->equals($three)); |
| 78 | + self::assertFalse($three->equals($one)); |
| 79 | + self::assertFalse($three->equals($two)); |
| 80 | + } |
| 81 | + |
| 82 | + public function testRevealNeverReturnsEmptyString(): void |
| 83 | + { |
| 84 | + $plaintext = new Secret($this->data); |
| 85 | + |
| 86 | + self::assertSame($this->data, $plaintext->reveal()); |
| 87 | + } |
| 88 | +} |
0 commit comments