Skip to content

Commit

Permalink
Fix zip compatibility issue (#94)
Browse files Browse the repository at this point in the history
### What does this PR do?
- `UnzipStepRunner` tests will now pass for all supported PHP versions
- some variables in the test were renamed for better readability and to
match linting standards

### What problem does it fix?

- the `ZipCentralDirectoryEntry` and `ZipFileEntry` had some code that
was PHP == 7.0 incompatible
- some tests for the `UnzipStepRunner` were commented out since it broke
the pipeline for PHP == 7.0

### How to test if it works?
- this PR includes tests for `UnzipStepRunner`
- all tests should pass for all supported PHP versions
  • Loading branch information
reimic authored Mar 28, 2024
1 parent 228977b commit 410c88d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
6 changes: 3 additions & 3 deletions src/WordPress/Blueprints/Runner/Step/UnzipStepRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class UnzipStepRunner extends BaseStepRunner {
* Runs the Unzip Step
*
* @param UnzipStep $input Step.
* @param Tracker $progress_tracker Tracker.
* @param Tracker $progress_tracker Tracker.
* @return void
*/
public function run(
$input,
$progress_tracker
UnzipStep $input,
Tracker $progress_tracker
) {
$progress_tracker->set( 10, 'Unzipping...' );

Expand Down
2 changes: 1 addition & 1 deletion src/WordPress/Zip/ZipCentralDirectoryEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct(
$this->versionNeeded = $versionNeeded;
$this->versionCreated = $versionCreated;
$this->firstByteAt = $firstByteAt;
$this->isDirectory = $this->path[- 1] === '/';
$this->isDirectory = substr( $this->path, -1 ) === '/';
}

public function isFileEntry() {
Expand Down
2 changes: 1 addition & 1 deletion src/WordPress/Zip/ZipFileEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function __construct(
$this->compressionMethod = $compressionMethod;
$this->generalPurpose = $generalPurpose;
$this->version = $version;
$this->isDirectory = $this->path[- 1] === '/';
$this->isDirectory = substr( $this->path, -1 ) === '/';
}

public function isFileEntry() {
Expand Down
77 changes: 37 additions & 40 deletions tests/unit/steps/UnzipStepRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace unit\steps;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnitTestCase;
use Symfony\Component\Filesystem\Filesystem;
Expand All @@ -25,17 +26,17 @@ class UnzipStepRunnerTest extends PHPUnitTestCase {
private $runtime;

/**
* @var UnzipStepRunner $step
* @var UnzipStepRunner $step_runner
*/
private $step;
private $step_runner;

/**
* @var Filesystem
*/
private $file_system;
private $filesystem;

/**
* @var Stub
* @var ResourceManager&MockObject $resource_manager resource manager mock
*/
private $resource_manager;

Expand All @@ -48,51 +49,47 @@ public function before() {

$this->resource_manager = $this->createMock( ResourceManager::class );

$this->step = new UnzipStepRunner();
$this->step->setRuntime( $this->runtime );
$this->step->setResourceManager( $this->resource_manager );
$this->step_runner = new UnzipStepRunner();
$this->step_runner->setRuntime( $this->runtime );
$this->step_runner->setResourceManager( $this->resource_manager );

$this->file_system = new Filesystem();
$this->filesystem = new Filesystem();
}

/**
* @after
*/
public function after() {
$this->file_system->remove( $this->document_root );
$this->filesystem->remove( $this->document_root );
}

public function test(){
$this->assertTrue(true); // Placeholder until true test are fixed.
public function testUnzipFileWhenUsingAbsolutePath() {
$zip = __DIR__ . '/resources/test_zip.zip';
$this->resource_manager->method( 'getStream' )
->willReturn( fopen( $zip, 'rb' ) );

$step = new UnzipStep();
$step->setZipFile( $zip );
$extracted_file_path = $this->runtime->resolvePath( 'dir/test_zip.txt' );
$step->setExtractToPath( Path::getDirectory( $extracted_file_path ) );

$this->step_runner->run( $step, new Tracker() );

self::assertFileEquals( __DIR__ . '/resources/test_zip.txt', $extracted_file_path );
}

// public function testUnzipFileWhenUsingAbsolutePath() {
// $zip = __DIR__ . '/resources/test_zip.zip';
// $this->resource_manager->method( 'getStream' )
// ->willReturn( fopen( $zip, 'rb' ) );
//
// $input = new UnzipStep();
// $input->setZipFile( $zip );
// $extracted_file_path = $this->runtime->resolvePath( 'dir/test_zip.txt' );
// $input->setExtractToPath( Path::getDirectory( $extracted_file_path ) );
//
// $this->step->run( $input, new Tracker() );
//
// $this->assertFileEquals( __DIR__ . '/resources/test_zip.txt', $extracted_file_path );
// }
//
// public function testUnzipFileWhenUsingRelativePath() {
// $zip = __DIR__ . '/resources/test_zip.zip';
// $this->resource_manager->method( 'getStream' )
// ->willReturn( fopen( $zip, 'rb' ) );
//
// $input = new UnzipStep();
// $input->setZipFile( $zip );
// $input->setExtractToPath( 'dir' );
//
// $this->step->run( $input, new Tracker() );
//
// $extracted_file_path = $this->runtime->resolvePath( 'dir/test_zip.txt' );
// $this->assertFileEquals( __DIR__ . '/resources/test_zip.txt', $extracted_file_path );
// }
public function testUnzipFileWhenUsingRelativePath() {
$zip = __DIR__ . '/resources/test_zip.zip';
$this->resource_manager->method( 'getStream' )
->willReturn( fopen( $zip, 'rb' ) );

$step = new UnzipStep();
$step->setZipFile( $zip );
$step->setExtractToPath( 'dir' );

$this->step_runner->run( $step, new Tracker() );

$extracted_file_path = $this->runtime->resolvePath( 'dir/test_zip.txt' );
self::assertFileEquals( __DIR__ . '/resources/test_zip.txt', $extracted_file_path );
}
}

0 comments on commit 410c88d

Please sign in to comment.