2
0

ZipFileSetTestCase.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the nelexa/zip package.
  5. * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace PhpZip\Tests;
  10. use PhpZip\Util\StringUtil;
  11. use PhpZip\ZipFile;
  12. /**
  13. * Class ZipFileSetTestCase.
  14. */
  15. abstract class ZipFileSetTestCase extends ZipTestCase
  16. {
  17. protected static array $files = [
  18. '.hidden' => 'Hidden file',
  19. 'text file.txt' => 'Text file',
  20. 'Текстовый документ.txt' => 'Текстовый документ',
  21. '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.',
  22. 'empty dir/' => '',
  23. 'empty dir2/ещё пустой каталог/' => '',
  24. 'catalog/New File' => 'New Catalog File',
  25. 'catalog/New File 2' => 'New Catalog File 2',
  26. 'catalog/Empty Dir/' => '',
  27. 'category/list.txt' => 'Category list',
  28. 'category/Pictures/128x160/Car/01.jpg' => 'File 01.jpg',
  29. 'category/Pictures/128x160/Car/02.jpg' => 'File 02.jpg',
  30. 'category/Pictures/240x320/Car/01.jpg' => 'File 01.jpg',
  31. 'category/Pictures/240x320/Car/02.jpg' => 'File 02.jpg',
  32. ];
  33. /**
  34. * Before test.
  35. */
  36. protected function setUp(): void
  37. {
  38. parent::setUp();
  39. $this->fillDirectory();
  40. }
  41. protected function fillDirectory(): void
  42. {
  43. foreach (self::$files as $name => $content) {
  44. $fullName = $this->outputDirname . '/' . $name;
  45. if (StringUtil::endsWith($name, '/')) {
  46. if (!is_dir($fullName) && !mkdir($fullName, 0755, true) && !is_dir($fullName)) {
  47. throw new \RuntimeException(sprintf('Directory "%s" was not created', $fullName));
  48. }
  49. } else {
  50. $dirname = \dirname($fullName);
  51. if (!is_dir($dirname) && !mkdir($dirname, 0755, true) && !is_dir($dirname)) {
  52. throw new \RuntimeException(sprintf('Directory "%s" was not created', $dirname));
  53. }
  54. file_put_contents($fullName, $content);
  55. }
  56. }
  57. }
  58. protected static function assertFilesResult(
  59. ZipFile $zipFile,
  60. array $actualResultFiles = [],
  61. string $localPath = '/'
  62. ): void {
  63. $localPath = rtrim($localPath, '/');
  64. $localPath = empty($localPath) ? '' : $localPath . '/';
  65. static::assertCount(\count($zipFile), $actualResultFiles);
  66. $actualResultFiles = array_flip($actualResultFiles);
  67. foreach (self::$files as $file => $content) {
  68. $zipEntryName = $localPath . $file;
  69. if (isset($actualResultFiles[$file])) {
  70. static::assertTrue(isset($zipFile[$zipEntryName]), 'Not found entry name ' . $zipEntryName);
  71. static::assertSame(
  72. $zipFile[$zipEntryName],
  73. $content,
  74. sprintf('The content of the entry "%s" is not as expected.', $zipEntryName)
  75. );
  76. unset($actualResultFiles[$file]);
  77. } else {
  78. static::assertFalse(isset($zipFile[$zipEntryName]));
  79. }
  80. }
  81. static::assertEmpty($actualResultFiles);
  82. }
  83. }