Skip to content

Commit

Permalink
Fix QA
Browse files Browse the repository at this point in the history
Alse require Composer 1.5+ so we can access ZipDonwloader::extract
  • Loading branch information
gmazzap committed Aug 31, 2020
1 parent 6a1d044 commit 615dde5
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php-qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
if: "!contains(github.event.head_commit.message, 'ci skip')"
strategy:
matrix:
php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4']
php-versions: ['7.1', '7.2', '7.3', '7.4']
steps:
- uses: actions/checkout@v2

Expand Down
Binary file modified .gitignore
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WP Starter is the easiest and fastest way to bootstrap WordPress sites entirely
# System Requirements

- PHP 7.0+
- [Composer](https://getcomposer.org/) v1
- [Composer](https://getcomposer.org/) >= 1.5.0 <= 2

# License

Expand Down
61 changes: 7 additions & 54 deletions src/Util/Unzipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Composer\Config;
use Composer\Downloader\ZipDownloader;
use Composer\IO\IOInterface;
use Composer\Util\Platform;

/**
* Wrapper around Composer ZipDownloader because we need just an unzipper, not a downloader.
Expand All @@ -32,7 +31,7 @@ class Unzipper
*/
public function __construct(IOInterface $io, Config $config)
{
$this->unzipper = $this->createUnzipper($io, $config);
$this->unzipper = new ZipDownloader($io, $config);
}

/**
Expand All @@ -44,58 +43,12 @@ public function __construct(IOInterface $io, Config $config)
*/
public function unzip(string $zipPath, string $target): bool
{
/** @var callable(string, string): bool */
$unzip = [$this->unzipper, 'unzip'];
try {
$this->unzipper->extract($zipPath, $target);

return $unzip($zipPath, $target);
}

/**
* We create this anonymous class extending ZipDownloader because its extract* methods
* we are interested in are protected, and the public `extract()` method does not return.
*
* @param IOInterface $io
* @param Config $config
* @return ZipDownloader
*
* phpcs:disable Inpsyde.CodeQuality.NestingLevel
*/
private function createUnzipper(IOInterface $io, Config $config): ZipDownloader
{
// phpcs:enable Inpsyde.CodeQuality.NestingLevel

return new class ($io, $config) extends ZipDownloader
{
/**
* @var bool
*/
private $useZipArchive;

/**
* @param IOInterface $io
* @param Config $config
*/
public function __construct(IOInterface $io, Config $config)
{
$this->useZipArchive = Platform::isWindows();
parent::__construct($io, $config);
}

/**
* @param string $zipPath
* @param string $target
* @return bool
*/
public function unzip(string $zipPath, string $target): bool
{
try {
return $this->useZipArchive
? $this->extractWithZipArchive($zipPath, $target, false)
: $this->extractWithSystemUnzip($zipPath, $target, false);
} catch (\Throwable $exception) {
return false;
}
}
};
return true;
} catch (\Throwable $throwable) {
return false;
}
}
}
2 changes: 1 addition & 1 deletion tests/integration/Cli/PhpToolProcessFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function testRunWpCliCommandViaFileSystemBootstrap()
*/
private function assertProcessWorks(PhpToolProcess $process)
{
static::assertTrue($process->execute('-r "echo \'Hi!!!\'";'));
static::assertTrue($process->execute('-r "echo \'Hi!!!\';"'));

$output = trim($this->collectOutput());

Expand Down
39 changes: 15 additions & 24 deletions tests/integration/Util/UnzipperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,37 @@

namespace WeCodeMore\WpStarter\Tests\Integration\Util;

use org\bovigo\vfs\vfsStream;
use WeCodeMore\WpStarter\Tests\IntegrationTestCase;
use WeCodeMore\WpStarter\Util\Unzipper;

class UnzipperTest extends IntegrationTestCase
{
private function createUnzipper(): Unzipper
{
return new Unzipper($this->createComposerIo(), $this->createComposerConfig());
}

/**
* @covers \WeCodeMore\WpStarter\Util\Unzipper
*/
public function testUnzipFail()
{
$source = getenv('TESTS_FIXTURES_PATH') . '/faulty-zip.zip';

$dir = vfsStream::setup('directory');
$targetDir = $dir->url();
$dir = getenv('TESTS_FIXTURES_PATH');
$source = $dir . '/faulty-zip.zip';
$unzipper = new Unzipper($this->createComposerIo(), $this->createComposerConfig());

$unzipper = $this->createUnzipper();

static::assertFalse($unzipper->unzip($source, $targetDir));
static::assertFalse($dir->hasChildren());
static::assertFalse($unzipper->unzip($source, $dir));
}

/**
* @covers \WeCodeMore\WpStarter\Util\Unzipper
*/
public function testUnzipSuccess()
{
$source = getenv('TESTS_FIXTURES_PATH') . '/good-zip.zip';

$dir = vfsStream::setup('directory');
$targetDir = $dir->url();

$unzipper = $this->createUnzipper();

static::assertTrue($unzipper->unzip($source, $targetDir));
static::assertTrue($dir->hasChildren());
$dir = getenv('TESTS_FIXTURES_PATH');
$unzipper = new Unzipper($this->createComposerIo(), $this->createComposerConfig());

try {
static::assertTrue($unzipper->unzip("{$dir}/good-zip.zip", $dir));
// this is the file contained in `good-zip.zip` file
static::assertTrue(file_exists("{$dir}/some-zip-content.txt"));
} finally {
@unlink("{$dir}/some-zip-content.txt");
}
}
}
}
2 changes: 1 addition & 1 deletion tests/src/DummyPhpTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ public function prepareCommand(string $command, Paths $paths, Io $io): string

return $command;
}
}
}

0 comments on commit 615dde5

Please sign in to comment.