@@ -17,68 +17,233 @@ final class InMemoryCipherKeyStoreTest extends TestCase
1717 public function testStoreAndLoad (): void
1818 {
1919 $ key = new CipherKey (
20- 'foo ' ,
21- 'bar ' ,
22- 'baz ' ,
20+ 'key-1 ' ,
21+ 'subject-1 ' ,
22+ 'secret ' ,
2323 'aes-256-gcm ' ,
2424 new DateTimeImmutable (),
2525 );
2626
2727 $ store = new InMemoryCipherKeyStore ();
28- $ store ->store (' foo ' , $ key );
28+ $ store ->store ($ key );
2929
30- self ::assertSame ($ key , $ store ->get ('foo ' ));
30+ self ::assertSame ($ key , $ store ->get ('key-1 ' ));
3131 }
3232
3333 public function testLoadFailed (): void
3434 {
3535 $ this ->expectException (CipherKeyNotExists::class);
3636
3737 $ store = new InMemoryCipherKeyStore ();
38- $ store ->get ('foo ' );
38+ $ store ->get ('non-existent ' );
3939 }
4040
4141 public function testRemove (): void
4242 {
4343 $ key = new CipherKey (
44- 'foo ' ,
45- 'bar ' ,
46- 'baz ' ,
44+ 'key-1 ' ,
45+ 'subject-1 ' ,
46+ 'secret ' ,
4747 'aes-256-gcm ' ,
4848 new DateTimeImmutable (),
4949 );
5050
5151 $ store = new InMemoryCipherKeyStore ();
52- $ store ->store (' foo ' , $ key );
52+ $ store ->store ($ key );
5353
54- self ::assertSame ($ key , $ store ->get ('foo ' ));
54+ self ::assertSame ($ key , $ store ->get ('key-1 ' ));
5555
56- $ store ->remove ('foo ' );
56+ $ store ->remove ('key-1 ' );
5757
5858 $ this ->expectException (CipherKeyNotExists::class);
5959
60- $ store ->get ('foo ' );
60+ $ store ->get ('key-1 ' );
6161 }
6262
6363 public function testClear (): void
6464 {
6565 $ key = new CipherKey (
66- 'foo ' ,
67- 'bar ' ,
68- 'baz ' ,
66+ 'key-1 ' ,
67+ 'subject-1 ' ,
68+ 'secret ' ,
6969 'aes-256-gcm ' ,
7070 new DateTimeImmutable (),
7171 );
7272
7373 $ store = new InMemoryCipherKeyStore ();
74- $ store ->store (' foo ' , $ key );
74+ $ store ->store ($ key );
7575
76- self ::assertSame ($ key , $ store ->get ('foo ' ));
76+ self ::assertSame ($ key , $ store ->get ('key-1 ' ));
7777
7878 $ store ->clear ();
7979
8080 $ this ->expectException (CipherKeyNotExists::class);
8181
82- $ store ->get ('foo ' );
82+ $ store ->get ('key-1 ' );
83+ }
84+
85+ public function testCurrentKeyFor (): void
86+ {
87+ $ key1 = new CipherKey (
88+ 'key-1 ' ,
89+ 'subject-1 ' ,
90+ 'secret-1 ' ,
91+ 'aes-256-gcm ' ,
92+ new DateTimeImmutable (),
93+ );
94+
95+ $ key2 = new CipherKey (
96+ 'key-2 ' ,
97+ 'subject-1 ' ,
98+ 'secret-2 ' ,
99+ 'aes-256-gcm ' ,
100+ new DateTimeImmutable (),
101+ );
102+
103+ $ store = new InMemoryCipherKeyStore ();
104+ $ store ->store ($ key1 );
105+ $ store ->store ($ key2 );
106+
107+ self ::assertSame ($ key2 , $ store ->currentKeyFor ('subject-1 ' ));
108+ }
109+
110+ public function testCurrentKeyForNotExists (): void
111+ {
112+ $ this ->expectException (CipherKeyNotExists::class);
113+
114+ $ store = new InMemoryCipherKeyStore ();
115+ $ store ->currentKeyFor ('non-existent ' );
116+ }
117+
118+ public function testRemoveWithSubjectId (): void
119+ {
120+ $ key1 = new CipherKey (
121+ 'key-1 ' ,
122+ 'subject-1 ' ,
123+ 'secret-1 ' ,
124+ 'aes-256-gcm ' ,
125+ new DateTimeImmutable (),
126+ );
127+
128+ $ key2 = new CipherKey (
129+ 'key-2 ' ,
130+ 'subject-1 ' ,
131+ 'secret-2 ' ,
132+ 'aes-256-gcm ' ,
133+ new DateTimeImmutable (),
134+ );
135+
136+ $ key3 = new CipherKey (
137+ 'key-3 ' ,
138+ 'subject-2 ' ,
139+ 'secret-3 ' ,
140+ 'aes-256-gcm ' ,
141+ new DateTimeImmutable (),
142+ );
143+
144+ $ store = new InMemoryCipherKeyStore ();
145+ $ store ->store ($ key1 );
146+ $ store ->store ($ key2 );
147+ $ store ->store ($ key3 );
148+
149+ $ store ->removeWithSubjectId ('subject-1 ' );
150+
151+ $ this ->expectException (CipherKeyNotExists::class);
152+ $ store ->get ('key-1 ' );
153+ }
154+
155+ public function testRemoveWithSubjectIdDoesNotAffectOtherSubjects (): void
156+ {
157+ $ key1 = new CipherKey (
158+ 'key-1 ' ,
159+ 'subject-1 ' ,
160+ 'secret-1 ' ,
161+ 'aes-256-gcm ' ,
162+ new DateTimeImmutable (),
163+ );
164+
165+ $ key2 = new CipherKey (
166+ 'key-2 ' ,
167+ 'subject-2 ' ,
168+ 'secret-2 ' ,
169+ 'aes-256-gcm ' ,
170+ new DateTimeImmutable (),
171+ );
172+
173+ $ store = new InMemoryCipherKeyStore ();
174+ $ store ->store ($ key1 );
175+ $ store ->store ($ key2 );
176+
177+ $ store ->removeWithSubjectId ('subject-1 ' );
178+
179+ self ::assertSame ($ key2 , $ store ->get ('key-2 ' ));
180+ }
181+
182+ public function testRemoveWithSubjectIdNonExistent (): void
183+ {
184+ $ store = new InMemoryCipherKeyStore ();
185+ $ store ->removeWithSubjectId ('non-existent ' );
186+
187+ $ this ->expectNotToPerformAssertions ();
188+ }
189+
190+ public function testRemoveNonExistent (): void
191+ {
192+ $ store = new InMemoryCipherKeyStore ();
193+ $ store ->remove ('non-existent ' );
194+
195+ $ this ->expectNotToPerformAssertions ();
196+ }
197+
198+ public function testStoreOverwritesExistingKey (): void
199+ {
200+ $ key1 = new CipherKey (
201+ 'key-1 ' ,
202+ 'subject-1 ' ,
203+ 'secret-1 ' ,
204+ 'aes-256-gcm ' ,
205+ new DateTimeImmutable (),
206+ );
207+
208+ $ key2 = new CipherKey (
209+ 'key-1 ' ,
210+ 'subject-1 ' ,
211+ 'secret-2 ' ,
212+ 'aes-256-gcm ' ,
213+ new DateTimeImmutable (),
214+ );
215+
216+ $ store = new InMemoryCipherKeyStore ();
217+ $ store ->store ($ key1 );
218+ $ store ->store ($ key2 );
219+
220+ self ::assertSame ($ key2 , $ store ->get ('key-1 ' ));
221+ self ::assertSame ($ key2 , $ store ->currentKeyFor ('subject-1 ' ));
222+ }
223+
224+ public function testMultipleSubjects (): void
225+ {
226+ $ key1 = new CipherKey (
227+ 'key-1 ' ,
228+ 'subject-1 ' ,
229+ 'secret-1 ' ,
230+ 'aes-256-gcm ' ,
231+ new DateTimeImmutable (),
232+ );
233+
234+ $ key2 = new CipherKey (
235+ 'key-2 ' ,
236+ 'subject-2 ' ,
237+ 'secret-2 ' ,
238+ 'aes-256-gcm ' ,
239+ new DateTimeImmutable (),
240+ );
241+
242+ $ store = new InMemoryCipherKeyStore ();
243+ $ store ->store ($ key1 );
244+ $ store ->store ($ key2 );
245+
246+ self ::assertSame ($ key1 , $ store ->currentKeyFor ('subject-1 ' ));
247+ self ::assertSame ($ key2 , $ store ->currentKeyFor ('subject-2 ' ));
83248 }
84249}
0 commit comments