Skip to content

Commit a3a9153

Browse files
committed
test: add and use directory factory
1 parent 20daf97 commit a3a9153

File tree

3 files changed

+98
-16
lines changed

3 files changed

+98
-16
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
},
1212
"autoload-dev": {
1313
"psr-4": {
14-
"StellarWP\\Slic\\Test\\": "tests/"
14+
"StellarWP\\Slic\\Test\\": "tests/",
15+
"StellarWP\\Slic\\Test\\Support\\": "tests/_support"
1516
}
1617
},
1718
"config": {

tests/Cli/PHPVersionTest.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@
22

33
namespace StellarWP\Slic\Test\Cli;
44

5+
use StellarWP\Slic\Test\Support\Factories\Directory;
6+
57
class PHPVersionTest extends BaseTestCase {
68
public function test_default_php_version_is_7_4(): void {
7-
// Create a temporary directory.
8-
$dir = sys_get_temp_dir() . '/slic-test-' . uniqid( '', true );
9-
10-
// In the temp directory create a plugin.
11-
mkdir( $dir . '/00-default', 0777, true );
12-
file_put_contents(
13-
$dir . '/00-default/plugin.php',
14-
'/**
15-
* Plugin Name: Test Plugin
16-
*/'
17-
);
18-
19-
// Change to the directory.
20-
chdir( $dir );
9+
$pluginsDir = Directory::createTemp()
10+
->createPlugin( 'test-plugin' )
11+
->getAbsolutePath();
12+
chdir( $pluginsDir );
2113

2214
// Set the directory as slic root.
2315
$this->slicExec( 'here' );
@@ -30,7 +22,7 @@ public function test_default_php_version_is_7_4(): void {
3022
);
3123

3224
// Set the target to the plugin.
33-
$this->slicExec( 'use 00-default', true );
25+
$this->slicExec( 'use test-plugin', true );
3426

3527
// Check the plugin PHP version.
3628
$this->assertStringContainsString(
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace StellarWP\Slic\Test\Support\Factories;
4+
5+
use RuntimeException;
6+
7+
class Directory {
8+
private string $absolutePath;
9+
10+
/**
11+
* Directory constructor; it will create the directory as side-effect.
12+
*
13+
* since TBD
14+
*
15+
* @param string $absolutePath The absolute path to the directory to create.
16+
*
17+
* @throws RuntimeException On failure to create the directory.
18+
*/
19+
public function __construct( string $absolutePath ) {
20+
$this->absolutePath = $this->createDirectory( $absolutePath );
21+
}
22+
23+
/**
24+
* Returns the directory absolute path.
25+
*
26+
* @return string The directory absolute path.
27+
*/
28+
public function getAbsolutePath(): string {
29+
return $this->absolutePath;
30+
}
31+
32+
/**
33+
* Factory method to create a new directory in the system temp directory.
34+
*
35+
* @param string|null $path The relative path of the directory to create, or `null` to generate a random path.
36+
*
37+
* @return self
38+
*
39+
* @throws RuntimeException If the directory already exists or cannot be created.
40+
*/
41+
public static function createTemp( ?string $path = null ): self {
42+
$path ??= '/slic-test-plugins-dir-' . uniqid( '', true );
43+
$absolutePath = sys_get_temp_dir() . '/' . ltrim( $path, '/' );
44+
45+
return new self( $absolutePath );
46+
}
47+
48+
/**
49+
* Creates a plugin directory that contains an empty plugin main file.
50+
*
51+
* @param string $pluginDirectoryName The name of the plugin directory to create, e.g. `test-plugin`.
52+
*
53+
* @return $this
54+
*
55+
* @throws RuntimeException On failure to create the plugin directory or the main plugin file.
56+
*/
57+
public function createPlugin( string $pluginDirectoryName ): self {
58+
/** @noinspection UnusedFunctionResultInspection */
59+
$this->createDirectory( $this->absolutePath . '/' . ltrim( $pluginDirectoryName, '/' ) );
60+
61+
$pluginFilePath = $this->absolutePath . "/$pluginDirectoryName/plugin.php";
62+
if ( ! file_put_contents( $pluginFilePath, "/**\n* Plugin Name: Test Plugin\n*/" ) ) {
63+
throw new RuntimeException( "Failed to create $pluginFilePath" );
64+
}
65+
66+
return $this;
67+
}
68+
69+
/**
70+
* Creates a directory.
71+
*
72+
* @param string $absolutePath The absolute path to the directory to create.
73+
*
74+
* @return string The absolute path to the created directory.
75+
*
76+
* @throws RuntimeException On failure to create the directory or if the directory already exists.
77+
*/
78+
private function createDirectory( string $absolutePath ): string {
79+
if ( is_dir( $absolutePath ) ) {
80+
throw new RuntimeException( "Directory $absolutePath already exists." );
81+
}
82+
83+
if ( ! mkdir( $absolutePath, 0777, true ) || ! is_dir( $absolutePath ) ) {
84+
throw new RuntimeException( "Failed to create plugins directory $absolutePath" );
85+
}
86+
87+
return $absolutePath;
88+
}
89+
}

0 commit comments

Comments
 (0)