2
2
3
3
namespace AndreasElia \PostmanGenerator \Tests \Feature ;
4
4
5
+ use AndreasElia \PostmanGenerator \Tests \Fixtures \CollectionHelpersTrait ;
5
6
use AndreasElia \PostmanGenerator \Tests \TestCase ;
6
7
use Illuminate \Support \Arr ;
7
8
use Illuminate \Support \Facades \Storage ;
8
9
9
10
class ExportPostmanTest extends TestCase
10
11
{
12
+ use CollectionHelpersTrait;
13
+
11
14
protected function setUp (): void
12
15
{
13
16
parent ::setUp ();
@@ -32,14 +35,25 @@ public function test_standard_export_works(bool $formDataEnabled)
32
35
33
36
$ collectionItems = $ collection ['item ' ];
34
37
35
- $ this ->assertCount (count ($ routes ), $ collectionItems );
38
+ $ totalCollectionItems = $ this ->countCollectionItems ($ collection ['item ' ]);
39
+
40
+ $ this ->assertEquals (count ($ routes ), $ totalCollectionItems );
36
41
37
42
foreach ($ routes as $ route ) {
38
- $ collectionRoute = Arr::first ($ collectionItems , function ($ item ) use ($ route ) {
43
+ $ methods = $ route ->methods ();
44
+
45
+ $ collectionRoutes = Arr::where ($ collectionItems , function ($ item ) use ($ route ) {
39
46
return $ item ['name ' ] == $ route ->uri ();
40
47
});
48
+
49
+ $ collectionRoute = Arr::first ($ collectionRoutes );
50
+
51
+ if (! in_array ($ collectionRoute ['request ' ]['method ' ], $ methods )) {
52
+ $ methods = collect ($ collectionRoutes )->pluck ('request.method ' )->toArray ();
53
+ }
54
+
41
55
$ this ->assertNotNull ($ collectionRoute );
42
- $ this ->assertTrue (in_array ($ collectionRoute ['request ' ]['method ' ], $ route -> methods () ));
56
+ $ this ->assertTrue (in_array ($ collectionRoute ['request ' ]['method ' ], $ methods ));
43
57
}
44
58
}
45
59
@@ -68,16 +82,25 @@ public function test_bearer_export_works(bool $formDataEnabled)
68
82
69
83
$ this ->assertCount (2 , $ collectionVariables );
70
84
71
- $ collectionItems = $ collection ['item ' ];
85
+ $ totalCollectionItems = $ this -> countCollectionItems ( $ collection ['item ' ]) ;
72
86
73
- $ this ->assertCount (count ($ routes ), $ collectionItems );
87
+ $ this ->assertEquals (count ($ routes ), $ totalCollectionItems );
74
88
75
89
foreach ($ routes as $ route ) {
76
- $ collectionRoute = Arr::first ($ collectionItems , function ($ item ) use ($ route ) {
90
+ $ methods = $ route ->methods ();
91
+
92
+ $ collectionRoutes = Arr::where ($ collection ['item ' ], function ($ item ) use ($ route ) {
77
93
return $ item ['name ' ] == $ route ->uri ();
78
94
});
95
+
96
+ $ collectionRoute = Arr::first ($ collectionRoutes );
97
+
98
+ if (! in_array ($ collectionRoute ['request ' ]['method ' ], $ methods )) {
99
+ $ methods = collect ($ collectionRoutes )->pluck ('request.method ' )->toArray ();
100
+ }
101
+
79
102
$ this ->assertNotNull ($ collectionRoute );
80
- $ this ->assertTrue (in_array ($ collectionRoute ['request ' ]['method ' ], $ route -> methods () ));
103
+ $ this ->assertTrue (in_array ($ collectionRoute ['request ' ]['method ' ], $ methods ));
81
104
}
82
105
}
83
106
@@ -106,16 +129,25 @@ public function test_basic_export_works(bool $formDataEnabled)
106
129
107
130
$ this ->assertCount (2 , $ collectionVariables );
108
131
109
- $ collectionItems = $ collection ['item ' ];
132
+ $ totalCollectionItems = $ this -> countCollectionItems ( $ collection ['item ' ]) ;
110
133
111
- $ this ->assertCount (count ($ routes ), $ collectionItems );
134
+ $ this ->assertEquals (count ($ routes ), $ totalCollectionItems );
112
135
113
136
foreach ($ routes as $ route ) {
114
- $ collectionRoute = Arr::first ($ collectionItems , function ($ item ) use ($ route ) {
137
+ $ methods = $ route ->methods ();
138
+
139
+ $ collectionRoutes = Arr::where ($ collection ['item ' ], function ($ item ) use ($ route ) {
115
140
return $ item ['name ' ] == $ route ->uri ();
116
141
});
142
+
143
+ $ collectionRoute = Arr::first ($ collectionRoutes );
144
+
145
+ if (! in_array ($ collectionRoute ['request ' ]['method ' ], $ methods )) {
146
+ $ methods = collect ($ collectionRoutes )->pluck ('request.method ' )->toArray ();
147
+ }
148
+
117
149
$ this ->assertNotNull ($ collectionRoute );
118
- $ this ->assertTrue (in_array ($ collectionRoute ['request ' ]['method ' ], $ route -> methods () ));
150
+ $ this ->assertTrue (in_array ($ collectionRoute ['request ' ]['method ' ], $ methods ));
119
151
}
120
152
}
121
153
@@ -135,9 +167,9 @@ public function test_structured_export_works(bool $formDataEnabled)
135
167
136
168
$ routes = $ this ->app ['router ' ]->getRoutes ();
137
169
138
- $ collectionItems = $ collection ['item ' ];
170
+ $ totalCollectionItems = $ this -> countCollectionItems ( $ collection ['item ' ]) ;
139
171
140
- $ this ->assertCount (count ($ routes ), $ collectionItems [ 0 ][ ' item ' ] );
172
+ $ this ->assertEquals (count ($ routes ), $ totalCollectionItems );
141
173
}
142
174
143
175
public function test_rules_printing_export_works ()
@@ -301,6 +333,51 @@ public function test_uri_is_correct()
301
333
$ this ->assertEquals ($ targetRequest ['request ' ]['url ' ]['raw ' ], '{{base_url}}/example/phpDocRoute ' );
302
334
}
303
335
336
+ public function test_api_resource_routes_set_parameters_correctly_with_hyphens ()
337
+ {
338
+ $ this ->artisan ('export:postman ' )->assertExitCode (0 );
339
+
340
+ $ collection = collect (json_decode (Storage::get ('postman/ ' .config ('api-postman.filename ' )), true )['item ' ]);
341
+
342
+ $ targetRequest = $ collection
343
+ ->where ('name ' , 'example/users/{user}/audit-logs/{audit_log} ' )
344
+ ->where ('request.method ' , 'PATCH ' )
345
+ ->first ();
346
+
347
+ $ this ->assertEquals ($ targetRequest ['name ' ], 'example/users/{user}/audit-logs/{audit_log} ' );
348
+ $ this ->assertEquals ($ targetRequest ['request ' ]['url ' ]['raw ' ], '{{base_url}}/example/users/:user/audit-logs/:audit_log ' );
349
+ }
350
+
351
+ public function test_api_resource_routes_set_parameters_correctly_with_underscores ()
352
+ {
353
+ $ this ->artisan ('export:postman ' )->assertExitCode (0 );
354
+
355
+ $ collection = collect (json_decode (Storage::get ('postman/ ' .config ('api-postman.filename ' )), true )['item ' ]);
356
+
357
+ $ targetRequest = $ collection
358
+ ->where ('name ' , 'example/users/{user}/other_logs/{other_log} ' )
359
+ ->where ('request.method ' , 'PATCH ' )
360
+ ->first ();
361
+
362
+ $ this ->assertEquals ($ targetRequest ['name ' ], 'example/users/{user}/other_logs/{other_log} ' );
363
+ $ this ->assertEquals ($ targetRequest ['request ' ]['url ' ]['raw ' ], '{{base_url}}/example/users/:user/other_logs/:other_log ' );
364
+ }
365
+
366
+ public function test_api_resource_routes_set_parameters_correctly_with_camel_case ()
367
+ {
368
+ $ this ->artisan ('export:postman ' )->assertExitCode (0 );
369
+
370
+ $ collection = collect (json_decode (Storage::get ('postman/ ' .config ('api-postman.filename ' )), true )['item ' ]);
371
+
372
+ $ targetRequest = $ collection
373
+ ->where ('name ' , 'example/users/{user}/someLogs/{someLog} ' )
374
+ ->where ('request.method ' , 'PATCH ' )
375
+ ->first ();
376
+
377
+ $ this ->assertEquals ($ targetRequest ['name ' ], 'example/users/{user}/someLogs/{someLog} ' );
378
+ $ this ->assertEquals ($ targetRequest ['request ' ]['url ' ]['raw ' ], '{{base_url}}/example/users/:user/someLogs/:someLog ' );
379
+ }
380
+
304
381
public static function providerFormDataEnabled (): array
305
382
{
306
383
return [
0 commit comments