Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions inc/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Syde\WpPhpUnitIntegration\ShutdownFunctionRegisterer;
use Syde\WpPhpUnitIntegration\SymfonyProcessFactory;
use Syde\WpPhpUnitIntegration\Task\ActivateTestedPlugin;
use Syde\WpPhpUnitIntegration\Task\ActivateTestedPluginDependencies;
use Syde\WpPhpUnitIntegration\Task\ActivateTestedTheme;
use Syde\WpPhpUnitIntegration\Task\Bundle\Cleanup;
use Syde\WpPhpUnitIntegration\Task\Bundle\Load;
Expand Down Expand Up @@ -130,6 +131,7 @@
$contextual = (match ($container->get(PackageTypeDetector::class)->determine()) {
PackageType::Plugin => [
$container->get(SymlinkTestedPlugin::class),
$container->get(ActivateTestedPluginDependencies::class),
$container->get(ActivateTestedPlugin::class),
],
PackageType::Theme => [
Expand All @@ -155,6 +157,10 @@
$container->get(PackageRootPath::class),
$container->get(WpCli::class),
),
ActivateTestedPluginDependencies::class => static fn (ContainerInterface $container): ActivateTestedPluginDependencies => new ActivateTestedPluginDependencies(
$container->get(PackageRootPath::class),
$container->get(WpCli::class),
),
ActivateTestedTheme::class => static fn (ContainerInterface $container): ActivateTestedTheme => new ActivateTestedTheme(
$container->get(PackageRootPath::class),
$container->get(WpCli::class),
Expand Down
52 changes: 52 additions & 0 deletions src/Task/ActivateTestedPluginDependencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Syde\WpPhpUnitIntegration\Task;

use Syde\WpPhpUnitIntegration\Path\LocalDependencyPath;
use Syde\WpPhpUnitIntegration\WpCli;
use Symfony\Component\Filesystem\Path;

readonly class ActivateTestedPluginDependencies implements Task
{
public function __construct(
private LocalDependencyPath $packageRootPath,
private WpCli $wpCli,
) {
}

public function execute(): void
{
$testedPluginName = Path::getFilenameWithoutExtension($this->packageRootPath->path());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note:

This is now done in multiple files https://github.com/search?q=repo%3Ainpsyde%2Fwp-phpunit-integration%20getFilenameWithoutExtension&type=code.

In a future PR, I would like to move this "filename without extension" functionality to some central place, perhaps a service class, rather than doing it in the tasks themselves.


foreach ($this->requiredPlugins($testedPluginName) as $requiredPlugin) {
$this->wpCli->run(['plugin', 'activate', $requiredPlugin]);
}
}

/**
* @return string[]
*/
private function requiredPlugins(string $pluginName): array
{
$requiredPluginsField = json_decode(
$this->wpCli->run([
'plugin',
'get',
$pluginName,
'--field=requires_plugins',
'--format=json',
]),
flags: JSON_THROW_ON_ERROR,
);

if (!is_string($requiredPluginsField) || $requiredPluginsField === '') {
return [];
}

return array_filter(
array_map('trim', explode(',', $requiredPluginsField)),
);
}
}
9 changes: 7 additions & 2 deletions src/WpCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
/**
* @param string[] $args
*/
public function run(array $args): void
public function run(array $args): string
{
$extendedArgs = [
$this->wpCliPath->path(),
Expand All @@ -29,14 +29,19 @@ public function run(array $args): void
'--skip-themes',
'--path=' . $this->wordPressPath->path(),
];
$output = '';

$this->symfonyProcessFactory->create($extendedArgs)->run(
static function (string $type, string $data): void {
static function (string $type, string $data) use (&$output): void {
if ($type === Process::ERR) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new Exception($data);
}

$output .= $data;
}
);

return $output;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

declare(strict_types=1);

namespace Syde\WpPhpUnitIntegration\Tests\Functional\Task;

use PHPUnit\Framework\Attributes\DataProvider;
use Syde\WpPhpUnitIntegration\Task\ActivateTestedPluginDependencies;
use Syde\WpPhpUnitIntegration\Tests\FunctionalTestCase;
use Syde\WpPhpUnitIntegration\WpCli;

final class ActivateTestedPluginDependenciesTest extends FunctionalTestCase
{
public function testPluginActivateNotCalledWhenNoRequiredPlugins(): void
{
$this->filesystem->mkdir($this->workspace . '/acme');

$wpCli = $this->createMock(WpCli::class);
$wpCli
->expects($this->once())
->method('run')
->with(
[
'plugin',
'get',
'acme',
'--field=requires_plugins',
'--format=json',
],
)
->willReturn('""');

(new ActivateTestedPluginDependencies(
$this->localDependencyPath($this->workspace . '/acme'),
$wpCli,
))->execute();
}

/**
* @param list<array{0: list<string>, 1: string}> $wpCliArgsReturnMap
*/
#[DataProvider('wpCliArgsReturnMapProvider')]
public function testRequiredPluginsAreActivated(array $wpCliArgsReturnMap): void
{
$this->filesystem->mkdir($this->workspace . '/acme');

$wpCli = $this->createMock(WpCli::class);
$wpCli
->expects($this->exactly(count($wpCliArgsReturnMap)))
->method('run')
->willReturnMap($wpCliArgsReturnMap);

(new ActivateTestedPluginDependencies(
$this->localDependencyPath($this->workspace . '/acme'),
$wpCli,
))->execute();
}

public static function wpCliArgsReturnMapProvider(): \Generator
{
yield [
[
[
[
'plugin',
'get',
'acme',
'--field=requires_plugins',
'--format=json',
],
'"woocommerce"',
],
[
[
'plugin',
'activate',
'woocommerce',
],
'',
],
],
];

yield [
[
[
[
'plugin',
'get',
'acme',
'--field=requires_plugins',
'--format=json',
],
'"woocommerce,, ,"',
],
[
[
'plugin',
'activate',
'woocommerce',
],
'',
],
],
];

yield [
[
[
[
'plugin',
'get',
'acme',
'--field=requires_plugins',
'--format=json',
],
'"woocommerce, woocommerce-subscriptions"',
],
[
[
'plugin',
'activate',
'woocommerce',
],
'',
],
[
[
'plugin',
'activate',
'woocommerce-subscriptions',
],
'',
],
],
];
}
}
Loading