Skip to content

Commit

Permalink
Refactor MkdirStepRunner and test it (#92)
Browse files Browse the repository at this point in the history
`MkdirStepRunner` will now:
- handle both relative and absolute paths
- throw an exception if the directory it wants to create already exists
- throw an original Symfony Filesystem `IOException` not rewrapping it
into a `BlueprintException`, as agreed in #65

### What problem does it fix?
- until now the runner would only handle absolute paths
- has thrown an exception in a bad way
- we use this step in the `examples` directory, and since it only
accepted absolute paths and we have provided it a relative path the
example was flawed

### How to test if it works?
- this PR includes a full suite of tests for this runner
  • Loading branch information
reimic authored Apr 3, 2024
1 parent 425106c commit 9bfbb0b
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/WordPress/Blueprints/Runner/Step/MkdirStepRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

namespace WordPress\Blueprints\Runner\Step;

use Symfony\Component\Filesystem\Filesystem;
use WordPress\Blueprints\BlueprintException;
use WordPress\Blueprints\Model\DataClass\MkdirStep;


class MkdirStepRunner extends BaseStepRunner {

/**
* @param \WordPress\Blueprints\Model\DataClass\MkdirStep $input
* @param MkdirStep $input
*/
function run( $input ) {
// @TODO: Treat $input->path as relative path to the document root (unless it's absolute)
$success = mkdir( $input->path );
if ( ! $success ) {
throw new \Exception( "Failed to create directory at {$input->path}" );
function run( MkdirStep $input ) {
$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 9bfbb0b

Please sign in to comment.