Skip to content

Commit

Permalink
Test UnzipStepRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
reimic committed Mar 4, 2024
1 parent 80786d8 commit 8fb5513
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 48 deletions.
44 changes: 24 additions & 20 deletions src/WordPress/Blueprints/Model/DataClass/UnzipStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,64 @@

namespace WordPress\Blueprints\Model\DataClass;

class UnzipStep implements StepDefinitionInterface
{
class UnzipStep implements StepDefinitionInterface {

public const DISCRIMINATOR = 'unzip';

/** @var Progress */
public $progress;
public Progress $progress;

/** @var bool */
public $continueOnError;

/** @var string */
public $step = 'unzip';
/**
* Step
*
* @var string
*/
public string $step = 'unzip';

/** @var string|ResourceDefinitionInterface */
public $zipFile;
/**
* The file to extract
*
* @var string|ResourceDefinitionInterface
*/
public $zip_file;

/**
* The path to extract the zip file to
*
* @var string
*/
public $extractToPath;
public string $extract_to_path;


public function setProgress(Progress $progress)
{
public function setProgress( Progress $progress ) {
$this->progress = $progress;
return $this;
}


public function setContinueOnError(bool $continueOnError)
{
public function setContinueOnError( bool $continueOnError ) {
$this->continueOnError = $continueOnError;
return $this;
}


public function setStep(string $step)
{
public function setStep( string $step ) {
$this->step = $step;
return $this;
}


public function setZipFile($zipFile)
{
$this->zipFile = $zipFile;
public function setZipFile( $zip_file ) {
$this->zip_file = $zip_file;
return $this;
}


public function setExtractToPath(string $extractToPath)
{
$this->extractToPath = $extractToPath;
public function setExtractToPath( string $extract_to_path ) {
$this->extract_to_path = $extract_to_path;
return $this;
}
}
22 changes: 17 additions & 5 deletions src/WordPress/Blueprints/Runner/Step/UnzipStepRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,33 @@

namespace WordPress\Blueprints\Runner\Step;

use WordPress\Blueprints\BlueprintException;
use WordPress\Blueprints\Model\DataClass\UnzipStep;
use WordPress\Blueprints\Progress\Tracker;
use function WordPress\Zip\zip_extract_to;

class UnzipStepRunner extends BaseStepRunner {

/**
* Runs the Unzip Step
*
* @param UnzipStep $input Step.
* @param Tracker|null $progress Tracker.
* @return void
*/
public function run(
UnzipStep $input,
Tracker $progress = null
) {
$progress?->set( 10, 'Unzipping...' );
if ( null !== $progress ) {
$progress->set( 10, 'Unzipping...' );
}

// @TODO: Expose a generic helper method for this, e.g. $this->getExecutionContext()->resolvePath($input->extractToPath);
$toPath = $this->getRuntime()->getDocumentRoot() . '/' . $input->extractToPath;
zip_extract_to( $this->getResource( $input->zipFile ), $toPath );
$resolved_to_path = $this->getRuntime()->resolvePath( $input->extract_to_path );
try {
zip_extract_to( $this->getResource( $input->zip_file ), $resolved_to_path );
} catch ( \Throwable $exception ) {
throw new BlueprintException( "Failed to unzip file \"$input->zip_file\".", 0, $exception );
}
}

}
89 changes: 89 additions & 0 deletions tests/Blueprints/Runner/Step/UnzipStepRunnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Blueprints\Runner\Step;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use WordPress\Blueprints\BlueprintException;
use WordPress\Blueprints\Model\DataClass\FilesystemResource;
use WordPress\Blueprints\Model\DataClass\UnzipStep;
use WordPress\Blueprints\Resources\Resolver\FilesystemResourceResolver;
use WordPress\Blueprints\Resources\ResourceManager;
use WordPress\Blueprints\Runner\Step\UnzipStepRunner;
use PHPUnit\Framework\TestCase;
use WordPress\Blueprints\Runtime\NativePHPRuntime;

class UnzipStepRunnerTest extends TestCase {


/**
* @var string
*/
private string $document_root;

/**
* @var NativePHPRuntime
*/
private NativePHPRuntime $runtime;

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

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

/**
* @before
*/
public function before() {
$this->document_root = Path::makeAbsolute( 'test', sys_get_temp_dir() );
$this->runtime = new NativePHPRuntime( $this->document_root );

$resource_manager = $this->createStub( ResourceManager::class );
$resource_manager->method( 'getStream' )
->willReturn( fopen( __DIR__ . '\test.zip', 'rb' ) );

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

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

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

public function testUnzipFileWhenUsingAbsolutePath(): void {
$input = new UnzipStep();
$zip = __DIR__ . '\test.zip';
$input->setZipFile( $zip );
$relative_path = 'dir';
$absolute_path = $this->runtime->resolvePath( $relative_path );
$input->setExtractToPath( $absolute_path );

$this->step->run( $input );

$this->assertDirectoryExists( $absolute_path );
}

public function testUnzipFileWhenUsingRelativePath(): void {
$input = new UnzipStep();
$zip = __DIR__ . '\test.zip';
$input->setZipFile( $zip );
$relative_path = 'dir';
$input->setExtractToPath( $relative_path );

$this->step->run( $input );

$absolute_path = $this->runtime->resolvePath( $relative_path );
$this->assertDirectoryExists( $absolute_path );
}
}
File renamed without changes.
23 changes: 0 additions & 23 deletions tests/Unit/UnzipStep.php

This file was deleted.

0 comments on commit 8fb5513

Please sign in to comment.