File tree Expand file tree Collapse file tree 2 files changed +26
-8
lines changed Expand file tree Collapse file tree 2 files changed +26
-8
lines changed Original file line number Diff line number Diff line change 6
6
7
7
use cebe \openapi \spec \Schema ;
8
8
use Faker \Provider \Base ;
9
-
10
9
use function array_unique ;
11
- use function array_values ;
10
+ use function count ;
11
+ use function dump ;
12
12
13
13
/**
14
14
* @internal
@@ -30,10 +30,20 @@ public static function generate(Schema $schema): array
30
30
31
31
for ($ i = 0 ; $ i < $ itemSize ; $ i ++) {
32
32
$ fakeData [] = $ itemSchema ->generate ();
33
- }
34
33
35
- if ($ schema ->uniqueItems === true ) {
36
- $ fakeData = array_values (array_unique ($ fakeData ));
34
+ if ($ schema ->uniqueItems !== true ) {
35
+ continue ;
36
+ }
37
+
38
+ $ uniqueData = array_unique ($ fakeData );
39
+
40
+ if (count ($ uniqueData ) >= count ($ fakeData )) {
41
+ continue ;
42
+ }
43
+
44
+ $ i -= count ($ fakeData ) - count ($ uniqueData );
45
+
46
+ $ fakeData = $ uniqueData ;
37
47
}
38
48
39
49
return $ fakeData ;
Original file line number Diff line number Diff line change 9
9
use Vural \OpenAPIFaker \Tests \Unit \UnitTestCase ;
10
10
11
11
use function array_unique ;
12
+ use function count ;
13
+ use function mt_srand ;
14
+ use function random_int ;
12
15
use function Safe \sort ;
13
16
17
+ use const MT_RAND_PHP ;
18
+
14
19
/**
15
20
* @uses \Vural\OpenAPIFaker\SchemaFaker\SchemaFaker
16
21
* @uses \Vural\OpenAPIFaker\SchemaFaker\StringFaker
@@ -127,23 +132,26 @@ function it_handles_nested_arrays()
127
132
/** @test */
128
133
function it_can_generate_unique_elements ()
129
134
{
135
+ mt_srand (227 , MT_RAND_PHP );
136
+
130
137
$ fakeData = ArrayFaker::generate (SchemaFactory::fromJson (
131
138
<<< JSON
132
139
{
133
140
"type": "array",
134
141
"items": {
135
142
"type": "integer",
136
143
"minimum": 1,
137
- "maximum": 2
144
+ "maximum": 5
138
145
},
139
- "minItems": 1 ,
140
- "maxItems": 2 ,
146
+ "minItems": 5 ,
147
+ "maxItems": 5 ,
141
148
"uniqueItems": true
142
149
}
143
150
JSON
144
151
));
145
152
146
153
self ::assertIsArray ($ fakeData );
154
+ self ::assertCount (5 , $ fakeData );
147
155
self ::assertSame ($ fakeData , array_unique ($ fakeData ));
148
156
}
149
157
You can’t perform that action at this time.
0 commit comments