28
28
use TestHelpers \SetupHelper ;
29
29
use PHPUnit \Framework \Assert ;
30
30
use Psr \Http \Message \ResponseInterface ;
31
+ use splitbrain \PHPArchive \Tar ;
32
+ use splitbrain \PHPArchive \Zip ;
33
+ use splitbrain \PHPArchive \Archive ;
31
34
32
35
require_once 'bootstrap.php ' ;
33
36
@@ -62,6 +65,38 @@ public function setUpScenario(BeforeScenarioScope $scope): void {
62
65
);
63
66
}
64
67
68
+ /**
69
+ * @param string $type
70
+ *
71
+ * @return Archive
72
+ */
73
+ public function getArchiveClass (string $ type ): Archive {
74
+ if ($ type === 'zip ' ) {
75
+ return new Zip ();
76
+ } elseif ($ type === 'tar ' ) {
77
+ return new Tar ();
78
+ } else {
79
+ throw new Exception ('Unknown archive type: ' . $ type );
80
+ }
81
+ }
82
+
83
+ /**
84
+ * @param string $dir
85
+ *
86
+ * @return void
87
+ */
88
+ public function removeDir (string $ dir ): void {
89
+ $ items = \glob ("$ dir/* " );
90
+ foreach ($ items as $ item ) {
91
+ if (\is_dir ($ item )) {
92
+ $ this ->removeDir ($ item );
93
+ } else {
94
+ \unlink ($ item );
95
+ }
96
+ }
97
+ \rmdir ($ dir );
98
+ }
99
+
65
100
/**
66
101
* @param string $user
67
102
* @param string $resource
@@ -92,9 +127,10 @@ private function getArchiverQueryString(
92
127
}
93
128
94
129
/**
95
- * @When user :user downloads the archive of :resource using the resource :addressType and setting these headers
130
+ * @When /^ user "([^"]*)" downloads the (zip|tar) archive of "([^"]*)" using the resource (id|ids|path|paths) and setting these headers:$/
96
131
*
97
132
* @param string $user
133
+ * @param string $archiveType
98
134
* @param string $resource
99
135
* @param string $addressType id|path
100
136
* @param TableNode $headersTable
@@ -104,8 +140,9 @@ private function getArchiverQueryString(
104
140
* @throws GuzzleException
105
141
* @throws Exception
106
142
*/
107
- public function userDownloadsTheArchive (
143
+ public function userDownloadsTheZipOrTarArchiveOfResourceUsingResourceIdOrPathAndSettingTheseHeaders (
108
144
string $ user ,
145
+ string $ archiveType ,
109
146
string $ resource ,
110
147
string $ addressType ,
111
148
TableNode $ headersTable
@@ -118,7 +155,7 @@ public function userDownloadsTheArchive(
118
155
foreach ($ headersTable as $ row ) {
119
156
$ headers [$ row ['header ' ]] = $ row ['value ' ];
120
157
}
121
- $ this ->featureContext ->setResponse ($ this ->downloadArchive ($ user , $ resource , $ addressType , null , $ headers ));
158
+ $ this ->featureContext ->setResponse ($ this ->downloadArchive ($ user , $ resource , $ addressType , $ archiveType , null , $ headers ));
122
159
}
123
160
124
161
/**
@@ -140,13 +177,14 @@ public function userDownloadsTheArchiveOfItemOfUser(
140
177
string $ owner ,
141
178
string $ addressType
142
179
): void {
143
- $ this ->featureContext ->setResponse ($ this ->downloadArchive ($ downloader , $ resource , $ addressType , $ owner ));
180
+ $ this ->featureContext ->setResponse ($ this ->downloadArchive ($ downloader , $ resource , $ addressType , null , $ owner ));
144
181
}
145
182
146
183
/**
147
184
* @param string $downloader
148
185
* @param string $resource
149
186
* @param string $addressType
187
+ * @param string|null $archiveType
150
188
* @param string|null $owner
151
189
* @param array|null $headers
152
190
*
@@ -158,12 +196,16 @@ public function downloadArchive(
158
196
string $ downloader ,
159
197
string $ resource ,
160
198
string $ addressType ,
199
+ ?string $ archiveType = null ,
161
200
?string $ owner = null ,
162
201
?array $ headers = null
163
202
): ResponseInterface {
164
203
$ owner = $ owner ?? $ downloader ;
165
204
$ downloader = $ this ->featureContext ->getActualUsername ($ downloader );
166
205
$ queryString = $ this ->getArchiverQueryString ($ owner , $ resource , $ addressType );
206
+ if ($ archiveType !== null ) {
207
+ $ queryString .= '&output-format= ' . $ archiveType ;
208
+ }
167
209
return HttpRequestHelper::get (
168
210
$ this ->featureContext ->getBaseUrl () . '/archiver? ' . $ queryString ,
169
211
$ this ->featureContext ->getStepLineRef (),
@@ -220,28 +262,34 @@ public function theDownloadedArchiveShouldContainTheseFiles(string $type, TableN
220
262
$ this ->featureContext ->verifyTableNodeColumns ($ expectedFiles , ['name ' , 'content ' ]);
221
263
$ contents = $ this ->featureContext ->getResponse ()->getBody ()->getContents ();
222
264
$ tempFile = \tempnam (\sys_get_temp_dir (), 'OcAcceptanceTests_ ' );
265
+ $ tempExtractFolder = $ tempFile ;
223
266
\unlink ($ tempFile ); // we only need the name
224
267
$ tempFile = $ tempFile . '. ' . $ type ; // it needs the extension
225
268
\file_put_contents ($ tempFile , $ contents );
226
269
227
270
// open the archive
228
- $ archiveData = new RecursiveIteratorIterator (
229
- new PharData ($ tempFile ),
230
- RecursiveIteratorIterator::SELF_FIRST
231
- );
271
+ $ tar = $ this ->getArchiveClass ($ type );
272
+ $ tar ->open ($ tempFile );
273
+ $ archiveData = $ tar ->contents ();
274
+
275
+ // extract the archive
276
+ $ tar ->open ($ tempFile );
277
+ $ tar ->extract ($ tempExtractFolder );
278
+ $ tar ->close ();
279
+
232
280
foreach ($ expectedFiles ->getHash () as $ expectedItem ) {
233
281
$ expectedPath = trim ($ expectedItem ['name ' ], "/ " );
234
282
$ found = false ;
235
283
foreach ($ archiveData as $ info ) {
236
284
// get only the parent folder path for the given item
237
- $ actualPath = explode (". $ type " , $ info ->getPathname ())[1 ];
238
- $ actualPath = trim ($ actualPath , "/ " );
285
+ $ actualPath = $ info ->getPath ();
239
286
240
287
if ($ expectedPath === $ actualPath ) {
241
- if (!$ info ->isDir ()) {
288
+ if (!$ info ->getIsdir ()) {
289
+ $ fileContent = \file_get_contents ("$ tempExtractFolder/ $ actualPath " );
242
290
Assert::assertEquals (
243
291
$ expectedItem ['content ' ],
244
- $ info -> getContent () ,
292
+ $ fileContent ,
245
293
__METHOD__ .
246
294
" content of ' " . $ expectedPath . "' not as expected "
247
295
);
@@ -255,5 +303,6 @@ public function theDownloadedArchiveShouldContainTheseFiles(string $type, TableN
255
303
}
256
304
}
257
305
\unlink ($ tempFile );
306
+ $ this ->removeDir ($ tempExtractFolder );
258
307
}
259
308
}
0 commit comments