2
0

SymlinkTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Constants\ZipOptions;
  11. use PhpZip\Util\FilesUtil;
  12. use PhpZip\ZipFile;
  13. use Symfony\Component\Finder\Finder;
  14. /**
  15. * @internal
  16. *
  17. * @small
  18. */
  19. final class SymlinkTest extends ZipTestCase
  20. {
  21. /**
  22. * @dataProvider provideAllowSymlink
  23. *
  24. * @throws \Exception
  25. */
  26. public function testSymlink(bool $allowSymlink): void
  27. {
  28. self::skipTestForWindows();
  29. if (!is_dir($this->outputDirname)) {
  30. self::assertTrue(mkdir($this->outputDirname, 0755, true));
  31. }
  32. $contentsFile = random_bytes(100);
  33. $filePath = $this->outputDirname . '/file.bin';
  34. $symlinkPath = $this->outputDirname . '/symlink.bin';
  35. $symlinkTarget = basename($filePath);
  36. self::assertNotFalse(file_put_contents($filePath, $contentsFile));
  37. self::assertTrue(symlink($symlinkTarget, $symlinkPath));
  38. $finder = (new Finder())->in($this->outputDirname);
  39. $zipFile = new ZipFile();
  40. $zipFile->addFromFinder($finder);
  41. $zipFile->saveAsFile($this->outputFilename);
  42. $zipFile->close();
  43. self::assertCorrectZipArchive($this->outputFilename);
  44. FilesUtil::removeDir($this->outputDirname);
  45. self::assertDirectoryDoesNotExist($this->outputDirname);
  46. self::assertTrue(mkdir($this->outputDirname, 0755, true));
  47. $zipFile->openFile($this->outputFilename);
  48. $zipFile->extractTo($this->outputDirname, null, [
  49. ZipOptions::EXTRACT_SYMLINKS => $allowSymlink,
  50. ]);
  51. $zipFile->close();
  52. $splFileInfo = new \SplFileInfo($symlinkPath);
  53. if ($allowSymlink) {
  54. self::assertTrue($splFileInfo->isLink());
  55. self::assertSame($splFileInfo->getLinkTarget(), $symlinkTarget);
  56. } else {
  57. self::assertFalse($splFileInfo->isLink());
  58. self::assertStringEqualsFile($symlinkPath, $symlinkTarget);
  59. }
  60. }
  61. public function provideAllowSymlink(): \Generator
  62. {
  63. yield 'allow' => [true];
  64. yield 'deny' => [false];
  65. }
  66. }