ZipFileSetTestCase.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace PhpZip\Tests;
  3. use PhpZip\Util\StringUtil;
  4. use PhpZip\ZipFile;
  5. /**
  6. * Class ZipFileSetTestCase.
  7. */
  8. abstract class ZipFileSetTestCase extends ZipTestCase
  9. {
  10. protected static $files = [
  11. '.hidden' => 'Hidden file',
  12. 'text file.txt' => 'Text file',
  13. 'Текстовый документ.txt' => 'Текстовый документ',
  14. 'LoremIpsum.txt' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
  15. 'empty dir/' => '',
  16. 'empty dir2/ещё пустой каталог/' => '',
  17. 'catalog/New File' => 'New Catalog File',
  18. 'catalog/New File 2' => 'New Catalog File 2',
  19. 'catalog/Empty Dir/' => '',
  20. 'category/list.txt' => 'Category list',
  21. 'category/Pictures/128x160/Car/01.jpg' => 'File 01.jpg',
  22. 'category/Pictures/128x160/Car/02.jpg' => 'File 02.jpg',
  23. 'category/Pictures/240x320/Car/01.jpg' => 'File 01.jpg',
  24. 'category/Pictures/240x320/Car/02.jpg' => 'File 02.jpg',
  25. ];
  26. /**
  27. * Before test.
  28. */
  29. protected function setUp()
  30. {
  31. parent::setUp();
  32. $this->fillDirectory();
  33. }
  34. protected function fillDirectory()
  35. {
  36. foreach (self::$files as $name => $content) {
  37. $fullName = $this->outputDirname . '/' . $name;
  38. if (StringUtil::endsWith($name, '/')) {
  39. if (!is_dir($fullName) && !mkdir($fullName, 0755, true) && !is_dir($fullName)) {
  40. throw new \RuntimeException(sprintf('Directory "%s" was not created', $fullName));
  41. }
  42. } else {
  43. $dirname = \dirname($fullName);
  44. if (!is_dir($dirname) && !mkdir($dirname, 0755, true) && !is_dir($dirname)) {
  45. throw new \RuntimeException(sprintf('Directory "%s" was not created', $dirname));
  46. }
  47. file_put_contents($fullName, $content);
  48. }
  49. }
  50. }
  51. /**
  52. * @param ZipFile $zipFile
  53. * @param array $actualResultFiles
  54. * @param string $localPath
  55. */
  56. protected static function assertFilesResult(
  57. ZipFile $zipFile,
  58. array $actualResultFiles = [],
  59. $localPath = '/'
  60. ) {
  61. $localPath = rtrim($localPath, '/');
  62. $localPath = empty($localPath) ? '' : $localPath . '/';
  63. static::assertCount(\count($zipFile), $actualResultFiles);
  64. $actualResultFiles = array_flip($actualResultFiles);
  65. foreach (self::$files as $file => $content) {
  66. $zipEntryName = $localPath . $file;
  67. if (isset($actualResultFiles[$file])) {
  68. static::assertTrue(isset($zipFile[$zipEntryName]));
  69. static::assertSame(
  70. $zipFile[$zipEntryName],
  71. $content,
  72. sprintf('The content of the entry "%s" is not as expected.', $zipEntryName)
  73. );
  74. unset($actualResultFiles[$file]);
  75. } else {
  76. static::assertFalse(isset($zipFile[$zipEntryName]));
  77. }
  78. }
  79. static::assertEmpty($actualResultFiles);
  80. }
  81. }