5
5
use Base \BaseTest ;
6
6
use PHPUnit \Framework \Attributes \Test ;
7
7
use Rcsofttech85 \FileHandler \Exception \FileEncryptorException ;
8
+ use Rcsofttech85 \FileHandler \Exception \FileHandlerException ;
8
9
use Rcsofttech85 \FileHandler \FileEncryptor ;
9
10
use SodiumException ;
10
11
@@ -29,28 +30,31 @@ protected function tearDown(): void
29
30
/**
30
31
* @return void
31
32
* @throws FileEncryptorException
33
+ * @throws FileHandlerException
32
34
* @throws SodiumException
33
35
*/
34
36
#[Test]
35
37
public function throwExceptionOnDecryptingNonEncryptedFile (): void
36
38
{
37
39
$ this ->expectException (FileEncryptorException::class);
38
40
$ this ->expectExceptionMessage ('file is not encrypted ' );
39
- $ this ->fileEncryptor ->decryptFile ();
41
+ $ this ->fileEncryptor ->decryptFile (' movie.csv ' );
40
42
}
41
43
44
+
42
45
/**
43
46
* @return void
44
47
* @throws FileEncryptorException
45
48
*/
46
49
#[Test]
47
50
public function canEncryptFile (): void
48
51
{
49
- $ isFileEncrypted = $ this ->fileEncryptor ->encryptFile ();
52
+ $ isFileEncrypted = $ this ->fileEncryptor ->encryptFile (' movie.csv ' );
50
53
51
54
$ this ->assertTrue ($ isFileEncrypted );
52
55
}
53
56
57
+
54
58
/**
55
59
* @return void
56
60
* @throws FileEncryptorException
@@ -60,9 +64,10 @@ public function throwExceptionIfAlreadyEncrypted(): void
60
64
{
61
65
$ this ->expectException (FileEncryptorException::class);
62
66
$ this ->expectExceptionMessage ('file is already encrypted ' );
63
- $ this ->fileEncryptor ->encryptFile ();
67
+ $ this ->fileEncryptor ->encryptFile (' movie.csv ' );
64
68
}
65
69
70
+
66
71
/**
67
72
* @return void
68
73
* @throws FileEncryptorException
@@ -71,12 +76,12 @@ public function throwExceptionIfAlreadyEncrypted(): void
71
76
public function throwExceptionIfFileHasNoContentWhileEncrypt (): void
72
77
{
73
78
file_put_contents ("test " , "" );
74
- $ file = new FileEncryptor ('test ' , 'pass ' );
75
79
$ this ->expectException (FileEncryptorException::class);
76
80
$ this ->expectExceptionMessage ('File has no content ' );
77
- $ file -> encryptFile ();
81
+ $ this -> fileEncryptor -> encryptFile (' test ' );
78
82
}
79
83
84
+
80
85
#[Test]
81
86
public function throwExceptionIfCouldNotConvertHexToBin (): void
82
87
{
@@ -89,42 +94,55 @@ public function throwExceptionIfCouldNotConvertHexToBin(): void
89
94
* @return void
90
95
* @throws FileEncryptorException
91
96
* @throws SodiumException
97
+ * @throws FileHandlerException
92
98
*/
93
99
#[Test]
94
100
public function throwExceptionIfFileHasNoContent (): void
95
101
{
96
102
file_put_contents ("test " , "" );
97
- $ file = new FileEncryptor ('test ' , 'pass ' );
98
103
$ this ->expectException (FileEncryptorException::class);
99
104
$ this ->expectExceptionMessage ('File has no content ' );
100
- $ file -> decryptFile ();
105
+ $ this -> fileEncryptor -> decryptFile (' test ' );
101
106
}
102
107
108
+
103
109
/**
104
110
* @return void
105
111
* @throws FileEncryptorException
112
+ * @throws FileHandlerException
106
113
* @throws SodiumException
107
114
*/
108
-
109
115
#[Test]
110
116
public function throwExceptionIfDecryptionFails (): void
111
117
{
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
+ }
117
134
}
118
135
119
136
/**
120
137
* @return void
121
138
* @throws FileEncryptorException
139
+ * @throws FileHandlerException
122
140
* @throws SodiumException
123
141
*/
124
142
#[Test]
125
143
public function canDecryptFile (): void
126
144
{
127
- $ isFileDecrypted = $ this ->fileEncryptor ->decryptFile ();
145
+ $ isFileDecrypted = $ this ->fileEncryptor ->decryptFile (' movie.csv ' );
128
146
129
147
$ this ->assertTrue ($ isFileDecrypted );
130
148
}
0 commit comments