Skip to content

Commit

Permalink
test UnzipStepRunner with resource manager mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
reimic committed Mar 2, 2024
1 parent c2df397 commit 0b2e611
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 61 deletions.
6 changes: 0 additions & 6 deletions src/WordPress/Blueprints/Resources/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,9 @@ public function enqueue( array $resourceDeclarations ) {
}

public function getStream( $key ) {
// TODO how would this have a random zip file in it's map?
return $this->map[ $key ];
}

public function isManagedStream ( $key )
{
return $this->map->offsetExists($key);
}

public function bufferToTemporaryFile( $resource, $callback, $suffix = null ) {
$fp = $this->getStream( $resource );
$path = $this->fs->tempnam( sys_get_temp_dir(), 'resource', $suffix );
Expand Down
12 changes: 1 addition & 11 deletions src/WordPress/Blueprints/Runner/Step/UnzipStepRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace WordPress\Blueprints\Runner\Step;

use PHPUnit\Exception;
use WordPress\Blueprints\BlueprintException;
use WordPress\Blueprints\Model\DataClass\UnzipStep;
use WordPress\Blueprints\Progress\Tracker;
Expand All @@ -26,17 +25,8 @@ public function run(
}

$resolved_to_path = $this->getRuntime()->resolvePath( $input->extract_to_path );

// TODO - does it make any sense at all to expect it is initially part of the resource map?
if ( $this->isManagedResource( $input->zip_file ) ) {
$resolved_resource = $this->getResource( $input->zip_file );
} else {
$resolved_resource = $input->zip_file;
}

// TODO - How does parsing work? Why and how is the stream initialized earlier and not here?
try {
zip_extract_to( $resolved_resource, $resolved_to_path );
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 );
}
Expand Down
28 changes: 7 additions & 21 deletions tests/Blueprints/Runner/Step/UnzipStepRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ public function before() {
$this->document_root = Path::makeAbsolute( 'test', sys_get_temp_dir() );
$this->runtime = new NativePHPRuntime( $this->document_root );

$resource_manager = new ResourceManager( new FilesystemResourceResolver() );
$this->step = new UnzipStepRunner();
$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 );

Expand All @@ -60,8 +63,7 @@ public function after() {

public function testUnzipFileWhenUsingAbsolutePath(): void {
$input = new UnzipStep();
// TODO discuss WP_Filesystem use or style rule change
$zip = fopen( __DIR__ . '\test.zip', 'rb' );
$zip = __DIR__ . '\test.zip';
$input->setZipFile( $zip );
$relative_path = 'dir';
$absolute_path = $this->runtime->resolvePath( $relative_path );
Expand All @@ -74,8 +76,7 @@ public function testUnzipFileWhenUsingAbsolutePath(): void {

public function testUnzipFileWhenUsingRelativePath(): void {
$input = new UnzipStep();
// TODO discuss WP_Filesystem use or style rule change
$zip = fopen( __DIR__ . '\test.zip', 'rb' );
$zip = __DIR__ . '\test.zip';
$input->setZipFile( $zip );
$relative_path = 'dir';
$input->setExtractToPath( $relative_path );
Expand All @@ -85,19 +86,4 @@ public function testUnzipFileWhenUsingRelativePath(): void {
$absolute_path = $this->runtime->resolvePath( $relative_path );
$this->assertDirectoryExists( $absolute_path );
}

public function testThrowExceptionWhenZipFileNotStream(): void {
$input = new UnzipStep();
// TODO discuss WP_Filesystem use or style rule change
$zip = __DIR__ . '\test.zip';
$input->setZipFile( $zip );
$relative_path = 'dir';
$input->setExtractToPath( $relative_path );

$absolute_path = $this->runtime->resolvePath( $relative_path );

$this->expectException( BlueprintException::class );
$this->expectExceptionMessage( "Failed to unzip file \"$zip\"." );
$this->step->run( $input );
}
}
23 changes: 0 additions & 23 deletions tests/Unit/UnzipStep.php

This file was deleted.

0 comments on commit 0b2e611

Please sign in to comment.