Skip to content

Commit

Permalink
Clean up RmStepRunner and its tests (#95)
Browse files Browse the repository at this point in the history
### What does this PR do?
- removes the try / catch block from the `RmStepRunner`
- renames some variables in the test class for better readability and to
match linting standards

### What problem does it fix?
- the runner was catching IOExceptions, but we agreed it shouldn't #65
- some variables could've been named better

### How to test if it works?
- this PR includes tests for `RmStepRunner`
- all tests should pass for all supported PHP versions
  • Loading branch information
reimic authored Mar 28, 2024
1 parent 410c88d commit 425106c
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 154 deletions.
16 changes: 6 additions & 10 deletions src/WordPress/Blueprints/Runner/Step/RmStepRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ class RmStepRunner extends BaseStepRunner {
/**
* @param RmStep $input
*/
public function run( $input ) {
$resolvedPath = $this->getRuntime()->resolvePath( $input->path );
$fileSystem = new Filesystem();
if ( false === $fileSystem->exists( $resolvedPath ) ) {
throw new BlueprintException( "Failed to remove \"$resolvedPath\": the directory or file does not exist." );
}
try {
$fileSystem->remove( $resolvedPath );
} catch ( IOException $exception ) {
throw new BlueprintException( "Failed to remove the directory or file at \"$resolvedPath\"", 0, $exception );
public function run( RmStep $input ) {
$resolved_path = $this->getRuntime()->resolvePath( $input->path );
$filesystem = new Filesystem();
if ( false === $filesystem->exists( $resolved_path ) ) {
throw new BlueprintException( "Failed to remove \"$resolved_path\": the directory or file does not exist." );
}
$filesystem->remove( $resolved_path );
}
}
276 changes: 132 additions & 144 deletions tests/unit/steps/RmStepRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,166 +10,154 @@
use WordPress\Blueprints\Runner\Step\RmStepRunner;
use WordPress\Blueprints\Runtime\Runtime;

class RmStepRunnerTest extends PHPUnitTestCase
{
/**
* @var string
*/
private $documentRoot;

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

/**
* @var RmStepRunner
*/
private $step;

/**
* @var Filesystem
*/
private $fileSystem;
class RmStepRunnerTest extends PHPUnitTestCase {
/**
* @var string
*/
private $document_root;

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

$this->step = new RmStepRunner();
$this->step->setRuntime($this->runtime);
/**
* @var RmStepRunner
*/
private $step_runner;

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

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

public function testRemoveDirectoryWhenUsingAbsolutePath()
{
$absolutePath = $this->runtime->resolvePath("dir");
$this->fileSystem->mkdir($absolutePath);

$input = new RmStep();
$input->path = $absolutePath;

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

$this->assertDirectoryDoesNotExist($absolutePath);
}

public function testRemoveDirectoryWhenUsingRelativePath()
{
$relativePath = "dir";
$absolutePath = $this->runtime->resolvePath($relativePath);
$this->fileSystem->mkdir($absolutePath);

$input = new RmStep();
$input->path = $relativePath;

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

$this->assertDirectoryDoesNotExist($absolutePath);
}

public function testRemoveDirectoryWithSubdirectory()
{
$relativePath = "dir/subdir";
$absolutePath = $this->runtime->resolvePath($relativePath);
$this->fileSystem->mkdir($absolutePath);

$input = new RmStep();
$input->path = dirname($relativePath);

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

$this->assertDirectoryDoesNotExist($absolutePath);
}

public function testRemoveDirectoryWithFile()
{
$relativePath = "dir/file.txt";
$absolutePath = $this->runtime->resolvePath($relativePath);
$this->fileSystem->dumpFile($absolutePath, "test");

$input = new RmStep();
$input->path = dirname($relativePath);

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

$this->assertDirectoryDoesNotExist(dirname($absolutePath));
}

public function testRemoveFile()
{
$relativePath = "file.txt";
$absolutePath = $this->runtime->resolvePath($relativePath);
$this->fileSystem->dumpFile($absolutePath, "test");
/**
* @var Filesystem
*/
private $filesystem;

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

$this->step->run($input);
$this->step_runner = new RmStepRunner();
$this->step_runner->setRuntime( $this->runtime );

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

public function testThrowExceptionWhenRemovingNonexistentDirectoryAndUsingRelativePath()
{
$relativePath = "dir";
$absolutePath = $this->runtime->resolvePath($relativePath);
/**
* @after
*/
public function after() {
$this->filesystem->remove( $this->document_root );
}

$input = new RmStep();
$input->path = $relativePath;
public function testRemoveDirectoryWhenUsingAbsolutePath() {
$absolute_path = $this->runtime->resolvePath( "dir" );
$this->filesystem->mkdir( $absolute_path );

$this->expectException(BlueprintException::class);
$this->expectExceptionMessage("Failed to remove \"$absolutePath\": the directory or file does not exist.");
$this->step->run($input);
}
$step = new RmStep();
$step->path = $absolute_path;

public function testThrowExceptionWhenRemovingNonexistentDirectoryAndUsingAbsolutePath()
{
$absolutePath = "/dir";
$this->step_runner->run( $step );

$input = new RmStep();
$input->path = $absolutePath;
self::assertDirectoryDoesNotExist( $absolute_path );
}

$this->expectException(BlueprintException::class);
$this->expectExceptionMessage("Failed to remove \"$absolutePath\": the directory or file does not exist.");
$this->step->run($input);
}
public function testRemoveDirectoryWhenUsingRelativePath() {
$relative_path = "dir";
$absolute_path = $this->runtime->resolvePath( $relative_path );
$this->filesystem->mkdir( $absolute_path );

public function testThrowExceptionWhenRemovingNonexistentFileAndUsingAbsolutePath()
{
$relativePath = "/file.txt";
$step = new RmStep();
$step->path = $relative_path;

$input = new RmStep();
$input->path = $relativePath;
$this->step_runner->run( $step );

$this->expectException(BlueprintException::class);
$this->expectExceptionMessage("Failed to remove \"$relativePath\": the directory or file does not exist.");
$this->step->run($input);
}
self::assertDirectoryDoesNotExist( $absolute_path );
}

public function testThrowExceptionWhenRemovingNonexistentFileAndUsingRelativePath()
{
$relativePath = "file.txt";
$absolutePath = $this->runtime->resolvePath($relativePath);
public function testRemoveDirectoryWithSubdirectory() {
$relative_path = "dir/subdir";
$absolute_path = $this->runtime->resolvePath( $relative_path );
$this->filesystem->mkdir( $absolute_path );

$input = new RmStep();
$input->path = $relativePath;
$step = new RmStep();
$step->path = dirname( $relative_path );

$this->expectException(BlueprintException::class);
$this->expectExceptionMessage("Failed to remove \"$absolutePath\": the directory or file does not exist.");
$this->step->run($input);
}
$this->step_runner->run( $step );

self::assertDirectoryDoesNotExist( $absolute_path );
}

public function testRemoveDirectoryWithFile() {
$relative_path = "dir/file.txt";
$absolute_pPath = $this->runtime->resolvePath( $relative_path );
$this->filesystem->dumpFile( $absolute_pPath, "test" );

$step = new RmStep();
$step->path = dirname( $relative_path );

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

self::assertDirectoryDoesNotExist( dirname( $absolute_pPath ) );
}

public function testRemoveFile() {
$relative_path = "file.txt";
$absolute_path = $this->runtime->resolvePath( $relative_path );
$this->filesystem->dumpFile( $absolute_path, "test" );

$step = new RmStep();
$step->path = $relative_path;

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

self::assertDirectoryDoesNotExist( $absolute_path );
}

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

$step = new RmStep();
$step->path = $relative_path;

self::expectException( BlueprintException::class );
self::expectExceptionMessage( "Failed to remove \"$absolute_path\": the directory or file does not exist." );
$this->step_runner->run( $step );
}

public function testThrowExceptionWhenRemovingNonexistentDirectoryAndUsingAbsolutePath() {
$absolute_path = "/dir";

$step = new RmStep();
$step->path = $absolute_path;

self::expectException( BlueprintException::class );
self::expectExceptionMessage( "Failed to remove \"$absolute_path\": the directory or file does not exist." );
$this->step_runner->run( $step );
}

public function testThrowExceptionWhenRemovingNonexistentFileAndUsingAbsolutePath() {
$relative_path = "/file.txt";

$step = new RmStep();
$step->path = $relative_path;

self::expectException( BlueprintException::class );
self::expectExceptionMessage( "Failed to remove \"$relative_path\": the directory or file does not exist." );
$this->step_runner->run( $step );
}

public function testThrowExceptionWhenRemovingNonexistentFileAndUsingRelativePath() {
$relativePath = "file.txt";
$absolutePath = $this->runtime->resolvePath($relativePath);

$step = new RmStep();
$step->path = $relativePath;

self::expectException( BlueprintException::class );
self::expectExceptionMessage( "Failed to remove \"$absolutePath\": the directory or file does not exist." );
$this->step_runner->run( $step );
}
}

0 comments on commit 425106c

Please sign in to comment.