Skip to content

Commit

Permalink
Merge pull request #693 from iateadonut/patch-1
Browse files Browse the repository at this point in the history
Update PluginProject.php
  • Loading branch information
lucatume authored Feb 12, 2024
2 parents fa02ca5 + 1576f48 commit 4ece923
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased] Unreleased

### Added

- Print file and line of failure during setup of plugin project, (#694, thanks to @iateadonut)

## [4.0.19] 2024-02-09;

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/Project/ContentProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract public function getActivationString(): string;

abstract protected function symlinkProjectInContentDir(string $wpRootDir): void;

abstract protected function activate(string $wpRootDir, int $serverLocalhostPort): bool;
abstract public function activate(string $wpRootDir, int $serverLocalhostPort): bool;

/**
* @return array<string>|false
Expand Down
3 changes: 2 additions & 1 deletion src/Project/PluginProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function getPluginFilePathName(): string
* @throws Throwable
* @throws ProcessException
*/
protected function activate(string $wpRootDir, int $serverLocalhostPort): bool
public function activate(string $wpRootDir, int $serverLocalhostPort): bool
{
$codeExec = new CodeExecutionFactory($wpRootDir, 'localhost:' . $serverLocalhostPort);
$pluginString = basename(dirname($this->pluginFile)) . '/' . basename($this->pluginFile);
Expand All @@ -113,6 +113,7 @@ protected function activate(string $wpRootDir, int $serverLocalhostPort): bool
if ($activationResult instanceof Throwable) {
$message = $activationResult->getMessage();
$this->sayWarning('Could not activate plugin: ' . $message);
$this->say($activationResult->getFile() . ":" . $activationResult->getLine());
$this->say('This might happen because the plugin has unmet dependencies; wp-browser configuration ' .
'will continue, but you will need to manually activate the plugin and update the dump in ' .
'tests/Support/Data/dump.sql.');
Expand Down
4 changes: 2 additions & 2 deletions src/Project/ThemeProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ protected function symlinkProjectInContentDir(string $wpRootDir): void
* @throws Throwable
* @throws ProcessException
*/
protected function activate(string $wpRootDir, int $serverLocalhostPort): bool
public function activate(string $wpRootDir, int $serverLocalhostPort): bool
{
$codeExec = new CodeExecutionFactory($wpRootDir, 'localhost:' . $serverLocalhostPort);
$switchTheme = $codeExec->toSwitchTheme($this->basename, false);
$activationResult = Loop::executeClosure($switchTheme)->getReturnValue();
if ($activationResult instanceof Throwable) {
$message = $activationResult->getMessage();
$this->sayWarning('Could not activate plugin: ' . $message);
$this->sayWarning('Could not activate theme: ' . $message);
$this->say('This might happen because the theme has unmet dependencies; wp-browser configuration ' .
'will continue, but you will need to manually activate the theme and update the dump in ' .
'tests/Support/Data/dump.sql.');
Expand Down
54 changes: 54 additions & 0 deletions tests/unit/lucatume/WPBrowser/Project/PluginProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
use lucatume\WPBrowser\Tests\Traits\CliCommandTestingTools;
use lucatume\WPBrowser\Tests\Traits\TmpFilesCleanup;
use lucatume\WPBrowser\Tests\Traits\UopzFunctions;
use lucatume\WPBrowser\Utils\Env;
use lucatume\WPBrowser\Utils\Filesystem as FS;
use lucatume\WPBrowser\Utils\Random;
use lucatume\WPBrowser\WordPress\Database\MysqlDatabase;
use lucatume\WPBrowser\WordPress\Installation;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\NullOutput;
use tad\Codeception\SnapshotAssertions\SnapshotAssertions;

Expand Down Expand Up @@ -67,7 +72,56 @@ public function should_build_on_plugin_directory_correctly(): void
$output = new NullOutput();

$pluginProject = new PluginProject($input, $output, $pluginDir);

$this->assertEquals('Acme Plugin', $pluginProject->getName());
$this->assertEquals($pluginDir . '/plugin.php', $pluginProject->getPluginFilePathName());
}

/**
* It should provide information about the failure to activate due to error
*
* @test
*/
public function should_provide_information_about_the_failure_to_activate_due_to_error(): void
{
$wpRootDir = FS::tmpDir('plugin_project_');
$dbName = Random::dbName();
$db = new MysqlDatabase(
$dbName,
Env::get('WORDPRESS_DB_USER'),
Env::get('WORDPRESS_DB_PASSWORD'),
Env::get('WORDPRESS_DB_HOST')
);
Installation::scaffold($wpRootDir)
->configure($db)
->install(
'http://localhost:1234',
'admin',
'password',
'admin@example.com',
'Test'
);
FS::mkdirp($wpRootDir . '/wp-content/plugins/acme-plugin', [
'plugin.php' => <<< PHP
<?php
/* Plugin Name: Acme Plugin */
throw new \Exception('Something went wrong.');
PHP

]);
$pluginDir = $wpRootDir . '/wp-content/plugins/acme-plugin';
$input = new ArrayInput([]);
$output = new BufferedOutput();

$pluginProject = new PluginProject($input, $output, $pluginDir);
$this->assertFalse($pluginProject->activate($wpRootDir, 1234));
$expected = "Could not activate plugin: Something went wrong. \n" .
"{{wp_root_dir}}/wp-content/plugins/acme-plugin/plugin.php:3\n" .
"This might happen because the plugin has unmet dependencies; wp-browser configuration will continue, " .
"but you will need to manually activate the plugin and update the dump in tests/Support/Data/dump.sql.";
$this->assertEquals(
$expected,
trim(str_replace($wpRootDir, '{{wp_root_dir}}', $output->fetch()))
);
}
}
57 changes: 56 additions & 1 deletion tests/unit/lucatume/WPBrowser/Project/ThemeProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
use lucatume\WPBrowser\Tests\Traits\CliCommandTestingTools;
use lucatume\WPBrowser\Tests\Traits\TmpFilesCleanup;
use lucatume\WPBrowser\Tests\Traits\UopzFunctions;
use lucatume\WPBrowser\Utils\Env;
use lucatume\WPBrowser\Utils\Filesystem as FS;
use lucatume\WPBrowser\Utils\Random;
use lucatume\WPBrowser\WordPress\Database\MysqlDatabase;
use lucatume\WPBrowser\WordPress\Installation;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\NullOutput;
use tad\Codeception\SnapshotAssertions\SnapshotAssertions;
use lucatume\WPBrowser\Utils\Filesystem as FS;

class ThemeProjectTest extends Unit
{
Expand Down Expand Up @@ -113,4 +118,54 @@ public function should_build_correctly_on_child_theme_directory(): void
$this->assertEquals('Some Theme', $themeProject->getName());
$this->assertEquals('theme', $themeProject->getType());
}

/**
* It should provide information about the failure to activate due to error
*
* @test
*/
public function should_provide_information_about_the_failure_to_activate_due_to_error(): void
{
$wpRootDir = FS::tmpDir('theme_project_');
$dbName = Random::dbName();
$db = new MysqlDatabase(
$dbName,
Env::get('WORDPRESS_DB_USER'),
Env::get('WORDPRESS_DB_PASSWORD'),
Env::get('WORDPRESS_DB_HOST')
);
Installation::scaffold($wpRootDir)
->configure($db)
->install(
'http://localhost:1234',
'admin',
'password',
'admin@example.com',
'Test'
);
FS::mkdirp($wpRootDir . '/wp-content/themes/acme-theme', [
'style.css' => <<< PHP
<?php
/*
Theme Name: Acme Theme
Requires PHP: 23.89
*/
PHP,
'index.php' => '<?php',
'functions.php' => '<?php'
]);
$themeDir = $wpRootDir . '/wp-content/themes/acme-theme';
$input = new ArrayInput([]);
$output = new BufferedOutput();

$themeProject = new ThemeProject($input, $output, $themeDir);
$this->assertFalse($themeProject->activate($wpRootDir, 1234));
$expected = "Could not activate theme: Error: Current PHP version does not meet minimum requirements for Acme Theme. \n" .
"This might happen because the theme has unmet dependencies; wp-browser configuration will continue, " .
"but you will need to manually activate the theme and update the dump in tests/Support/Data/dump.sql.";
$this->assertEquals(
$expected,
trim(str_replace($wpRootDir, '{{wp_root_dir}}', $output->fetch()))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public function should_support_complex_plugin_load_in_sqlite_context(): void
{
$wpRoot = FS::tmpDir('installation_');
$db = new SQLiteDatabase($wpRoot, 'db.sqlite');
$installation = Installation::scaffold($wpRoot, '6.1.1')
$installation = Installation::scaffold($wpRoot)
->configure($db)
->install(
'https://localhost:2389',
Expand Down

0 comments on commit 4ece923

Please sign in to comment.