-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor MkdirStepRunner and test it (#92)
`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
Showing
2 changed files
with
122 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |