@@ -33,21 +33,19 @@ public static function register()
3333
3434class HttpRecorder
3535{
36- private $ stubsFolder = 'snapshots ' ;
36+ private $ snapshotDirectory = 'snapshots ' ;
3737
3838 private $ usePrettyJson = true ;
3939
4040 public function storeIn ($ directory )
4141 {
42- $ this ->stubsFolder = $ directory ;
43-
42+ $ this ->snapshotDirectory = $ directory ;
4443 return $ this ;
4544 }
4645
4746 public function minifyJson ()
4847 {
4948 $ this ->usePrettyJson = false ;
50-
5149 return $ this ;
5250 }
5351
@@ -57,14 +55,17 @@ public function handle(Request $request)
5755
5856 $ urlInfo = parse_url ($ request ->url ());
5957
60- //create specific filename for storing stubs
61- $ filename = Str::lower ($ request ->method ()).'_ ' ;
62- $ filename .= Str::slug (Str::replace ('/ ' , '- ' , $ urlInfo ['path ' ]));
63- $ filename .= '_ ' .Str::slug (Str::replace ('& ' , '_ ' , Str::replace ('= ' , '- ' , $ urlInfo ['query ' ])));
64- $ filename .= '.json ' ;
58+ //create specific filename for storing snapshots
59+ $ method = Str::lower ($ request ->method ());
60+ $ name = Str::slug (Str::replace ('/ ' , '- ' , $ urlInfo ['path ' ]));
61+ $ query = Str::slug (Str::replace ('& ' , '_ ' , Str::replace ('= ' , '- ' , $ urlInfo ['query ' ])));
62+
63+ $ fileName = "{$ method }_ {$ name }_ {$ query }.json " ;
64+ $ directoryPath = "tests/ {$ this ->snapshotDirectory }" ;
65+ $ filePath = "{$ directoryPath }/ {$ fileName }" ;
6566
66- if ($ forceRecording || ! File::exists (' tests/ ' . $ this -> stubsFolder . ' / ' . $ filename )) {
67- File::makeDirectory (' tests/ ' . $ this -> stubsFolder , 0777 , true , true );
67+ if ($ forceRecording || ! File::exists ($ filePath )) {
68+ File::makeDirectory ($ directoryPath , 0777 , true , true );
6869
6970 $ client = new Client ();
7071 $ response = $ client ->request ($ request ->method (), $ request ->url (), [
@@ -79,14 +80,14 @@ public function handle(Request $request)
7980 ];
8081
8182 file_put_contents (
82- ' tests/ ' . $ this -> stubsFolder . ' / ' . $ filename ,
83+ $ filePath ,
8384 json_encode ($ recordedResponse , $ this ->usePrettyJson ? JSON_PRETTY_PRINT : 0 )
8485 );
8586
8687 return Http::response ($ recordedResponse ['data ' ], $ response ->getStatusCode ());
8788 }
8889
89- $ preRecordedData = json_decode (file_get_contents (' tests/ ' . $ this -> stubsFolder . ' / ' . $ filename ), true );
90+ $ preRecordedData = json_decode (file_get_contents ($ filePath ), true );
9091
9192 return Http::response ($ preRecordedData ['data ' ], $ preRecordedData ['status ' ]);
9293 }
0 commit comments