55use Base \BaseTest ;
66use PHPUnit \Framework \Attributes \Test ;
77use Rcsofttech85 \FileHandler \Exception \FileEncryptorException ;
8+ use Rcsofttech85 \FileHandler \Exception \FileHandlerException ;
89use Rcsofttech85 \FileHandler \FileEncryptor ;
910use SodiumException ;
1011
@@ -29,28 +30,31 @@ protected function tearDown(): void
2930 /**
3031 * @return void
3132 * @throws FileEncryptorException
33+ * @throws FileHandlerException
3234 * @throws SodiumException
3335 */
3436 #[Test]
3537 public function throwExceptionOnDecryptingNonEncryptedFile (): void
3638 {
3739 $ this ->expectException (FileEncryptorException::class);
3840 $ this ->expectExceptionMessage ('file is not encrypted ' );
39- $ this ->fileEncryptor ->decryptFile ();
41+ $ this ->fileEncryptor ->decryptFile (' movie.csv ' );
4042 }
4143
44+
4245 /**
4346 * @return void
4447 * @throws FileEncryptorException
4548 */
4649 #[Test]
4750 public function canEncryptFile (): void
4851 {
49- $ isFileEncrypted = $ this ->fileEncryptor ->encryptFile ();
52+ $ isFileEncrypted = $ this ->fileEncryptor ->encryptFile (' movie.csv ' );
5053
5154 $ this ->assertTrue ($ isFileEncrypted );
5255 }
5356
57+
5458 /**
5559 * @return void
5660 * @throws FileEncryptorException
@@ -60,9 +64,10 @@ public function throwExceptionIfAlreadyEncrypted(): void
6064 {
6165 $ this ->expectException (FileEncryptorException::class);
6266 $ this ->expectExceptionMessage ('file is already encrypted ' );
63- $ this ->fileEncryptor ->encryptFile ();
67+ $ this ->fileEncryptor ->encryptFile (' movie.csv ' );
6468 }
6569
70+
6671 /**
6772 * @return void
6873 * @throws FileEncryptorException
@@ -71,12 +76,12 @@ public function throwExceptionIfAlreadyEncrypted(): void
7176 public function throwExceptionIfFileHasNoContentWhileEncrypt (): void
7277 {
7378 file_put_contents ("test " , "" );
74- $ file = new FileEncryptor ('test ' , 'pass ' );
7579 $ this ->expectException (FileEncryptorException::class);
7680 $ this ->expectExceptionMessage ('File has no content ' );
77- $ file -> encryptFile ();
81+ $ this -> fileEncryptor -> encryptFile (' test ' );
7882 }
7983
84+
8085 #[Test]
8186 public function throwExceptionIfCouldNotConvertHexToBin (): void
8287 {
@@ -89,42 +94,55 @@ public function throwExceptionIfCouldNotConvertHexToBin(): void
8994 * @return void
9095 * @throws FileEncryptorException
9196 * @throws SodiumException
97+ * @throws FileHandlerException
9298 */
9399 #[Test]
94100 public function throwExceptionIfFileHasNoContent (): void
95101 {
96102 file_put_contents ("test " , "" );
97- $ file = new FileEncryptor ('test ' , 'pass ' );
98103 $ this ->expectException (FileEncryptorException::class);
99104 $ this ->expectExceptionMessage ('File has no content ' );
100- $ file -> decryptFile ();
105+ $ this -> fileEncryptor -> decryptFile (' test ' );
101106 }
102107
108+
103109 /**
104110 * @return void
105111 * @throws FileEncryptorException
112+ * @throws FileHandlerException
106113 * @throws SodiumException
107114 */
108-
109115 #[Test]
110116 public function throwExceptionIfDecryptionFails (): void
111117 {
112- $ fileEncryptor = new FileEncryptor ('movie.csv ' , 'wrong ' );
113-
114- $ this ->expectException (FileEncryptorException::class);
115- $ this ->expectExceptionMessage ('could not decrypt file ' );
116- $ fileEncryptor ->decryptFile ();
118+ $ filePath = '.env ' ;
119+ $ originalContent = file_get_contents ($ filePath );
120+ if (!$ originalContent ) {
121+ $ this ->fail ('file not found ' );
122+ }
123+ $ password = $ _ENV [FileEncryptor::ENCRYPT_PASSWORD ];
124+ $ updatedContent = str_replace ($ password , 'pass ' , $ originalContent );
125+
126+ file_put_contents ($ filePath , $ updatedContent );
127+ try {
128+ $ this ->expectException (FileEncryptorException::class);
129+ $ this ->expectExceptionMessage ('could not decrypt file ' );
130+ $ this ->fileEncryptor ->decryptFile ('movie.csv ' );
131+ } finally {
132+ file_put_contents ($ filePath , $ originalContent );
133+ }
117134 }
118135
119136 /**
120137 * @return void
121138 * @throws FileEncryptorException
139+ * @throws FileHandlerException
122140 * @throws SodiumException
123141 */
124142 #[Test]
125143 public function canDecryptFile (): void
126144 {
127- $ isFileDecrypted = $ this ->fileEncryptor ->decryptFile ();
145+ $ isFileDecrypted = $ this ->fileEncryptor ->decryptFile (' movie.csv ' );
128146
129147 $ this ->assertTrue ($ isFileDecrypted );
130148 }
0 commit comments