Skip to content

Commit 72c4df1

Browse files
committed
Get Blueprints unit tests to work with the new API
1 parent 7ef392a commit 72c4df1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1337
-2816
lines changed

components/Blueprints/DataReference/DataReferenceResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function resolve( DataReference $reference, ?Tracker $progress_tracker =
9393
return new File( $tracked_stream, basename( $path ) );
9494
} elseif ( $this->executionContext->is_dir( $path ) ) {
9595
// @TODO (low priority): Actually track the download progress for directories.
96-
$this->subTrackers[ $reference->id ]->finish();
96+
$progress_tracker->finish();
9797

9898
return new Directory(
9999
new ChrootLayer( $this->executionContext, $path ),

components/Blueprints/ProgressObserver.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ class ProgressObserver {
2020
*
2121
* @param callable $logCallback Function that receives progress updates
2222
*/
23-
public function __construct( callable $logCallback ) {
24-
$this->logCallback = $logCallback;
23+
public function __construct( ?callable $logCallback = null ) {
24+
$this->logCallback = $logCallback ?? function() {
25+
// noop
26+
};
2527
}
2628

2729
/**

components/Blueprints/Runner.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ class Runner {
5252
private DataReferenceResolver $assets;
5353
private Filesystem $blueprintExecutionContext;
5454
private array $blueprintArray;
55-
private array $dataReferences;
55+
private array $dataReferences = [];
5656
private ?VersionConstraint $phpVersionConstraint;
5757
private Tracker $mainTracker;
5858
private ProgressObserver $progressObserver;
59+
public ?Runtime $runtime;
5960

6061
public function __construct( private RunnerConfiguration $configuration ) {
6162
$this->client = new Client( [
@@ -72,11 +73,7 @@ public function __construct( private RunnerConfiguration $configuration ) {
7273
$this->mainTracker = new Tracker();
7374

7475
// Set up progress logging
75-
$this->progressObserver = $configuration->getProgressObserver() ?? new ProgressObserver(
76-
function ( $progress, $caption ) {
77-
fprintf( STDERR, "[%3d%%] %s\n", $progress, $caption );
78-
}
79-
);
76+
$this->progressObserver = $configuration->getProgressObserver() ?? new ProgressObserver();
8077
$this->progressObserver->attachTo( $this->mainTracker );
8178
}
8279

@@ -106,7 +103,7 @@ public function run(): void {
106103
$targetResolutionStage->setCaption( 'Resolving target site' );
107104

108105
$targetSiteFs = LocalFilesystem::create( $this->configuration->getTargetSiteRoot() );
109-
$runtime = new Runtime(
106+
$this->runtime = new Runtime(
110107
$targetSiteFs,
111108
$this->configuration,
112109
$this->assets,
@@ -116,15 +113,15 @@ public function run(): void {
116113
);
117114

118115
if ( $this->configuration->getExecutionMode() === 'apply-to-existing-site' ) {
119-
ExistingSiteResolver::resolve( $runtime, $targetResolutionStage );
116+
ExistingSiteResolver::resolve( $this->runtime, $targetResolutionStage );
120117
} else {
121-
NewSiteResolver::resolve( $runtime, $targetResolutionStage );
118+
NewSiteResolver::resolve( $this->runtime, $targetResolutionStage );
122119
}
123120
$targetResolutionStage->finish();
124121

125122
$plan = $this->createExecutionPlan();
126123
$this->assets->startEagerResolution( $this->dataReferences, $dataResolutionStage );
127-
$this->executePlan( $executionStage, $plan, $runtime );
124+
$this->executePlan( $executionStage, $plan, $this->runtime );
128125
} finally {
129126
LocalFilesystem::create( $tempRoot )->rmdir( '/', [
130127
'recursive' => true,
@@ -160,8 +157,8 @@ private function loadBlueprint() {
160157
$stream = $resolved->stream;
161158

162159
if ( is_zip_file_stream( $stream ) ) {
163-
$this->blueprintExecutionContext = new ZipFilesystem( $stream );
164160
$blueprintString = $this->blueprintExecutionContext->get_contents( '/blueprint.json' );
161+
$this->blueprintExecutionContext = $this->configuration->getExecutionContext() ?? new ZipFilesystem( $stream );
165162
} else {
166163
// JSON file
167164
$blueprintString = $stream->consume_all();
@@ -171,16 +168,16 @@ private function loadBlueprint() {
171168
// It was resolved as an ExecutionContextPath, but it's actually a local
172169
// filesystem path at this point.
173170
// The execution context is the directory containing the blueprint.json file.
174-
$this->blueprintExecutionContext = LocalFilesystem::create( dirname( $reference->get_path() ) );
171+
$this->blueprintExecutionContext = $this->configuration->getExecutionContext() ?? LocalFilesystem::create( dirname( $reference->get_path() ) );
175172
} elseif ( $reference instanceof InlineFile ) {
176-
$this->blueprintExecutionContext = InMemoryFilesystem::create();
173+
$this->blueprintExecutionContext = $this->configuration->getExecutionContext() ?? InMemoryFilesystem::create();
177174
} else {
178175
throw new \Exception( 'Unsupported blueprint reference type: ' . get_class( $reference ) );
179176
}
180177
}
181178
} elseif ( $resolved instanceof Directory ) {
182-
$this->blueprintExecutionContext = $resolved->filesystem;
183-
$blueprintString = $this->blueprintExecutionContext->get_contents( '/blueprint.json' );
179+
$blueprintString = $resolved->filesystem->get_contents( '/blueprint.json' );
180+
$this->blueprintExecutionContext = $this->configuration->getExecutionContext() ?? $resolved->filesystem;
184181
} else {
185182
throw new \Exception( 'Invalid blueprint reference' );
186183
}

components/Blueprints/Runtime.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public function getTargetFilesystem(): Filesystem {
4949
return $this->targetFs;
5050
}
5151

52+
public function getTempRoot(): string {
53+
return $this->tempRoot;
54+
}
55+
5256
public function getDataReferenceResolver(): DataReferenceResolver {
5357
return $this->assets;
5458
}

components/Blueprints/Steps/InstallPluginStep.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class InstallPluginStep implements StepInterface {
4242
* @param DataReference $source Plugin source reference.
4343
* @param bool $active Activate after install?
4444
* @param array<string, mixed>|null $activationOptions Optional activation data.
45-
* @param PluginErrorBehavior $onError Error handling behavior.
45+
* @param string $onError Error handling behavior.
4646
*/
4747
public function __construct(
4848
DataReference $source,
@@ -75,7 +75,6 @@ public function run( Runtime $runtime, Tracker $tracker ) {
7575

7676
if ( is_zip_file_stream( $plugin_data->stream ) ) {
7777
pipe_stream( $plugin_data->stream, $zip_stream );
78-
$plugin_data->stream->close_reading();
7978
} else {
8079
$zip_encoder = new ZipEncoder( $zip_stream );
8180
$zip_encoder->append_file( new FileEntry( [
@@ -85,6 +84,7 @@ public function run( Runtime $runtime, Tracker $tracker ) {
8584
] ) );
8685
$zip_encoder->close();
8786
}
87+
$plugin_data->stream->close_reading();
8888
}
8989
$zip_stream->close_writing();
9090

components/Blueprints/Steps/RmStep.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use WordPress\Blueprints\Progress\Tracker;
66
use WordPress\Blueprints\Runtime;
7+
use WordPress\Filesystem\FilesystemException;
78

89
/**
910
* Represents the 'rm' (remove file) step.

components/Blueprints/tests/bootstrap.php

Lines changed: 0 additions & 4 deletions
This file was deleted.

components/Blueprints/tests/unit/blueprint-validator/BlueprintV2ValidatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\Attributes\DataProvider;
66
use PHPUnit\Framework\TestCase;
77
use WordPress\Blueprints\BlueprintV2Validator;
8+
use WordPress\Blueprints\Validator;
89

910
/**
1011
* Tests for the BlueprintV2Validator class.
@@ -22,7 +23,7 @@ class BlueprintV2ValidatorTest extends TestCase {
2223
*/
2324
public function setUp(): void {
2425
parent::setUp();
25-
$this->validator = new BlueprintV2Validator();
26+
$this->validator = new Validator();
2627
}
2728

2829
/**

components/Blueprints/tests/unit/blueprint/BlueprintMapperTest.php

Lines changed: 0 additions & 194 deletions
This file was deleted.

components/Blueprints/tests/unit/configuration/PhpUnitTestCase.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)