Skip to content

Commit

Permalink
test(SymlinkerTest) restore removed code
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Jun 25, 2024
1 parent ec51c85 commit 5e28aca
Showing 1 changed file with 52 additions and 28 deletions.
80 changes: 52 additions & 28 deletions tests/unit/lucatume/WPBrowser/Extension/SymlinkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@

namespace unit\lucatume\WPBrowser\Extension;

use Codeception\Codecept;
use Codeception\Event\SuiteEvent;
use Codeception\Exception\ModuleConfigException;
use Codeception\Exception\ModuleException;
use Codeception\Suite;
use Codeception\Test\Unit;
use lucatume\WPBrowser\Extension\Symlinker;
use lucatume\WPBrowser\Tests\Traits\LoopIsolation;
use lucatume\WPBrowser\Utils\Filesystem as FS;
use lucatume\WPBrowser\WordPress\Installation;
use PHPUnit\Framework\Assert;
use Symfony\Component\EventDispatcher\EventDispatcher;

class SymlinkerTest extends Unit
{
use LoopIsolation;

private function getSuiteEvent(): SuiteEvent
{
if (Codecept::VERSION >= 5) {
return new SuiteEvent(new Suite(new EventDispatcher(), 'test'));
}

// Codeception 4.x.
return new SuiteEvent(new Suite());
}

public function test_exists(): void
{
$symlinker = new Symlinker([
Expand All @@ -29,67 +42,72 @@ public function test_throw_if_wp_root_folder_is_not_set(): void
{
$this->expectException(ModuleConfigException::class);
$this->expectExceptionMessage('The `wpRootFolder` configuration parameter must be set.');
$suiteEvent = $this->getSuiteEvent();

$symlinker = new Symlinker([
], []);
$symlinker->onModuleInit(new SuiteEvent());
$symlinker->onModuleInit($suiteEvent);
}

public function test_throw_if_wp_root_folder_does_not_point_to_a_valid_installation(): void
{
$symlinker = new Symlinker([
'wpRootFolder' => __DIR__,
], []);
$suiteEvent = $this->getSuiteEvent();

$this->expectException(ModuleConfigException::class);
$this->expectExceptionMessage('The `wpRootFolder` does not point to a valid WordPress installation.');

$this->assertInIsolation(static function () use ($symlinker) {
$symlinker->onModuleInit(new SuiteEvent());
$this->assertInIsolation(static function () use ($symlinker, $suiteEvent) {
$symlinker->onModuleInit($suiteEvent);
});
}

public function test_throw_if_plugins_are_not_array(): void
{
$this->expectException(ModuleConfigException::class);
$this->expectExceptionMessage('The `plugins` configuration parameter must be an array.');
$suiteEvent = $this->getSuiteEvent();

$symlinker = new Symlinker([
'wpRootFolder' => __DIR__,
'plugins' => 'not-an-array',
], []);
$symlinker->onModuleInit(new SuiteEvent());
$symlinker->onModuleInit($suiteEvent);
}

public function test_throw_if_themes_are_not_array(): void
{
$this->expectException(ModuleConfigException::class);
$this->expectExceptionMessage('The `themes` configuration parameter must be an array.');
$suiteEvent = $this->getSuiteEvent();

$symlinker = new Symlinker([
'wpRootFolder' => __DIR__,
'themes' => 'not-an-array',
], []);
$symlinker->onModuleInit(new SuiteEvent());
$symlinker->onModuleInit($suiteEvent);
}

public function test_without_plugins_or_themes(): void
{
$workingDir = FS::tmpDir('symlinker_');
$wpRoot = FS::tmpDir('symlinker_');
Installation::scaffold($wpRoot);
$suiteEvent = $this->getSuiteEvent();

$symlinker = new Symlinker([
'wpRootFolder' => $wpRoot,
], []);

$this->assertInIsolation(static function () use ($symlinker, $workingDir) {
$this->assertInIsolation(static function () use ($symlinker, $workingDir, $suiteEvent) {
chdir($workingDir);

Assert::assertSame($workingDir, getcwd());

$symlinker->onModuleInit(new SuiteEvent());
$symlinker->afterSuite(new SuiteEvent());
$symlinker->onModuleInit($suiteEvent);
$symlinker->afterSuite($suiteEvent);
});
}

Expand All @@ -105,7 +123,6 @@ public function test_throws_if_plugin_file_does_not_exist(): void
$this->expectException(ModuleConfigException::class);
$this->expectExceptionMessage('Plugin file not-a-file/plugin.php does not exist.');


$symlinker = new Symlinker([
'wpRootFolder' => $wpRoot,
'plugins' => [
Expand Down Expand Up @@ -200,8 +217,9 @@ function theme_2_some_function() {
]);
$wpRoot = FS::tmpDir('symlinker_');
Installation::scaffold($wpRoot);
$suiteEvent = $this->getSuiteEvent();

$this->assertInIsolation(static function () use ($workingDir, $wpRoot) {
$this->assertInIsolation(static function () use ($workingDir, $wpRoot, $suiteEvent) {
chdir($workingDir);

Assert::assertSame($workingDir, getcwd());
Expand All @@ -228,7 +246,7 @@ function theme_2_some_function() {
Assert::assertFileDoesNotExist($wpRoot . '/wp-content/themes/theme-2/index.php');
Assert::assertFileDoesNotExist($wpRoot . '/wp-content/themes/theme-2/functions.php');

$symlinker->onModuleInit(new SuiteEvent());
$symlinker->onModuleInit($suiteEvent);

Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-1/plugin-1.php');
Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-2/main.php');
Expand All @@ -239,7 +257,7 @@ function theme_2_some_function() {
Assert::assertFileExists($wpRoot . '/wp-content/themes/theme-2/index.php');
Assert::assertFileExists($wpRoot . '/wp-content/themes/theme-2/functions.php');

$symlinker->afterSuite(new SuiteEvent());
$symlinker->afterSuite($suiteEvent);

Assert::assertFileDoesNotExist($wpRoot . '/wp-content/plugins/plugin-1/plugin-1.php');
Assert::assertFileDoesNotExist($wpRoot . '/wp-content/plugins/plugin-2/main.php');
Expand Down Expand Up @@ -318,8 +336,9 @@ function theme_2_some_function() {
]);
$wpRoot = FS::tmpDir('symlinker_');
Installation::scaffold($wpRoot);
$suiteEvent = $this->getSuiteEvent();

$this->assertInIsolation(static function () use ($workingDir, $wpRoot) {
$this->assertInIsolation(static function () use ($workingDir, $wpRoot, $suiteEvent) {
chdir($workingDir);

Assert::assertSame($workingDir, getcwd());
Expand All @@ -346,7 +365,7 @@ function theme_2_some_function() {
Assert::assertFileDoesNotExist($wpRoot . '/wp-content/themes/theme-2/index.php');
Assert::assertFileDoesNotExist($wpRoot . '/wp-content/themes/theme-2/functions.php');

$symlinker->onModuleInit(new SuiteEvent());
$symlinker->onModuleInit($suiteEvent);

Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-1/plugin-1.php');
Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-2/main.php');
Expand All @@ -357,7 +376,7 @@ function theme_2_some_function() {
Assert::assertFileExists($wpRoot . '/wp-content/themes/theme-2/index.php');
Assert::assertFileExists($wpRoot . '/wp-content/themes/theme-2/functions.php');

$symlinker->afterSuite(new SuiteEvent());
$symlinker->afterSuite($suiteEvent);

Assert::assertFileDoesNotExist($wpRoot . '/wp-content/plugins/plugin-1/plugin-1.php');
Assert::assertFileDoesNotExist($wpRoot . '/wp-content/plugins/plugin-2/main.php');
Expand Down Expand Up @@ -436,8 +455,9 @@ function theme_2_some_function() {
]);
$wpRoot = FS::tmpDir('symlinker_');
Installation::scaffold($wpRoot);
$suiteEvent = $this->getSuiteEvent();

$this->assertInIsolation(static function () use ($workingDir, $wpRoot) {
$this->assertInIsolation(static function () use ($workingDir, $wpRoot, $suiteEvent) {
chdir($workingDir);

Assert::assertSame($workingDir, getcwd());
Expand All @@ -463,7 +483,7 @@ function theme_2_some_function() {
Assert::assertFileDoesNotExist($wpRoot . '/wp-content/themes/theme-2/index.php');
Assert::assertFileDoesNotExist($wpRoot . '/wp-content/themes/theme-2/functions.php');

$symlinker->onModuleInit(new SuiteEvent());
$symlinker->onModuleInit($suiteEvent);

Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-1/plugin-1.php');
Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-2/main.php');
Expand All @@ -474,7 +494,7 @@ function theme_2_some_function() {
Assert::assertFileExists($wpRoot . '/wp-content/themes/theme-2/index.php');
Assert::assertFileExists($wpRoot . '/wp-content/themes/theme-2/functions.php');

$symlinker->afterSuite(new SuiteEvent());
$symlinker->afterSuite($suiteEvent);

Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-1/plugin-1.php');
Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-2/main.php');
Expand Down Expand Up @@ -553,8 +573,9 @@ function theme_2_some_function() {
]);
$wpRoot = FS::tmpDir('symlinker_');
Installation::scaffold($wpRoot);
$suiteEvent = $this->getSuiteEvent();

$this->assertInIsolation(static function () use ($workingDir, $wpRoot) {
$this->assertInIsolation(static function () use ($workingDir, $wpRoot, $suiteEvent) {
chdir($workingDir);

Assert::assertSame($workingDir, getcwd());
Expand All @@ -581,7 +602,7 @@ function theme_2_some_function() {
Assert::assertFileDoesNotExist($wpRoot . '/wp-content/themes/theme-2/index.php');
Assert::assertFileDoesNotExist($wpRoot . '/wp-content/themes/theme-2/functions.php');

$symlinker->onModuleInit(new SuiteEvent());
$symlinker->onModuleInit($suiteEvent);

Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-1/plugin-1.php');
Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-2/main.php');
Expand All @@ -592,7 +613,7 @@ function theme_2_some_function() {
Assert::assertFileExists($wpRoot . '/wp-content/themes/theme-2/index.php');
Assert::assertFileExists($wpRoot . '/wp-content/themes/theme-2/functions.php');

$symlinker->afterSuite(new SuiteEvent());
$symlinker->afterSuite($suiteEvent);

Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-1/plugin-1.php');
Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-2/main.php');
Expand Down Expand Up @@ -671,8 +692,9 @@ function theme_2_some_function() {
]);
$wpRoot = FS::tmpDir('symlinker_');
Installation::scaffold($wpRoot);
$suiteEvent = $this->getSuiteEvent();

$this->assertInIsolation(static function () use ($workingDir, $wpRoot) {
$this->assertInIsolation(static function () use ($workingDir, $wpRoot, $suiteEvent) {
chdir($workingDir);

Assert::assertSame($workingDir, getcwd());
Expand Down Expand Up @@ -708,7 +730,7 @@ function theme_2_some_function() {
Assert::assertFileExists($wpRoot . '/wp-content/themes/theme-2/index.php');
Assert::assertFileExists($wpRoot . '/wp-content/themes/theme-2/functions.php');

$symlinker->onModuleInit(new SuiteEvent());
$symlinker->onModuleInit($suiteEvent);

Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-1/plugin-1.php');
Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-2/main.php');
Expand All @@ -719,7 +741,7 @@ function theme_2_some_function() {
Assert::assertFileExists($wpRoot . '/wp-content/themes/theme-2/index.php');
Assert::assertFileExists($wpRoot . '/wp-content/themes/theme-2/functions.php');

$symlinker->afterSuite(new SuiteEvent());
$symlinker->afterSuite($suiteEvent);

Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-1/plugin-1.php');
Assert::assertFileExists($wpRoot . '/wp-content/plugins/plugin-2/main.php');
Expand Down Expand Up @@ -767,13 +789,14 @@ function activate_plugin_2(){
$wpRoot = FS::tmpDir('symlinker_');
$otherDir = FS::tmpDir('symlinker_');
Installation::scaffold($wpRoot);
$suiteEvent = $this->getSuiteEvent();

$this->expectException(ModuleException::class);
$this->expectExceptionMessage(
"Could not symlink plugin $workingDir/vendor/acme/plugin-2 to $wpRoot/wp-content/plugins/plugin-2: link already exists and target is $otherDir."
);

$this->assertInIsolation(static function () use ($workingDir, $wpRoot, $otherDir) {
$this->assertInIsolation(static function () use ($workingDir, $wpRoot, $otherDir, $suiteEvent) {
chdir($workingDir);

Assert::assertSame($workingDir, getcwd());
Expand All @@ -794,7 +817,7 @@ function activate_plugin_2(){
throw new \RuntimeException('Could not create symlinks in ' . $wpRoot);
}

$symlinker->onModuleInit(new SuiteEvent());
$symlinker->onModuleInit($suiteEvent);
});
}

Expand All @@ -809,8 +832,9 @@ function plugin_1_canary() {}
]);
$wpRoot = FS::tmpDir('symlinker_');
Installation::scaffold($wpRoot);
$suiteEvent = $this->getSuiteEvent();

$this->assertInIsolation(static function () use ($workingDir, $wpRoot) {
$this->assertInIsolation(static function () use ($workingDir, $wpRoot, $suiteEvent) {
chdir($workingDir);

Assert::assertSame($workingDir, getcwd());
Expand All @@ -826,7 +850,7 @@ function plugin_1_canary() {}

Assert::assertFileDoesNotExist($wpRoot . "/wp-content/plugins/{$workDirBasename}/plugin.php");

$symlinker->onModuleInit(new SuiteEvent());
$symlinker->onModuleInit($suiteEvent);

Assert::assertFileExists($wpRoot . "/wp-content/plugins/{$workDirBasename}/plugin.php");
Assert::assertTrue(is_link($wpRoot . "/wp-content/plugins/{$workDirBasename}"));
Expand Down

0 comments on commit 5e28aca

Please sign in to comment.