Browse Source

et les derniers T.U.

pull/4416/head
JamesRezo 10 months ago
parent
commit
de147ef1f2
  1. 16
      src/SpipArchiver.php
  2. 100
      tests/SpipArchiverTest.php

16
src/SpipArchiver.php

@ -55,8 +55,10 @@ class SpipArchiver extends AbstractArchiver implements ArchiverInterface
$archive = $this->archiveEnLecture();
if ($archive) {
if (1 === $archive->open($this->fichier_archive, 'lecture')) {
return $archive->extractTo($destination, $fichiers);
//$archive->close();
$retour = $archive->extractTo($destination, $fichiers);
$archive->close();
return $retour;
}
$this->setErreur(1);
@ -84,18 +86,14 @@ class SpipArchiver extends AbstractArchiver implements ArchiverInterface
}
$archive = $this->getArchive();
if ($archive) {
$retour = false;
if (1 === $archive->open($this->fichier_archive, 'creation')) {
$retour = $archive->compress($source, $fichiers);
$archive->close();
if (!$retour) {
$this->setErreur(1);
}
return $retour;
}
$this->setErreur(0);
$this->setErreur(intval(!$retour));
return true;
return $retour;
}
}

100
tests/SpipArchiverTest.php

@ -21,6 +21,26 @@ class SpipArchiverTest extends TestCase
{
@mkdir(__DIR__ . '/../var/tmp/directory');
@file_put_contents(__DIR__ . '/../var/tmp/directory/test.txt', 'contenu de test');
$test_retirer_zip = new \ZipArchive();
$test_retirer_zip->open(__DIR__ . '/../var/tmp/retirer.zip', \ZipArchive::CREATE);
$test_retirer_zip->addFromString('test.txt', 'contenu de test');
$test_retirer_zip->close();
@mkdir(__DIR__ . '/../var/tmp/tar/directory');
@file_put_contents(__DIR__ . '/../var/tmp/tar/directory/test.txt', 'contenu de test');
@mkdir(__DIR__ . '/../var/tmp/tar/directory/sub_directory');
@file_put_contents(__DIR__ . '/../var/tmp/tar/directory/sub_directory/test2.txt', 'contenu de test2');
$test_retirer_tar = new \Spip\Archiver\TarArchive();
$test_retirer_tar->open(__DIR__ . '/../var/tmp/tar/retirer.tar', 'creation');
$test_retirer_tar->compress(__DIR__ . '/../var/tmp/tar/directory', [
__DIR__ . '/../var/tmp/tar/directory/test.txt',
__DIR__ . '/../var/tmp/tar/directory/sub_directory/test2.txt',
]);
@unlink(__DIR__ . '/../var/tmp/tar/directory/test.txt');
@unlink(__DIR__ . '/../var/tmp/tar/directory/sub_directory/test2.txt');
@rmdir(__DIR__ . '/../var/tmp/tar/directory/sub_directory');
@rmdir(__DIR__ . '/../var/tmp/tar/directory');
}
public static function tearDownAfterClass(): void
@ -34,6 +54,9 @@ class SpipArchiverTest extends TestCase
@rmdir(__DIR__ . '/../var/tmp/directory');
@unlink(__DIR__ . '/../var/tmp/emballer.zip');
@unlink(__DIR__ . '/../var/tmp/emballer2.zip');
@unlink(__DIR__ . '/../var/tmp/tar/emballer.tar');
@unlink(__DIR__ . '/../var/tmp/retirer.zip');
@unlink(__DIR__ . '/../var/tmp/tar/retirer.tar');
}
public function dataInformer()
@ -154,6 +177,14 @@ class SpipArchiverTest extends TestCase
__DIR__ . '/../var/tmp',
[],
__DIR__ . '/../var/tmp/test.txt',
],
'destination-not-exists' => [
false,
5,
__DIR__ . '/../var/tmp/test.zip',
'',
[__DIR__ . '/../var/tmp/directory/test.txt'],
__DIR__ . '/../var/tmp/directory' . md5(mt_rand()),
],
'tar' => [
true,
@ -188,12 +219,28 @@ class SpipArchiverTest extends TestCase
//Then
$this->assertEquals($expected, $actual, 'decompress ok');
$this->assertEquals($expectedErreur, $archiver->erreur(), 'error code');
$this->assertTrue(file_exists($testFile), 'decompressed file exists');
$this->assertTrue($expectedErreur === 5 || file_exists($testFile), 'decompressed file exists');
}
public function dataEmballer()
{
return [
'exists' => [
false,
6,
__DIR__ . '/../var/tmp/test.zip',
'',
[__DIR__ . '/../var/tmp/directory/test.txt'],
__DIR__ . '/../var/tmp/directory',
],
'source-not-exists' => [
false,
7,
__DIR__ . '/../var/tmp/test.zip',
'',
[__DIR__ . '/../var/tmp/directory/test.txt'],
__DIR__ . '/../var/tmp/directory' . md5(mt_rand()),
],
'zip' => [
true,
0,
@ -210,6 +257,14 @@ class SpipArchiverTest extends TestCase
[__DIR__ . '/../var/tmp/directory/test.txt'],
null,
],
'tar' => [
true,
0,
__DIR__ . '/../var/tmp/tar/emballer.tar',
'',
[__DIR__ . '/../var/tmp/directory/test.txt'],
__DIR__ . '/../var/tmp/directory',
],
];
}
@ -229,4 +284,47 @@ class SpipArchiverTest extends TestCase
$this->assertEquals($expectedErreur, $archiver->erreur(), 'error code');
$this->assertTrue(file_exists($file), 'compressed file exists');
}
public function dataRetirer()
{
return [
'not-exists' => [
false,
3,
md5(mt_rand()),
'zip',
['test.txt'],
],
'zip' => [
true,
0,
__DIR__ . '/../var/tmp/retirer.zip',
'',
['test.txt'],
],
'tar' => [
true,
0,
__DIR__ . '/../var/tmp/tar/retirer.tar',
'',
['directory/test.txt'],
],
];
}
/**
* @dataProvider dataRetirer
*/
public function testRetirer($expected, $expectedErreur, $file, $extension, $files)
{
// Given
$archiver = new SpipArchiver($file, $extension);
// When
$actual = $archiver->retirer($files);
//Then
$this->assertEquals($expected, $actual, 'remove ok');
$this->assertEquals($expectedErreur, $archiver->erreur(), 'error code');
}
}

Loading…
Cancel
Save