File tree Expand file tree Collapse file tree 2 files changed +50
-4
lines changed Expand file tree Collapse file tree 2 files changed +50
-4
lines changed Original file line number Diff line number Diff line change @@ -140,8 +140,16 @@ public function withEnumerator($enumerator): self
140
140
*/
141
141
public function attachEnumerators (iterable $ enumerators ): void
142
142
{
143
- foreach ($ enumerators as $ enumerator ) {
144
- $ this ->{$ this ->fnDoSetBit }(($ this ->enumeration )::get ($ enumerator )->getOrdinal ());
143
+ $ bitset = $ this ->bitset ;
144
+
145
+ try {
146
+ foreach ($ enumerators as $ enumerator ) {
147
+ $ this ->{$ this ->fnDoSetBit }(($ this ->enumeration )::get ($ enumerator )->getOrdinal ());
148
+ }
149
+ } catch (\Throwable $ e ) {
150
+ // reset all changes until error happened
151
+ $ this ->bitset = $ bitset ;
152
+ throw $ e ;
145
153
}
146
154
}
147
155
@@ -192,8 +200,16 @@ public function withoutEnumerator($enumerator): self
192
200
*/
193
201
public function detachEnumerators (iterable $ enumerators ): void
194
202
{
195
- foreach ($ enumerators as $ enumerator ) {
196
- $ this ->{$ this ->fnDoUnsetBit }(($ this ->enumeration )::get ($ enumerator )->getOrdinal ());
203
+ $ bitset = $ this ->bitset ;
204
+
205
+ try {
206
+ foreach ($ enumerators as $ enumerator ) {
207
+ $ this ->{$ this ->fnDoUnsetBit }(($ this ->enumeration )::get ($ enumerator )->getOrdinal ());
208
+ }
209
+ } catch (\Throwable $ e ) {
210
+ // reset all changes until error happened
211
+ $ this ->bitset = $ bitset ;
212
+ throw $ e ;
197
213
}
198
214
}
199
215
Original file line number Diff line number Diff line change @@ -270,6 +270,36 @@ public function getIntegerEnumerations()
270
270
];
271
271
}
272
272
273
+ public function testAttachAtOnceAllOrNone ()
274
+ {
275
+ $ set = new EnumSet (EnumBasic::class);
276
+ $ set ->attachEnumerator (EnumBasic::ONE );
277
+
278
+ try {
279
+ $ set ->attachEnumerators ([EnumBasic::TWO , 'unknown ' ]);
280
+ } catch (InvalidArgumentException $ e ) {
281
+ // exception expected
282
+ } finally {
283
+ $ this ->assertTrue ($ set ->contains (EnumBasic::ONE ));
284
+ $ this ->assertFalse ($ set ->contains (EnumBasic::TWO ));
285
+ }
286
+ }
287
+
288
+ public function testDetachAtOnceAllOrNone ()
289
+ {
290
+ $ set = new EnumSet (EnumBasic::class);
291
+ $ set ->attachEnumerators ([EnumBasic::ONE ]);
292
+
293
+ try {
294
+ $ set ->detachEnumerators ([EnumBasic::ONE , EnumBasic::TWO , 'unknown ' ]);
295
+ } catch (InvalidArgumentException $ e ) {
296
+ // exception expected
297
+ } finally {
298
+ $ this ->assertTrue ($ set ->contains (EnumBasic::ONE ));
299
+ $ this ->assertFalse ($ set ->contains (EnumBasic::TWO ));
300
+ }
301
+ }
302
+
273
303
public function testGetBit ()
274
304
{
275
305
$ set = new EnumSet (EnumBasic::class);
You can’t perform that action at this time.
0 commit comments