|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sidus\EncryptionBundle\Tests\PHPUnit\Doctrine\Type; |
| 4 | + |
| 5 | +use Doctrine\DBAL\Platforms\MySqlPlatform; |
| 6 | +use PHPUnit\Framework\MockObject\MockObject; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Sidus\EncryptionBundle\Doctrine\Type\EncryptStringType; |
| 9 | +use Sidus\EncryptionBundle\Doctrine\Type\EncryptTextType; |
| 10 | +use Sidus\EncryptionBundle\Manager\EncryptionManagerInterface; |
| 11 | + |
| 12 | +class EncryptTextTypeTest extends TestCase |
| 13 | +{ |
| 14 | + public function testConvertToPHPValue(): void |
| 15 | + { |
| 16 | + [$type, $encryptionManager] = $this->createType(); |
| 17 | + $encryptedString = '\X666'; |
| 18 | + $platform = $this->createMock(MySqlPlatform::class); |
| 19 | + |
| 20 | + // The type SHOULD decrypt the encrypted string |
| 21 | + $encryptionManager |
| 22 | + ->expects($this->once()) |
| 23 | + ->method('decryptString') |
| 24 | + ->with(base64_decode($encryptedString)) |
| 25 | + ->willReturn('my_decrypted_string') |
| 26 | + ; |
| 27 | + |
| 28 | + $value = $type->convertToPHPValue($encryptedString, $platform); |
| 29 | + $this->assertEquals('my_decrypted_string', $value); |
| 30 | + } |
| 31 | + |
| 32 | + public function testConvertToDatabaseValue(): void |
| 33 | + { |
| 34 | + [$type, $encryptionManager] = $this->createType(); |
| 35 | + $string = 'my_string'; |
| 36 | + $platform = $this->createMock(MySqlPlatform::class); |
| 37 | + |
| 38 | + // The type SHOULD decrypt the encrypted string |
| 39 | + $encryptionManager |
| 40 | + ->expects($this->once()) |
| 41 | + ->method('encryptString') |
| 42 | + ->with($string) |
| 43 | + ->willReturn('my_encrypted_string') |
| 44 | + ; |
| 45 | + |
| 46 | + $value = $type->convertToDatabaseValue($string, $platform); |
| 47 | + $this->assertEquals(base64_encode('my_encrypted_string'), $value); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @return EncryptStringType[]|MockObject[] |
| 52 | + */ |
| 53 | + private function createType(): array |
| 54 | + { |
| 55 | + $encryptionManager = $this->createMock(EncryptionManagerInterface::class); |
| 56 | + $type = new EncryptTextType(); |
| 57 | + $type->setEncryptionManager($encryptionManager); |
| 58 | + |
| 59 | + return [$type, $encryptionManager]; |
| 60 | + } |
| 61 | +} |
0 commit comments