66use PHPUnit \Framework \MockObject \MockObject ;
77use PHPUnit \Framework \TestCase ;
88use Sidus \EncryptionBundle \Doctrine \Type \EncryptStringType ;
9+ use Sidus \EncryptionBundle \Encryption \Enabler \EncryptionEnablerInterface ;
910use Sidus \EncryptionBundle \Manager \EncryptionManagerInterface ;
1011
1112class EncryptStringTypeTest extends TestCase
1213{
1314 public function testConvertToPHPValue (): void
1415 {
15- [$ type , $ encryptionManager ] = $ this ->createType ();
16+ [$ type , $ encryptionManager, $ encryptionEnabler ] = $ this ->createType ();
1617 $ encryptedString = '\X666 ' ;
1718 $ platform = $ this ->createMock (MySqlPlatform::class);
1819
20+ $ encryptionEnabler
21+ ->expects ($ this ->once ())
22+ ->method ('isEncryptionEnabled ' )
23+ ->willReturn (true )
24+ ;
25+
1926 // The type SHOULD decrypt the encrypted string
2027 $ encryptionManager
2128 ->expects ($ this ->once ())
@@ -28,12 +35,40 @@ public function testConvertToPHPValue(): void
2835 $ this ->assertEquals ('my_decrypted_string ' , $ value );
2936 }
3037
38+ public function testConvertToPHPValueWithEncryptionDisabled (): void
39+ {
40+ [$ type , $ encryptionManager , $ encryptionEnabler ] = $ this ->createType ();
41+ $ encryptedString = '\X666 ' ;
42+ $ platform = $ this ->createMock (MySqlPlatform::class);
43+
44+ $ encryptionEnabler
45+ ->expects ($ this ->once ())
46+ ->method ('isEncryptionEnabled ' )
47+ ->willReturn (false )
48+ ;
49+
50+ // The type SHOULD not encrypt the encrypted string if the encryption is disabled
51+ $ encryptionManager
52+ ->expects ($ this ->never ())
53+ ->method ('decryptString ' )
54+ ;
55+
56+ $ value = $ type ->convertToPHPValue ($ encryptedString , $ platform );
57+ $ this ->assertEquals ('\X666 ' , $ value );
58+ }
59+
3160 public function testConvertToDatabaseValue (): void
3261 {
33- [$ type , $ encryptionManager ] = $ this ->createType ();
62+ [$ type , $ encryptionManager, $ encryptionEnabler ] = $ this ->createType ();
3463 $ string = 'my_string ' ;
3564 $ platform = $ this ->createMock (MySqlPlatform::class);
3665
66+ $ encryptionEnabler
67+ ->expects ($ this ->once ())
68+ ->method ('isEncryptionEnabled ' )
69+ ->willReturn (true )
70+ ;
71+
3772 // The type SHOULD decrypt the encrypted string
3873 $ encryptionManager
3974 ->expects ($ this ->once ())
@@ -46,15 +81,47 @@ public function testConvertToDatabaseValue(): void
4681 $ this ->assertEquals (base64_encode ('my_encrypted_string ' ), $ value );
4782 }
4883
84+ public function testConvertToDatabaseValueWithEncryptionDisabled (): void
85+ {
86+ [$ type , $ encryptionManager , $ encryptionEnabler ] = $ this ->createType ();
87+ $ string = 'my_string ' ;
88+ $ platform = $ this ->createMock (MySqlPlatform::class);
89+
90+ $ encryptionEnabler
91+ ->expects ($ this ->once ())
92+ ->method ('isEncryptionEnabled ' )
93+ ->willReturn (false )
94+ ;
95+
96+ // The type SHOULD not decrypt the encrypted string if the encryption is disabled
97+ $ encryptionManager
98+ ->expects ($ this ->never ())
99+ ->method ('encryptString ' )
100+ ;
101+
102+ $ value = $ type ->convertToDatabaseValue ($ string , $ platform );
103+ $ this ->assertEquals ('my_string ' , $ value );
104+ }
105+
106+ public function testGetName (): void
107+ {
108+ [$ type ] = $ this ->createType ();
109+
110+ $ this ->assertEquals ('encrypt_string ' , $ type ->getName ());
111+ }
112+
49113 /**
50114 * @return EncryptStringType[]|MockObject[]
51115 */
52116 private function createType (): array
53117 {
54118 $ encryptionManager = $ this ->createMock (EncryptionManagerInterface::class);
119+ $ encryptionEnabler = $ this ->createMock (EncryptionEnablerInterface::class);
120+
55121 $ type = new EncryptStringType ();
56122 $ type ->setEncryptionManager ($ encryptionManager );
123+ $ type ->setEncryptionEnabler ($ encryptionEnabler );
57124
58- return [$ type , $ encryptionManager ];
125+ return [$ type , $ encryptionManager, $ encryptionEnabler ];
59126 }
60127}
0 commit comments