Skip to content

Commit e45d0d9

Browse files
committed
add tests for testing file property preservation
1 parent 45c4569 commit e45d0d9

File tree

2 files changed

+130
-5
lines changed

2 files changed

+130
-5
lines changed

tests/TarTestCase.php

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TarTestCase extends TestCase
1616
protected $extensions = array('tar');
1717

1818
/** @inheritdoc */
19-
protected function setUp() : void
19+
protected function setUp(): void
2020
{
2121
parent::setUp();
2222
if (extension_loaded('zlib')) {
@@ -31,7 +31,7 @@ protected function setUp() : void
3131
}
3232

3333
/** @inheritdoc */
34-
protected function tearDown() : void
34+
protected function tearDown(): void
3535
{
3636
parent::tearDown();
3737
$this->extensions[] = null;
@@ -53,7 +53,8 @@ protected function getDir()
5353
* Callback check function
5454
* @param FileInfo $fileinfo
5555
*/
56-
public function increaseCounter($fileinfo) {
56+
public function increaseCounter($fileinfo)
57+
{
5758
$this->assertInstanceOf('\\splitbrain\\PHPArchive\\FileInfo', $fileinfo);
5859
$this->counter++;
5960
}
@@ -560,7 +561,8 @@ public function testZeroData()
560561
/**
561562
* Add a zero byte file to a tar and extract it again
562563
*/
563-
public function testZeroByteFile() {
564+
public function testZeroByteFile()
565+
{
564566
$archive = sys_get_temp_dir() . '/dwziptest' . md5(time()) . '.zip';
565567
$extract = sys_get_temp_dir() . '/dwziptest' . md5(time() + 1);
566568

@@ -814,7 +816,7 @@ public function testReadCurrentEntry()
814816
$tar = new Tar();
815817
$tar->open(__DIR__ . '/tar/test.tar');
816818
$pathsRead = array();
817-
foreach($tar->yieldContents() as $i) {
819+
foreach ($tar->yieldContents() as $i) {
818820
$this->assertFileExists($out . '/' . $i->getPath());
819821
if ($i->getIsdir()) {
820822
$this->assertEquals('', $tar->readCurrentEntry());
@@ -829,6 +831,67 @@ public function testReadCurrentEntry()
829831
self::RDelete($out);
830832
}
831833

834+
/**
835+
* Create an archive, extract it, and compare file properties
836+
*/
837+
public function testFilePropertiesPreservation()
838+
{
839+
$input = glob($this->getDir() . '/../src/*');
840+
$archive = sys_get_temp_dir() . '/dwtartest' . md5(time()) . '.tar';
841+
$extract = sys_get_temp_dir() . '/dwtartest' . md5(time() + 1);
842+
843+
// Create archive
844+
$tar = new Tar();
845+
$tar->create($archive);
846+
foreach ($input as $path) {
847+
$file = basename($path);
848+
$tar->addFile($path, $file);
849+
}
850+
$tar->close();
851+
$this->assertFileExists($archive);
852+
853+
// Extract archive
854+
$tar = new Tar();
855+
$tar->open($archive);
856+
$tar->extract($extract);
857+
$tar->close();
858+
859+
// Compare file properties
860+
foreach ($input as $originalPath) {
861+
$filename = basename($originalPath);
862+
$extractedPath = $extract . '/' . $filename;
863+
864+
$this->assertFileExists($extractedPath, "Extracted file should exist: $filename");
865+
866+
// Compare file sizes
867+
$originalSize = filesize($originalPath);
868+
$extractedSize = filesize($extractedPath);
869+
$this->assertEquals($originalSize, $extractedSize, "File size should match for: $filename");
870+
871+
// Compare file contents
872+
$originalContent = file_get_contents($originalPath);
873+
$extractedContent = file_get_contents($extractedPath);
874+
$this->assertEquals($originalContent, $extractedContent, "File content should match for: $filename");
875+
876+
// Compare modification times (allow small difference due to tar format limitations)
877+
$originalMtime = filemtime($originalPath);
878+
$extractedMtime = filemtime($extractedPath);
879+
$this->assertLessThanOrEqual(1, abs($originalMtime - $extractedMtime),
880+
"Modification time should be preserved (within 1 second) for: $filename");
881+
882+
// Compare file permissions (only on Unix-like systems)
883+
if (DIRECTORY_SEPARATOR === '/') {
884+
$originalPerms = fileperms($originalPath) & 0777;
885+
$extractedPerms = fileperms($extractedPath) & 0777;
886+
$this->assertEquals($originalPerms, $extractedPerms,
887+
"File permissions should match for: $filename");
888+
}
889+
}
890+
891+
self::RDelete($extract);
892+
unlink($archive);
893+
}
894+
832895
/**
833896
* recursive rmdir()/unlink()
834897
*

tests/ZipTestCase.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,68 @@ public function testUmlautWindows()
560560
$this->assertFileExists("$out/täst.txt");
561561
}
562562

563+
/**
564+
* Create an archive, extract it, and compare file properties
565+
*/
566+
public function testFilePropertiesPreservation()
567+
{
568+
$input = glob($this->getDir() . '/../src/*');
569+
$archive = sys_get_temp_dir() . '/dwtartest' . md5(time()) . '.zip';
570+
$extract = sys_get_temp_dir() . '/dwtartest' . md5(time() + 1);
571+
572+
// Create archive
573+
$zip = new Zip();
574+
$zip->create($archive);
575+
foreach ($input as $path) {
576+
$file = basename($path);
577+
$zip->addFile($path, $file);
578+
}
579+
$zip->close();
580+
$this->assertFileExists($archive);
581+
582+
// Extract archive
583+
$zip = new Zip();
584+
$zip->open($archive);
585+
$zip->extract($extract);
586+
$zip->close();
587+
588+
// Compare file properties
589+
foreach ($input as $originalPath) {
590+
$filename = basename($originalPath);
591+
$extractedPath = $extract . '/' . $filename;
592+
593+
$this->assertFileExists($extractedPath, "Extracted file should exist: $filename");
594+
595+
// Compare file sizes
596+
$originalSize = filesize($originalPath);
597+
$extractedSize = filesize($extractedPath);
598+
$this->assertEquals($originalSize, $extractedSize, "File size should match for: $filename");
599+
600+
// Compare file contents
601+
$originalContent = file_get_contents($originalPath);
602+
$extractedContent = file_get_contents($extractedPath);
603+
$this->assertEquals($originalContent, $extractedContent, "File content should match for: $filename");
604+
605+
// Compare modification times (allow small difference due to tar format limitations)
606+
$originalMtime = filemtime($originalPath);
607+
$extractedMtime = filemtime($extractedPath);
608+
$this->assertLessThanOrEqual(1, abs($originalMtime - $extractedMtime),
609+
"Modification time should be preserved (within 1 second) for: $filename");
610+
611+
// Compare file permissions (only on Unix-like systems)
612+
if (DIRECTORY_SEPARATOR === '/') {
613+
$originalPerms = fileperms($originalPath) & 0777;
614+
$extractedPerms = fileperms($extractedPath) & 0777;
615+
$this->assertEquals($originalPerms, $extractedPerms,
616+
"File permissions should match for: $filename");
617+
}
618+
}
619+
620+
self::RDelete($extract);
621+
unlink($archive);
622+
}
623+
624+
563625
/**
564626
* recursive rmdir()/unlink()
565627
*

0 commit comments

Comments
 (0)