@@ -286,6 +286,81 @@ public function random_or_create(): void
286286 static ::factory ()::repository ()->assert ()->count (2 );
287287 }
288288
289+ /**
290+ * @test
291+ */
292+ #[Test]
293+ public function random_range_or_create_enough_object (): void
294+ {
295+ static ::factory ()->create (['prop1 ' => 'a ' ]);
296+ static ::factory ()->create (['prop1 ' => 'a ' ]);
297+ static ::factory ()->create (['prop1 ' => 'a ' ]);
298+
299+ $ range = static ::factory ()::randomRangeOrCreate (2 , 3 );
300+
301+ $ this ->assertGreaterThanOrEqual (2 , \count ($ range ));
302+ $ this ->assertLessThanOrEqual (3 , \count ($ range ));
303+
304+ foreach ($ range as $ object ) {
305+ $ this ->assertSame ('a ' , $ object ->getProp1 ());
306+ }
307+ }
308+
309+ /**
310+ * @test
311+ */
312+ #[Test]
313+ public function random_range_or_create_not_enough_object (): void
314+ {
315+ static ::factory ()->create (['prop1 ' => 'a ' ]);
316+
317+ $ range = static ::factory ()::randomRangeOrCreate (2 , 3 );
318+
319+ $ this ->assertGreaterThanOrEqual (2 , \count ($ range ));
320+ $ this ->assertLessThanOrEqual (3 , \count ($ range ));
321+
322+ static ::factory ()::assert ()
323+ ->exists (['prop1 ' => 'default1 ' ]);
324+ }
325+
326+ /**
327+ * @test
328+ */
329+ #[Test]
330+ public function random_range_or_create_not_enough_object_with_criteria (): void
331+ {
332+ static ::factory ()->create (['prop1 ' => 'a ' ]);
333+ static ::factory ()->create (['prop1 ' => 'b ' ]);
334+
335+ $ range = static ::factory ()::randomRangeOrCreate (2 , 3 , ['prop1 ' => 'b ' ]);
336+
337+ $ this ->assertGreaterThanOrEqual (2 , \count ($ range ));
338+ $ this ->assertLessThanOrEqual (3 , \count ($ range ));
339+
340+ foreach ($ range as $ object ) {
341+ $ this ->assertSame ('b ' , $ object ->getProp1 ());
342+ }
343+ }
344+
345+ /**
346+ * @test
347+ */
348+ #[Test]
349+ public function random_range_or_create_no_object_with_criteria (): void
350+ {
351+ static ::factory ()->create (['prop1 ' => 'a ' ]);
352+ static ::factory ()->create (['prop1 ' => 'b ' ]);
353+
354+ $ range = static ::factory ()::randomRangeOrCreate (2 , 3 , ['prop1 ' => 'c ' ]);
355+
356+ $ this ->assertGreaterThanOrEqual (2 , \count ($ range ));
357+ $ this ->assertLessThanOrEqual (3 , \count ($ range ));
358+
359+ foreach ($ range as $ object ) {
360+ $ this ->assertSame ('c ' , $ object ->getProp1 ());
361+ }
362+ }
363+
289364 /**
290365 * @test
291366 */
0 commit comments