-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
270 additions
and
10 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
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,4 @@ | ||
#!/usr/bin/env bash | ||
|
||
echo "This binary was never modded to be executable" | ||
exit 0 |
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,4 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
echo "Hello from wp-cli custom binary"; | ||
exit(0); |
129 changes: 129 additions & 0 deletions
129
tests/unit/lucatume/WPBrowser/Module/WPCLICustomBinaryTest.php
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,129 @@ | ||
<?php | ||
|
||
namespace unit\lucatume\WPBrowser\Module; | ||
|
||
use Codeception\Exception\ModuleConfigException; | ||
use Codeception\Lib\Di; | ||
use Codeception\Lib\ModuleContainer; | ||
use Codeception\Test\Unit; | ||
use lucatume\WPBrowser\Module\WPCLI; | ||
use lucatume\WPBrowser\Utils\Filesystem; | ||
|
||
class WPCLICustomBinaryTest extends Unit | ||
{ | ||
/** | ||
* @var string|null | ||
*/ | ||
private $homeBackup; | ||
/** | ||
* @var string|null|false | ||
*/ | ||
private $envCacheDirValue = null; | ||
/** | ||
* @var string | ||
*/ | ||
private $wpCliCacheDir; | ||
/** | ||
* @var string | ||
*/ | ||
private $wpCliCacheDirBackup; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
if (isset($_SERVER['HOME'])) { | ||
$this->homeBackup = $_SERVER['HOME']; | ||
} | ||
$this->wpCliCacheDir = Filesystem::cacheDir() . '/wp-cli'; | ||
$this->wpCliCacheDirBackup = dirname($this->wpCliCacheDir) . '/wp-cli-cache-dir-backup'; | ||
if (is_dir($this->wpCliCacheDirBackup)) { | ||
Filesystem::rrmdir($this->wpCliCacheDirBackup); | ||
} | ||
if (is_dir($this->wpCliCacheDir)) { | ||
rename($this->wpCliCacheDir, $this->wpCliCacheDirBackup); | ||
} | ||
$this->assertDirectoryDoesNotExist($this->wpCliCacheDir); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
if ($this->homeBackup !== null) { | ||
$_SERVER['HOME'] = $this->homeBackup; | ||
} | ||
if (is_dir($this->wpCliCacheDir)) { | ||
Filesystem::rrmdir($this->wpCliCacheDir); | ||
} | ||
if (is_dir($this->wpCliCacheDirBackup)) { | ||
rename($this->wpCliCacheDirBackup, $this->wpCliCacheDir); | ||
} | ||
} | ||
|
||
public function test_configuration_allows_custom_binary(): void | ||
{ | ||
$binary = codecept_data_dir('bins/wp-cli-custom-bin'); | ||
$moduleContainer = new ModuleContainer(new Di(), []); | ||
|
||
$module = new WPCLI($moduleContainer, [ | ||
'path' => 'var/wordpress', | ||
'bin' => $binary, | ||
]); | ||
$module->cli(['core', 'version']); | ||
|
||
$this->assertEquals( | ||
'Hello from wp-cli custom binary', | ||
$module->grabLastShellOutput() | ||
); | ||
$this->assertDirectoryDoesNotExist($this->wpCliCacheDir); | ||
} | ||
|
||
public function test_configuration_supports_tilde_for_home_in_custom_binary(): void | ||
{ | ||
$_SERVER['HOME'] = codecept_data_dir(); | ||
$binary = '~/bins/wp-cli-custom-bin'; | ||
$binaryPath = codecept_data_dir('bins/wp-cli-custom-bin'); | ||
$moduleContainer = new ModuleContainer(new Di(), []); | ||
// Sanity check. | ||
$this->assertEquals(rtrim(codecept_data_dir(), '\\/'), Filesystem::homeDir()); | ||
|
||
$module = new WPCLI($moduleContainer, [ | ||
'path' => 'var/wordpress', | ||
'bin' => $binary, | ||
]); | ||
$module->cli(['core', 'version']); | ||
|
||
$this->assertEquals( | ||
'Hello from wp-cli custom binary', | ||
$module->grabLastShellOutput() | ||
); | ||
$this->assertDirectoryDoesNotExist($this->wpCliCacheDir); | ||
} | ||
|
||
public function test_throws_if_custom_binary_does_not_exist(): void | ||
{ | ||
$binary = codecept_data_dir('bins/not-a-bin'); | ||
$moduleContainer = new ModuleContainer(new Di(), []); | ||
|
||
$this->expectException(ModuleConfigException::class); | ||
|
||
$module = new WPCLI($moduleContainer, [ | ||
'path' => 'var/wordpress', | ||
'bin' => $binary, | ||
]); | ||
$module->cli(['core', 'version']); | ||
} | ||
|
||
public function test_throws_if_custom_binary_is_not_executable(): void | ||
{ | ||
$binary = codecept_data_dir('bins/not-executable'); | ||
$moduleContainer = new ModuleContainer(new Di(), []); | ||
|
||
$this->expectException(ModuleConfigException::class); | ||
|
||
$module = new WPCLI($moduleContainer, [ | ||
'path' => 'var/wordpress', | ||
'bin' => $binary, | ||
]); | ||
$module->cli(['core', 'version']); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
tests/unit/lucatume/WPBrowser/WordPress/CliProcessTest.php
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,78 @@ | ||
<?php | ||
|
||
|
||
namespace Unit\lucatume\WPBrowser\WordPress; | ||
|
||
use lucatume\WPBrowser\Exceptions\InvalidArgumentException; | ||
use lucatume\WPBrowser\Utils\Filesystem; | ||
use lucatume\WPBrowser\WordPress\CliProcess; | ||
|
||
class CliProcessTest extends \Codeception\Test\Unit | ||
{ | ||
/** | ||
* @var string|null | ||
*/ | ||
private $homeBackup; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
if (isset($_SERVER['HOME'])) { | ||
$this->homeBackup = $_SERVER['HOME']; | ||
} | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
if ($this->homeBackup !== null) { | ||
$_SERVER['HOME'] = $this->homeBackup; | ||
} | ||
} | ||
|
||
public function test_construct_with_custom_binary(): void | ||
{ | ||
$binary = codecept_data_dir('bins/wp-cli-custom-bin'); | ||
|
||
$cliProcess = new CliProcess(['core', 'version'], null, null, null, null, $binary); | ||
|
||
$this->assertEquals( | ||
escapeshellarg(PHP_BINARY) . ' ' . escapeshellarg($binary) . " 'core' 'version'", | ||
$cliProcess->getCommandLine() | ||
); | ||
} | ||
|
||
public function test_throws_if_custom_binary_does_not_exist(): void | ||
{ | ||
$binary = codecept_data_dir('bins/not-a-bin'); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
|
||
$cliProcess = new CliProcess(['core', 'version'], null, null, null, null, $binary); | ||
} | ||
|
||
public function test_throws_if_custom_binary_is_not_executable(): void | ||
{ | ||
$binary = codecept_data_dir('bins/not-executable'); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
|
||
$cliProcess = new CliProcess(['core', 'version'], null, null, null, null, $binary); | ||
} | ||
|
||
public function test_tilde_for_home_dir_is_supported_in_custom_binary_path(): void | ||
{ | ||
$_SERVER['HOME'] = codecept_data_dir(); | ||
$binary = '~/bins/wp-cli-custom-bin'; | ||
$binaryAbsolutePath = codecept_data_dir('bins/wp-cli-custom-bin'); | ||
// Sanity check. | ||
$this->assertEquals(rtrim(codecept_data_dir(), '\\/'), Filesystem::homeDir()); | ||
|
||
$cliProcess = new CliProcess(['core', 'version'], null, null, null, null, $binary); | ||
|
||
$this->assertEquals( | ||
escapeshellarg(PHP_BINARY) . ' ' . escapeshellarg($binaryAbsolutePath) . " 'core' 'version'", | ||
$cliProcess->getCommandLine() | ||
); | ||
} | ||
} |