Skip to content

Commit

Permalink
Merge remote-tracking branch 'blueprints/trunk' into new-e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
MayPaw committed Jun 1, 2024
2 parents 8bf6a2c + 87afea1 commit 6fbcf4b
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 17 deletions.
20 changes: 10 additions & 10 deletions src/WordPress/Blueprints/BlueprintParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InvalidArgumentException;
use Opis\JsonSchema\Errors\ErrorFormatter;
use stdClass;
use WordPress\Blueprints\Model\BlueprintBuilder;
use WordPress\Blueprints\Model\DataClass\Blueprint;

Expand All @@ -27,8 +28,13 @@ public function __construct(
$this->mapper = $mapper;
}

/**
* @param Blueprint|string|stdClass $raw_blueprint
* @return Blueprint
* @throws InvalidArgumentException if unsupported input type or json string can not be decoded
*/
public function parse( $raw_blueprint ) {
if ( $raw_blueprint instanceof \stdClass ) {
if ( $raw_blueprint instanceof stdClass ) {
return $this->fromObject( $raw_blueprint );
}

Expand All @@ -43,22 +49,16 @@ public function parse( $raw_blueprint ) {
}

if ( $raw_blueprint instanceof Blueprint ) {
$blueprint = $this->fromBlueprint($raw_blueprint);
return $blueprint;
}

if ( $raw_blueprint instanceof BlueprintBuilder ) {
$blueprint1 = $this->fromBlueprint($raw_blueprint->toBlueprint());
return $blueprint1;
return $this->fromBlueprint( $raw_blueprint );
}

throw new InvalidArgumentException(
'Unsupported $rawBlueprint type. Use a JSON string, a parsed JSON object, or a BlueprintBuilder instance.'
'Unsupported $rawBlueprint type. Use a JSON string, a parsed JSON object, or a Blueprint instance.'
);
}

/**
* @param \stdClass $data
* @param stdClass $data
*/
public function fromObject( $data ) {
$result = $this->validator->validate( $data );
Expand Down
12 changes: 5 additions & 7 deletions src/WordPress/Blueprints/Runner/Step/MkdirStepRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace WordPress\Blueprints\Runner\Step;

use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use WordPress\Blueprints\BlueprintException;
use WordPress\Blueprints\Model\DataClass\MkdirStep;
Expand All @@ -14,12 +13,11 @@ class MkdirStepRunner extends BaseStepRunner {
* @param MkdirStep $input
*/
function run( MkdirStep $input ) {
$resolvedPath = $this->getRuntime()->resolvePath( $input->path );
$fileSystem = new Filesystem();
try {
$fileSystem->mkdir( $resolvedPath );
} catch ( IOException $exception ) {
throw new BlueprintException( "Failed to create a directory at \"$resolvedPath\"", 0, $exception );
$resolved_path = $this->getRuntime()->resolvePath( $input->path );
$filesystem = new Filesystem();
if ( $filesystem->exists( $resolved_path ) ) {
throw new BlueprintException( "Failed to create \"$resolved_path\": the directory exists." );
}
$filesystem->mkdir( $resolved_path );
}
}
113 changes: 113 additions & 0 deletions tests/unit/steps/MkdirStepRunnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace unit\steps;

use PHPUnitTestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use WordPress\Blueprints\Model\DataClass\MkdirStep;
use WordPress\Blueprints\Runner\Step\MkdirStepRunner;
use WordPress\Blueprints\Runtime\Runtime;
use WordPress\Blueprints\BlueprintException;

class MkdirStepRunnerTest extends PHPUnitTestCase {
/**
* @var string
*/
private $document_root;

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

/**
* @var MkdirStepRunner
*/
private $step_runner;

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

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

$this->step_runner = new MkdirStepRunner();
$this->step_runner->setRuntime( $this->runtime );

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

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

public function testCreateDirectoryWhenUsingRelativePath() {
$path = 'dir';
$step = new MkdirStep();
$step->setPath( $path );

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

$resolved_path = $this->runtime->resolvePath( $path );
self::assertDirectoryExists( $resolved_path );
}

public function testCreateDirectoryWhenUsingAbsolutePath() {
$relative_path = 'dir';
$absolute_path = $this->runtime->resolvePath( $relative_path );

$step = new MkdirStep();
$step->setPath( $absolute_path );

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

self::assertDirectoryExists( $absolute_path );
}

public function testCreateDirectoryRecursively() {
$path = 'dir/subdir';
$step = new MkdirStep();
$step->setPath( $path );

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

$resolved_path = $this->runtime->resolvePath( $path );
self::assertDirectoryExists( $resolved_path );
}

public function testCreateReadableAndWritableDirectory() {
$path = 'dir';
$step = new MkdirStep();
$step->setPath( $path );

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

$resolved_path = $this->runtime->resolvePath( $path );
self::assertDirectoryExists( $resolved_path );
self::assertDirectoryIsWritable( $resolved_path );
self::assertDirectoryIsReadable( $resolved_path );
}

public function testThrowExceptionWhenCreatingDirectoryAndItAlreadyExists() {
$path = 'dir';
$resolved_path = $this->runtime->resolvePath( $path );
$this->filesystem->mkdir( $resolved_path );

$step = new MkdirStep();
$step->setPath( $path );

self::expectException( BlueprintException::class );
self::expectExceptionMessage( "Failed to create \"$resolved_path\": the directory exists." );
$this->step_runner->run( $step );
}
}

0 comments on commit 6fbcf4b

Please sign in to comment.