first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
{
"name": "wpdesk\/wp-codeception",
"description": "Library for WP Desk Codeception tests.",
"license": "MIT",
"keywords": [
"wordpress",
"codeception",
"admin"
],
"homepage": "https:\/\/gitlab.com\/wpdesk\/wp-codeception",
"type": "composer-plugin",
"minimum-stability": "stable",
"authors": [
{
"name": "grola",
"email": "grola@wpdesk.net"
}
],
"require": {
"php": ">=5.6",
"ext-json": "*",
"wpdesk\/wp-builder": "^1.0",
"lucatume\/wp-browser": "^2.2",
"composer-plugin-api": "^1.1"
},
"require-dev": {
"phpunit\/phpunit": "<7",
"wp-coding-standards\/wpcs": "^0.14.1",
"squizlabs\/php_codesniffer": "^3.0.2",
"mockery\/mockery": "*",
"10up\/wp_mock": "*",
"wimg\/php-compatibility": "^8"
},
"autoload": {
"psr-4": {
"FSVendor\\WPDesk\\Codeception\\": "src\/WPDesk\/Codeception",
"FSVendor\\Codeception\\Module\\": "src\/Codeception\/Module",
"FSVendor\\WPDesk\\Composer\\Codeception\\": "src\/WPDesk\/Composer"
}
},
"autoload-dev": {},
"scripts": {
"phpcs": "phpcs"
},
"extra": {
"class": "WPDesk\\Composer\\Codeception\\Plugin"
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace FSVendor\WPDesk\Codeception\Command;
use FSVendor\Codeception\Lib\Generator\Test;
/**
* Class code for codeception example test for WP Desk plugin activation.
*
* @package WPDesk\Codeception\Command
*/
class AcceptanceTestGenerator extends \FSVendor\Codeception\Lib\Generator\Test
{
protected $template = <<<EOF
<?php {{namespace}}
use WPDesk\\Codeception\\Tests\\Acceptance\\ActivationCest as ActivationCestAlias;
class {{name}} extends ActivationCestAlias {
\t/**
\t * Deactivate plugins before tests.
\t *
\t * @param AcceptanceTester \$i .
\t *
\t * @throws \\Codeception\\Exception\\ModuleException .
\t */
\tpublic function _before( \$i ) {
\t\t\$i->loginAsAdmin();
\t\t\$i->amOnPluginsPage();
\t\t\$i->deactivatePlugin( \$this->getPluginSlug() );
\t\t\$i->amOnPluginsPage();
\t\t\$i->seePluginDeactivated( \$this->getPluginSlug() );
\t\t\$i->amOnPluginsPage();
\t\t\$i->deactivatePlugin( self::WOOCOMMERCE_PLUGIN_SLUG );
\t\t\$i->amOnPluginsPage();
\t\t\$i->seePluginDeactivated( self::WOOCOMMERCE_PLUGIN_SLUG );
\t}
\t/**
\t * Plugin activation.
\t *
\t * @param AcceptanceTester \$i .
\t *
\t * @throws \\Codeception\\Exception\\ModuleException .
\t */
\tpublic function pluginActivation( \$i ) {
\t\t\$i->loginAsAdmin();
\t\t\$i->amOnPluginsPage();
\t\t\$i->seePluginDeactivated( \$this->getPluginSlug() );
\t\t// This is an example and you should change it to current plugin.
\t\t\$i->activateWPDeskPlugin(
\t\t\t\$this->getPluginSlug(),
\t\t\tarray( 'woocommerce' ),
\t\t\tarray( 'The “WooCommerce Fakturownia” plugin cannot run without WooCommerce active. Please install and activate WooCommerce plugin.' )
\t\t);
\t}
}
EOF;
}

View File

@@ -0,0 +1,70 @@
<?php
namespace FSVendor\WPDesk\Codeception\Command;
use FSVendor\Codeception\Command\GenerateTest;
use FSVendor\Codeception\CustomCommandInterface;
use FSVendor\Symfony\Component\Console\Input\InputInterface;
use FSVendor\Symfony\Component\Console\Output\OutputInterface;
/**
* Generates codeception example test for WP Desk plugin activation.
*
* @package WPDesk\Codeception\Command
*/
class GeneratePluginActivation extends \FSVendor\Codeception\Command\GenerateTest implements \FSVendor\Codeception\CustomCommandInterface
{
/**
* Get codeception command description.
*
* @return string
*/
public function getDescription()
{
return 'Generates plugin activation tests.';
}
/**
* Returns the name of the command.
*
* @return string
*/
public static function getCommandName()
{
return 'generate:activation';
}
/**
* Get generator class.
*
* @param array $config .
* @param string $class .
* @return AcceptanceTestGenerator
*/
protected function getGenerator($config, $class)
{
return new \FSVendor\WPDesk\Codeception\Command\AcceptanceTestGenerator($config, $class);
}
/**
* Execute command.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
public function execute(\FSVendor\Symfony\Component\Console\Input\InputInterface $input, \FSVendor\Symfony\Component\Console\Output\OutputInterface $output)
{
$suite = $input->getArgument('suite');
$class = $input->getArgument('class');
$config = $this->getSuiteConfig($suite);
$className = $this->getShortClassName($class);
$path = $this->createDirectoryFor($config['path'], $class);
$filename = $this->completeSuffix($className, 'Cest');
$filename = $path . $filename;
$gen = $this->getGenerator($config, $class);
$res = $this->createFile($filename, $gen->produce());
if (!$res) {
$output->writeln("<error>Test {$filename} already exists</error>");
return;
}
$output->writeln("<info>Test was created in {$filename}</info>");
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace FSVendor\WPDesk\Codeception\Command;
use FSVendor\Codeception\Command\GenerateTest;
use FSVendor\Codeception\CustomCommandInterface;
use FSVendor\Symfony\Component\Console\Input\InputInterface;
use FSVendor\Symfony\Component\Console\Output\OutputInterface;
/**
* Generates codeception example test for WP Desk plugin activation.
*
* @package WPDesk\Codeception\Command
*/
class GenerateWooCommerce extends \FSVendor\Codeception\Command\GenerateTest implements \FSVendor\Codeception\CustomCommandInterface
{
/**
* Get codeception command description.
*
* @return string
*/
public function getDescription()
{
return 'Generates woocommerce tests.';
}
/**
* Returns the name of the command.
*
* @return string
*/
public static function getCommandName()
{
return 'generate:woocommerce';
}
/**
* Get generator class.
*
* @param array $config .
* @param string $class .
* @return WooCommerceTestGenerator
*/
protected function getGenerator($config, $class)
{
return new \FSVendor\WPDesk\Codeception\Command\WooCommerceTestGenerator($config, $class);
}
/**
* Execute command.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
public function execute(\FSVendor\Symfony\Component\Console\Input\InputInterface $input, \FSVendor\Symfony\Component\Console\Output\OutputInterface $output)
{
$suite = $input->getArgument('suite');
$class = $input->getArgument('class');
$config = $this->getSuiteConfig($suite);
$className = $this->getShortClassName($class);
$path = $this->createDirectoryFor($config['path'], $class);
$filename = $this->completeSuffix($className, 'Cest');
$filename = $path . $filename;
$gen = $this->getGenerator($config, $class);
$res = $this->createFile($filename, $gen->produce());
if (!$res) {
$output->writeln("<error>Test {$filename} already exists</error>");
return;
}
$output->writeln("<info>Test was created in {$filename}</info>");
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace FSVendor\WPDesk\Codeception\Command;
use FSVendor\Codeception\Lib\Generator\Test;
/**
* Class code for codeception example test for WP Desk plugin activation.
*
* @package WPDesk\Codeception\Command
*/
class WooCommerceTestGenerator extends \FSVendor\Codeception\Lib\Generator\Test
{
protected $template = <<<EOF
<?php {{namespace}}
use WPDesk\\Codeception\\Tests\\Acceptance\\WooCommerceCest as WooCommerceCestAlias;
/**
* Common WooCommerce tests.
*/
class {{name}} extends WooCommerceCestAlias {
}
EOF;
}

View File

@@ -0,0 +1,16 @@
<?php
namespace FSVendor\WPDesk\Composer\Codeception;
use FSVendor\WPDesk\Composer\Codeception\Commands\CreateCodeceptionTests;
use FSVendor\WPDesk\Composer\Codeception\Commands\RunCodeceptionTests;
/**
* Links plugin commands handlers to composer.
*/
class CommandProvider implements \FSVendor\Composer\Plugin\Capability\CommandProvider
{
public function getCommands()
{
return [new \FSVendor\WPDesk\Composer\Codeception\Commands\CreateCodeceptionTests(), new \FSVendor\WPDesk\Composer\Codeception\Commands\RunCodeceptionTests()];
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace FSVendor\WPDesk\Composer\Codeception\Commands;
use FSVendor\Composer\Command\BaseCommand as CodeceptionBaseCommand;
use FSVendor\Symfony\Component\Console\Output\OutputInterface;
/**
* Base for commands - declares common methods.
*
* @package WPDesk\Composer\Codeception\Commands
*/
abstract class BaseCommand extends \FSVendor\Composer\Command\BaseCommand
{
/**
* @param string $command
* @param OutputInterface $output
*/
protected function execAndOutput($command, \FSVendor\Symfony\Component\Console\Output\OutputInterface $output)
{
\passthru($command);
}
}

View File

@@ -0,0 +1,116 @@
<?php
namespace FSVendor\WPDesk\Composer\Codeception\Commands;
use FSVendor\Composer\Downloader\FilesystemException;
use FSVendor\Symfony\Component\Console\Input\InputInterface;
use FSVendor\Symfony\Component\Console\Output\OutputInterface;
/**
* Codeception tests creator command.
*
* @package WPDesk\Composer\Codeception\Commands
*/
class CreateCodeceptionTests extends \FSVendor\WPDesk\Composer\Codeception\Commands\BaseCommand
{
use SedTrait;
/**
* Configure command.
*/
protected function configure()
{
parent::configure();
$this->setName('create-codeception-tests')->setDescription('Create codeception tests directories and files.');
}
/**
* Copy file.
*
* @param string $source
* @param string $dest
* @param string $exceptionMessage
* @throws FilesystemException
*/
private function copy($source, $dest, $exceptionMessage)
{
if (!\copy($source, $dest)) {
throw new \FSVendor\Composer\Downloader\FilesystemException($exceptionMessage);
}
}
/**
* Copy configuration files.
*
* @param $codeceptionDir
* @param $testsDir
* @param $codeceptionYml
* @param $envConfig
* @param $acceptanceYml
* @param $bootstrapScript
* @return void
* @throws FilesystemException
*/
private function copyConfigurationFiles($codeceptionDir, $testsDir, $codeceptionYml, $envConfig, $acceptanceYml, $bootstrapScript)
{
if (!\file_exists('./' . $codeceptionYml)) {
$this->copy('./vendor/wpdesk/wp-codeception/configuration/' . $codeceptionYml, './' . $codeceptionYml, 'Error copying codeception configuration file!');
}
if (!\file_exists('./' . $envConfig)) {
$this->copy('./vendor/wpdesk/wp-codeception/configuration/' . $envConfig, './' . $envConfig, 'Error copying codeception env configuration file!');
}
if (\file_exists($testsDir . '/' . $acceptanceYml)) {
\unlink($testsDir . '/' . $acceptanceYml);
}
$this->copy('./vendor/wpdesk/wp-codeception/configuration/' . $acceptanceYml, $testsDir . '/' . $acceptanceYml, 'Error copying codeception acceptance configuration file!');
if (!\file_exists($codeceptionDir . '/' . $bootstrapScript)) {
$this->copy('./vendor/wpdesk/wp-codeception/scripts/' . $bootstrapScript, $codeceptionDir . '/' . $bootstrapScript, 'Error copying codeception bootstrap script file!');
}
if (!@\file_exists($testsDir . '/_output')) {
\mkdir($testsDir . '/_output', 0777, \true);
}
if (!\file_exists($testsDir . '/_output/.gitignore')) {
$this->copy('./vendor/wpdesk/wp-codeception/configuration/_output.gitignore', $testsDir . '/_output/.gitignore', 'Error copying codeception acceptance output .gitignore file!');
}
if (!@\file_exists($testsDir . '/_support/_generated')) {
\mkdir($testsDir . '/_support/_generated', 0777, \true);
}
if (!\file_exists($testsDir . '/_support/_generated/.gitignore')) {
$this->copy('./vendor/wpdesk/wp-codeception/configuration/_generated.gitignore', $testsDir . '/_support/_generated/.gitignore', 'Error copying codeception acceptance output .gitignore file!');
}
}
/**
* Inject traits into tester class.
*
* @param string $testsDir
* @return void
*/
private function injectTraitsIntoTesterClass($testsDir)
{
$file_pattern = $testsDir . '/_support/AcceptanceTester.php';
$pattern = "/use _generated\\\\AcceptanceTesterActions;/";
$replace = "use _generated\\AcceptanceTesterActions;\n" . "\n\tuse \\WPDesk\\Codeception\\Tests\\Acceptance\\Tester\\TesterWordpressActions;" . "\n\tuse \\WPDesk\\Codeception\\Tests\\Acceptance\\Tester\\WooCommerce\\TesterWooCommerceAdminSettingsGeneralTrait;" . "\n\tuse \\WPDesk\\Codeception\\Tests\\Acceptance\\Tester\\WooCommerce\\TesterWooCommerceAdminNavigationTrait;" . "\n\tuse \\WPDesk\\Codeception\\Tests\\Acceptance\\Tester\\WooCommerce\\TesterWooCommerceCheckoutTrait;";
$this->wpdeskSed($file_pattern, $pattern, $replace);
}
/**
* Execute command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
* @throws FilesystemException
*/
protected function execute(\FSVendor\Symfony\Component\Console\Input\InputInterface $input, \FSVendor\Symfony\Component\Console\Output\OutputInterface $output)
{
$codeceptionDir = './tests/codeception';
$testsDir = $codeceptionDir . '/tests';
$codeceptionYml = 'codeception.dist.yml';
$envConfig = '.env.testing';
$acceptanceYml = 'acceptance.suite.yml';
$bootstrapScript = 'bootstrap.sh';
if (!@\file_exists($testsDir)) {
\mkdir($testsDir, 0777, \true);
}
$this->copyConfigurationFiles($codeceptionDir, $testsDir, $codeceptionYml, $envConfig, $acceptanceYml, $bootstrapScript);
$this->execAndOutput('./vendor/bin/codecept bootstrap ' . $codeceptionDir, $output);
$this->execAndOutput('./vendor/bin/codecept generate:activation acceptance ActivationCest', $output);
$this->execAndOutput('./vendor/bin/codecept generate:woocommerce acceptance WooCommerceCest', $output);
$this->injectTraitsIntoTesterClass($testsDir);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace FSVendor\WPDesk\Composer\Codeception\Commands;
use FSVendor\Symfony\Component\Console\Input\InputArgument;
use FSVendor\Symfony\Component\Console\Input\InputInterface;
use FSVendor\Symfony\Component\Console\Output\OutputInterface;
/**
* Codeception tests run command.
*
* @package WPDesk\Composer\Codeception\Commands
*/
class RunCodeceptionTests extends \FSVendor\WPDesk\Composer\Codeception\Commands\BaseCommand
{
const SINGLE = 'single';
const FAST = 'fast';
const WOOCOMMERCE_VERSION = 'woo_version';
/**
* Configure command.
*/
protected function configure()
{
parent::configure();
$this->setName('run-codeception-tests')->setDescription('Run codeception tests.')->setDefinition(array(new \FSVendor\Symfony\Component\Console\Input\InputArgument(self::SINGLE, \FSVendor\Symfony\Component\Console\Input\InputArgument::OPTIONAL, 'Name of Single test to run.', 'all'), new \FSVendor\Symfony\Component\Console\Input\InputArgument(self::FAST, \FSVendor\Symfony\Component\Console\Input\InputArgument::OPTIONAL, 'Fast tests - do not shutdown docker-compose.', 'slow'), new \FSVendor\Symfony\Component\Console\Input\InputArgument(self::WOOCOMMERCE_VERSION, \FSVendor\Symfony\Component\Console\Input\InputArgument::OPTIONAL, 'WooCommerce version to install.', '')));
}
/**
* Execute command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
protected function execute(\FSVendor\Symfony\Component\Console\Input\InputInterface $input, \FSVendor\Symfony\Component\Console\Output\OutputInterface $output)
{
$dockerComposeYaml = 'vendor/wpdesk/wp-codeception/docker/docker-compose.yaml';
$singleTest = $input->getArgument(self::SINGLE);
$fastTest = $input->getArgument(self::FAST);
$wooVersion = $input->getArgument(self::WOOCOMMERCE_VERSION);
$additionalParameters = '';
if (!empty($singleTest) && 'all' !== $singleTest) {
$additionalParameters .= ' -e CODECEPT_PARAM="acceptance ' . $singleTest . '" ';
}
if (!empty($wooVersion)) {
$additionalParameters .= ' -e WOOCOMMERCE_VERSION="' . $wooVersion . '" ';
}
$runTestsCommand = 'docker-compose -f ' . $dockerComposeYaml . ' run ' . $additionalParameters . 'codecept';
$output->writeln('Codeception command: ' . $runTestsCommand);
$this->execAndOutput($runTestsCommand, $output);
if (empty($fastTest) || self::FAST !== $fastTest) {
$this->execAndOutput('docker-compose -f ' . $dockerComposeYaml . ' down -v', $output);
}
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace FSVendor\WPDesk\Composer\Codeception\Commands;
/**
* Trait with a sed like command
* @see https://pl.wikipedia.org/wiki/Sed_(program)
*
* @package WPDesk\Composer\GitPlugin\Command
*/
trait SedTrait
{
/**
* SED.
*
* @param string $file_pattern .
* @param string $pattern .
* @param string $replace .
*
* @return string[] array of changed files
*/
private function wpdeskSed($file_pattern, $pattern, $replace)
{
$changed_files = [];
foreach (\glob($file_pattern) as $filename) {
$input = \file_get_contents($filename);
$output = \preg_replace($pattern, $replace, $input);
if ($output !== $input) {
$changed_files[] = $filename;
\file_put_contents($filename, $output);
}
}
return $changed_files;
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace FSVendor\WPDesk\Composer\Codeception;
use FSVendor\Composer\Composer;
use FSVendor\Composer\IO\IOInterface;
use FSVendor\Composer\Plugin\Capable;
use FSVendor\Composer\Plugin\PluginInterface;
/**
* Composer plugin.
*
* @package WPDesk\Composer\Codeception
*/
class Plugin implements \FSVendor\Composer\Plugin\PluginInterface, \FSVendor\Composer\Plugin\Capable
{
/**
* @var Composer
*/
private $composer;
/**
* @var IOInterface
*/
private $io;
public function activate(\FSVendor\Composer\Composer $composer, \FSVendor\Composer\IO\IOInterface $io)
{
$this->composer = $composer;
$this->io = $io;
}
public function getCapabilities()
{
return [\FSVendor\Composer\Plugin\Capability\CommandProvider::class => \FSVendor\WPDesk\Composer\Codeception\CommandProvider::class];
}
}