77namespace DeepL ;
88
99use PHPUnit \Framework \TestCase ;
10- use Psr \Http \Client \ClientInterface ;
1110use Ramsey \Uuid \Uuid ;
1211
1312class DeepLTestBase extends TestCase
@@ -68,11 +67,13 @@ class DeepLTestBase extends TestCase
6867 ];
6968
7069 protected const DOC_MINIFICATION_TEST_FILES_MAPPING = [
71- 'example_document_template.docx ' => '' ,
72- 'example_presentation_template.pptx ' => '' ,
73- 'example_zip_template.zip ' => '' ,
70+ 'example_document_template.docx ' => 'example_document.docx ' ,
71+ 'example_presentation_template.pptx ' => 'example_presentation.pptx ' ,
7472 ];
7573
74+ protected const DOC_MINIFICATION_UNSUPPORTED_TEST_TEMPLATE = 'example_zip_template.zip ' ;
75+ protected const DOC_MINIFICATION_UNSUPPORTED_TEST_FILE = 'example_zip.zip ' ;
76+
7677 protected const EXAMPLE_DOCUMENT_INPUT = DeepLTestBase::EXAMPLE_TEXT ['en ' ];
7778 protected const EXAMPLE_DOCUMENT_OUTPUT = DeepLTestBase::EXAMPLE_TEXT ['de ' ];
7879 protected $ EXAMPLE_LARGE_DOCUMENT_INPUT ;
@@ -248,48 +249,45 @@ public function assertExceptionClass($class, callable $function): \Exception
248249 $ this ->fail ("Expected exception of class ' $ class' but nothing was thrown " );
249250 }
250251
251- public function createDocumentMinificationTestFiles (): void
252+ public static function getFullPathForTestFile (string $ testFileName ): string
253+ {
254+ return __DIR__ . '/../resources/ ' . $ testFileName ;
255+ }
256+
257+ public static function createDocumentMinificationTestFiles (): void
252258 {
253259 foreach (DeepLTestBase::DOC_MINIFICATION_TEST_FILES_MAPPING as $ templateFilename => $ inflatedFilename ) {
254- $ testDocTemplateFile = __DIR__ . ' /../resources/ ' . $ templateFilename ;
255- $ testDocInflatedFile = __DIR__ . ' /../resources/ ' . $ inflatedFilename ;
256- $ this -> inflateTestFileWithLargeImage ($ testDocTemplateFile , $ testDocInflatedFile );
260+ $ testDocTemplateFile = self :: getFullPathForTestFile ( $ templateFilename) ;
261+ $ testDocInflatedFile = self :: getFullPathForTestFile ( $ inflatedFilename) ;
262+ self :: inflateTestFileWithLargeImage ($ testDocTemplateFile , $ testDocInflatedFile );
257263 }
258264 }
259265
260- public function removeDocumentMinificationTestFiles ()
266+ public static function removeDocumentMinificationTestFiles ()
261267 {
262268 foreach (DeepLTestBase::DOC_MINIFICATION_TEST_FILES_MAPPING as $ inflatedFileToDelete ) {
263269 unlink ($ inflatedFileToDelete );
264270 }
265271 }
266272
267- private function inflateTestFileWithLargeImage (string $ inputFile , string $ outputFile )
273+ protected static function inflateTestFileWithLargeImage (string $ inputFile , string $ outputFile )
268274 {
269- $ extractionDir = __DIR__ . ' /../resources/ ' ;
275+ $ extractionDir = self :: getFullPathForTestFile ( ' inflation_tmp_dir ' ) ;
270276 if (!mkdir ($ extractionDir )) {
271- throw new \RuntimeException (' Failed creating dir for test files for doc minification ' );
277+ throw new \RuntimeException (" Failed creating dir $ extractionDir for test files for doc minification" );
272278 }
273- $ zip = new \ZipArchive ();
274- if ($ zip ->open ($ inputFile ) === true ) {
275- $ zip ->extractTo ($ extractionDir );
276- $ zip ->close ();
277- } else {
278- throw new \RuntimeException ('Failed inflating test file for doc minification ' );
279- }
280-
281- $ inflatedImage = imagecreatetruecolor (18384 , 18384 );
282- $ white = imagecolorallocate ($ inflatedImage , 255 , 255 , 255 );
283- imagefill ($ inflatedImage , 0 , 0 , $ white );
284- $ iterator = new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator ($ extractionDir ));
285- foreach ($ iterator as $ file ) {
286- if ($ file ->getExtension () == 'png ' ) {
287- imagepng ($ inflatedImage , $ file ->getPathname ());
288- }
289- }
290-
279+ self ::extractZipFileTo ($ inputFile , $ extractionDir );
291280 $ zip = new \ZipArchive ();
292281 if ($ zip ->open ($ outputFile , \ZipArchive::CREATE ) === true ) {
282+ $ fakeImageName = 'placeholder_image.png ' ;
283+ $ characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+=-<,>.?: ' ;
284+ $ length = 90000000 ;
285+ # str_shuffle is not cryptographically secure, but this is just test data
286+ $ randomString = substr (str_shuffle (
287+ str_repeat ($ characters , ceil ($ length /strlen ($ characters )))
288+ ), 1 , $ length );
289+ $ zip ->addFromString ($ fakeImageName , $ randomString );
290+
293291 $ iterator = new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator ($ extractionDir ));
294292 foreach ($ iterator as $ file ) {
295293 if (substr ($ file , -2 ) === '/. ' || substr ($ file , -3 ) === '/.. ' ) {
@@ -298,7 +296,6 @@ private function inflateTestFileWithLargeImage(string $inputFile, string $output
298296 $ file ->isDir () ?
299297 $ zip ->addEmptyDir (str_replace ($ extractionDir . '/ ' , '' , $ file . '/ ' ))
300298 : $ zip ->addFile ($ file , str_replace ($ extractionDir . '/ ' , '' , $ file ));
301- $ zip ->setCompressionName ($ file , \ZipArchive::CM_STORE );
302299 }
303300 $ zip ->close ();
304301 } else {
@@ -308,6 +305,61 @@ private function inflateTestFileWithLargeImage(string $inputFile, string $output
308305 DocumentMinifier::recursivelyDeleteDirectory ($ extractionDir );
309306 }
310307
308+ protected static function extractZipFileTo (string $ zipFilePath , string $ extractionDirPath ): void
309+ {
310+ if (!is_dir ($ extractionDirPath )) {
311+ mkdir ($ extractionDirPath );
312+ }
313+ $ zip = new \ZipArchive ();
314+ if ($ zip ->open ($ zipFilePath ) === false ) {
315+ throw new \RuntimeException ("Failed opening zip file $ zipFilePath " );
316+ }
317+ $ zip ->extractTo ($ extractionDirPath );
318+ $ zip ->close ();
319+ }
320+
321+ public function assertDirectoriesAreEqual (string $ dir1 , string $ dir2 , string $ message = '' ): void
322+ {
323+ $ dir1Hashes = $ this ->getDirectoryContentsToHashes ($ dir1 );
324+ $ dir2Hashes = $ this ->getDirectoryContentsToHashes ($ dir2 );
325+
326+ $ this ->assertAssociativeArraysAreValueEqual ($ dir1Hashes , $ dir2Hashes , $ message );
327+ }
328+
329+ protected function assertAssociativeArraysAreValueEqual (array $ array1 , array $ array2 , string $ message = '' ): void
330+ {
331+ $ this ->assertEquals (count ($ array1 ), count ($ array2 ));
332+ foreach ($ array1 as $ key1 => $ value1 ) {
333+ if (is_string ($ value1 )) {
334+ $ this ->assertEquals ($ value1 , $ array2 [$ key1 ], $ message );
335+ } else {
336+ $ this ->assertAssociativeArraysAreValueEqual ($ array1 [$ key1 ], $ array2 [$ key1 ], $ message );
337+ }
338+ }
339+ }
340+
341+ protected function getDirectoryContentsToHashes (string $ dir ): array
342+ {
343+ $ hashes = array ();
344+ if (is_dir ($ dir )) {
345+ $ objects = scandir ($ dir );
346+ foreach ($ objects as $ path ) {
347+ if ($ path !== '. ' && $ path !== '.. ' ) {
348+ $ absolutePath = $ dir . '/ ' . $ path ;
349+ if (is_dir ($ path )) {
350+ $ hashes [$ path ] = $ this ->getDirectoryContentsToHashes ($ absolutePath );
351+ } else {
352+ $ hashes [$ path ] = hash_file ('md5 ' , $ absolutePath );
353+ if ($ hashes [$ path ] === false ) {
354+ throw new \RuntimeException ("Failed hashing $ absolutePath " );
355+ }
356+ }
357+ }
358+ }
359+ }
360+ return $ hashes ;
361+ }
362+
311363 /**
312364 * This is necessary due to https://github.com/php-mock/php-mock-phpunit#restrictions
313365 * In short, as these methods can be called by other tests before UserAgentTest and other
@@ -324,7 +376,7 @@ public static function setUpBeforeClass(): void
324376 self ::defineFunctionMock (__NAMESPACE__ , 'curl_setopt_array ' );
325377 }
326378
327- public function provideHttpClient ()
379+ public static function provideHttpClient ()
328380 {
329381 return [[null ], [new \GuzzleHttp \Client ()]];
330382 }
0 commit comments