From fc7cad4bf2ac676359efc1f93dc6bc36ad94b593 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca <150562+mcsf@users.noreply.github.com> Date: Fri, 6 Dec 2024 16:46:50 +0000 Subject: [PATCH] wp-env phpMyAdmin support: use config keys, support both envs Two docker-compose services are now defined: `phpmyadmin` and `tests-phpmyadmin`. These are off by default. They can be individually turned on by specifying a port in the each enviroment's config: { env: { development: { ... phpmyadminPort: 9000 }, tests: { ... } } } --- .../env/lib/build-docker-compose-config.js | 18 +++++- packages/env/lib/cli.js | 6 -- packages/env/lib/commands/start.js | 38 +++++++++---- .../get-config-from-environment-vars.js | 4 ++ packages/env/lib/config/parse-config.js | 4 +- .../__snapshots__/config-integration.js.snap | 16 +++--- packages/env/lib/config/test/parse-config.js | 55 ++++++++++++++++++- 7 files changed, 112 insertions(+), 29 deletions(-) diff --git a/packages/env/lib/build-docker-compose-config.js b/packages/env/lib/build-docker-compose-config.js index 88e4d0bb241b28..a1a4f68256b688 100644 --- a/packages/env/lib/build-docker-compose-config.js +++ b/packages/env/lib/build-docker-compose-config.js @@ -173,9 +173,13 @@ module.exports = function buildDockerComposeConfig( config ) { const testsMysqlPorts = `\${WP_ENV_TESTS_MYSQL_PORT:-${ config.env.tests.mysqlPort ?? '' }}:3306`; - const phpmyadminPorts = `\${WP_ENV_PHPMYADMIN_PORT:-${ + + const developmentPhpmyadminPorts = `\${WP_ENV_PHPMYADMIN_PORT:-${ config.env.development.phpmyadminPort ?? '' }}:80`; + const testsPhpmyadminPorts = `\${WP_ENV_TESTS_PHPMYADMIN_PORT:-${ + config.env.tests.phpmyadminPort ?? '' + }}:80`; return { services: { @@ -271,7 +275,7 @@ module.exports = function buildDockerComposeConfig( config ) { }, phpmyadmin: { image: 'phpmyadmin', - ports: [ phpmyadminPorts ], + ports: [ developmentPhpmyadminPorts ], environment: { PMA_PORT: 3306, PMA_HOST: 'mysql', @@ -279,6 +283,16 @@ module.exports = function buildDockerComposeConfig( config ) { PMA_PASSWORD: 'password', }, }, + 'tests-phpmyadmin': { + image: 'phpmyadmin', + ports: [ testsPhpmyadminPorts ], + environment: { + PMA_PORT: 3306, + PMA_HOST: 'tests-mysql', + PMA_USER: 'root', + PMA_PASSWORD: 'password', + }, + }, }, volumes: { ...( ! config.env.development.coreSource && { wordpress: {} } ), diff --git a/packages/env/lib/cli.js b/packages/env/lib/cli.js index 5eefffa1983b40..896df6cd59fed0 100644 --- a/packages/env/lib/cli.js +++ b/packages/env/lib/cli.js @@ -133,12 +133,6 @@ module.exports = function cli() { 'Download source updates and apply WordPress configuration.', default: false, } ); - args.option( 'phpmyadmin', { - describe: - 'Enables PHPMyAdmin. By default, a PHPMyAdmin server will be available on port 9000 (override with WP_ENV_PHPMYADMIN_PORT).', - type: 'boolean', - default: false, - } ); args.option( 'xdebug', { describe: 'Enables Xdebug. If not passed, Xdebug is turned off. If no modes are set, uses "debug". You may set multiple Xdebug modes by passing them in a comma-separated list: `--xdebug=develop,coverage`. See https://xdebug.org/docs/all_settings#mode for information about Xdebug modes.', diff --git a/packages/env/lib/commands/start.js b/packages/env/lib/commands/start.js index e48ffd7602a8b9..24d57d71fd3c62 100644 --- a/packages/env/lib/commands/start.js +++ b/packages/env/lib/commands/start.js @@ -43,18 +43,16 @@ const CONFIG_CACHE_KEY = 'config_checksum'; * Starts the development server. * * @param {Object} options - * @param {Object} options.spinner A CLI spinner which indicates progress. - * @param {boolean} options.update If true, update sources. - * @param {string} options.xdebug The Xdebug mode to set. - * @param {string} options.phpmyadmin Indicated whether or not PHPMyAdmin should be started. - * @param {boolean} options.scripts Indicates whether or not lifecycle scripts should be executed. - * @param {boolean} options.debug True if debug mode is enabled. + * @param {Object} options.spinner A CLI spinner which indicates progress. + * @param {boolean} options.update If true, update sources. + * @param {string} options.xdebug The Xdebug mode to set. + * @param {boolean} options.scripts Indicates whether or not lifecycle scripts should be executed. + * @param {boolean} options.debug True if debug mode is enabled. */ module.exports = async function start( { spinner, update, xdebug, - phpmyadmin, scripts, debug, } ) { @@ -182,7 +180,7 @@ module.exports = async function start( { } ); - if ( phpmyadmin ) { + if ( config.env.development.phpmyadminPort ) { await dockerCompose.upOne( 'phpmyadmin', { ...dockerComposeConfig, commandOptions: shouldConfigureWp @@ -191,6 +189,15 @@ module.exports = async function start( { } ); } + if ( config.env.tests.phpmyadminPort ) { + await dockerCompose.upOne( 'tests-phpmyadmin', { + ...dockerComposeConfig, + commandOptions: shouldConfigureWp + ? [ '--build', '--force-recreate' ] + : [], + } ); + } + // Make sure we've consumed the custom CLI dockerfile. if ( shouldConfigureWp ) { await dockerCompose.buildOne( [ 'cli' ], { ...dockerComposeConfig } ); @@ -248,10 +255,18 @@ module.exports = async function start( { dockerComposeConfig ); - const phpmyadminPort = phpmyadmin + const phpmyadminPort = config.env.development.phpmyadminPort ? await getPublicDockerPort( 'phpmyadmin', 80, dockerComposeConfig ) : null; + const testsPhpmyadminPort = config.env.tests.phpmyadminPort + ? await getPublicDockerPort( + 'tests-phpmyadmin', + 80, + dockerComposeConfig + ) + : null; + spinner.prefixText = [ 'WordPress development site started' + ( siteUrl ? ` at ${ siteUrl }` : '.' ), @@ -259,7 +274,10 @@ module.exports = async function start( { ( testsSiteUrl ? ` at ${ testsSiteUrl }` : '.' ), `MySQL is listening on port ${ mySQLPort }`, `MySQL for automated testing is listening on port ${ testsMySQLPort }`, - phpmyadminPort && `phpMyAdmin is listening on port ${ phpmyadminPort }`, + phpmyadminPort && + `phpMyAdmin started at http://localhost:${ phpmyadminPort }`, + testsPhpmyadminPort && + `phpMyAdmin for automated testing started at http://localhost:${ testsPhpmyadminPort }`, ] .filter( Boolean ) .join( '\n' ); diff --git a/packages/env/lib/config/get-config-from-environment-vars.js b/packages/env/lib/config/get-config-from-environment-vars.js index 95058ee85bfd8f..618b5fff257920 100644 --- a/packages/env/lib/config/get-config-from-environment-vars.js +++ b/packages/env/lib/config/get-config-from-environment-vars.js @@ -20,6 +20,7 @@ const { checkPort, checkVersion, checkString } = require( './validate-config' ); * @property {?number} mysqlPort An override for the development environment's MySQL port. * @property {?number} testsPort An override for the testing environment's port. * @property {?number} testsMysqlPort An override for the testing environment's MySQL port. + * @property {?number} phpmyadminPort An override for the development environment's phpMyAdmin port. * @property {?WPSource} coreSource An override for all environment's coreSource. * @property {?string} phpVersion An override for all environment's PHP version. * @property {?Object.} lifecycleScripts An override for various lifecycle scripts. @@ -43,6 +44,9 @@ module.exports = function getConfigFromEnvironmentVars( cacheDirectoryPath ) { phpmyadminPort: getPortFromEnvironmentVariable( 'WP_ENV_PHPMYADMIN_PORT' ), + testsPhpmyadminPort: getPortFromEnvironmentVariable( + 'WP_ENV_TESTS_PHPMYADMIN_PORT' + ), lifecycleScripts: getLifecycleScriptOverrides(), }; diff --git a/packages/env/lib/config/parse-config.js b/packages/env/lib/config/parse-config.js index 91ecb54d80856b..bddd7bc72aaee0 100644 --- a/packages/env/lib/config/parse-config.js +++ b/packages/env/lib/config/parse-config.js @@ -51,7 +51,7 @@ const mergeConfigs = require( './merge-configs' ); * @property {WPSource[]} themeSources Themes to load in the environment. * @property {number} port The port to use. * @property {number} mysqlPort The port to use for MySQL. Random if empty. - * @property {number} phpmyadminPort The port to use for PHPMyAdmin. + * @property {number} phpmyadminPort The port to use for phpMyAdmin. If empty, disabled phpMyAdmin. * @property {Object} config Mapping of wp-config.php constants to their desired values. * @property {Object.} mappings Mapping of WordPress directories to local directories which should be mounted. * @property {string|null} phpVersion Version of PHP to use in the environments, of the format 0.0. @@ -88,7 +88,7 @@ const DEFAULT_ENVIRONMENT_CONFIG = { port: 8888, testsPort: 8889, mysqlPort: null, - phpmyadminPort: 9000, + phpmyadminPort: null, mappings: {}, config: { FS_METHOD: 'direct', diff --git a/packages/env/lib/config/test/__snapshots__/config-integration.js.snap b/packages/env/lib/config/test/__snapshots__/config-integration.js.snap index d91d44ff6f96b2..6b671a6bc858eb 100644 --- a/packages/env/lib/config/test/__snapshots__/config-integration.js.snap +++ b/packages/env/lib/config/test/__snapshots__/config-integration.js.snap @@ -31,7 +31,7 @@ exports[`Config Integration should load local and override configuration files 1 "mappings": {}, "mysqlPort": 23306, "phpVersion": null, - "phpmyadminPort": 9000, + "phpmyadminPort": null, "pluginSources": [], "port": 999, "themeSources": [], @@ -61,7 +61,7 @@ exports[`Config Integration should load local and override configuration files 1 "mappings": {}, "mysqlPort": 23307, "phpVersion": null, - "phpmyadminPort": 9000, + "phpmyadminPort": null, "pluginSources": [], "port": 456, "themeSources": [], @@ -108,7 +108,7 @@ exports[`Config Integration should load local configuration file 1`] = ` "mappings": {}, "mysqlPort": 13306, "phpVersion": null, - "phpmyadminPort": 9000, + "phpmyadminPort": null, "pluginSources": [], "port": 123, "themeSources": [], @@ -138,7 +138,7 @@ exports[`Config Integration should load local configuration file 1`] = ` "mappings": {}, "mysqlPort": 23307, "phpVersion": null, - "phpmyadminPort": 9000, + "phpmyadminPort": null, "pluginSources": [], "port": 8889, "themeSources": [], @@ -185,7 +185,7 @@ exports[`Config Integration should use default configuration 1`] = ` "mappings": {}, "mysqlPort": null, "phpVersion": null, - "phpmyadminPort": 9000, + "phpmyadminPort": null, "pluginSources": [], "port": 8888, "themeSources": [], @@ -215,7 +215,7 @@ exports[`Config Integration should use default configuration 1`] = ` "mappings": {}, "mysqlPort": null, "phpVersion": null, - "phpmyadminPort": 9000, + "phpmyadminPort": null, "pluginSources": [], "port": 8889, "themeSources": [], @@ -262,7 +262,7 @@ exports[`Config Integration should use environment variables over local and over "mappings": {}, "mysqlPort": 23306, "phpVersion": null, - "phpmyadminPort": 9000, + "phpmyadminPort": null, "pluginSources": [], "port": 12345, "testsPort": 61234, @@ -293,7 +293,7 @@ exports[`Config Integration should use environment variables over local and over "mappings": {}, "mysqlPort": 23307, "phpVersion": null, - "phpmyadminPort": 9000, + "phpmyadminPort": null, "pluginSources": [], "port": 61234, "testsPort": 61234, diff --git a/packages/env/lib/config/test/parse-config.js b/packages/env/lib/config/test/parse-config.js index 6cd4f08280302d..cc6e2c7a96bbc0 100644 --- a/packages/env/lib/config/test/parse-config.js +++ b/packages/env/lib/config/test/parse-config.js @@ -22,7 +22,7 @@ const DEFAULT_CONFIG = { port: 8888, testsPort: 8889, mysqlPort: null, - phpmyadminPort: 9000, + phpmyadminPort: null, phpVersion: null, coreSource: { type: 'git', @@ -401,4 +401,57 @@ describe( 'parseConfig', () => { ) ); } ); + + it( 'should parse phpmyadmin configuration for a given environment', async () => { + readRawConfigFile.mockImplementation( async ( configFile ) => { + if ( configFile === '/test/gutenberg/.wp-env.json' ) { + return { + core: 'WordPress/WordPress#Test', + phpVersion: '1.0', + lifecycleScripts: { + afterStart: 'test', + }, + env: { + development: { + phpmyadminPort: 9001, + }, + }, + }; + } + } ); + + const parsed = await parseConfig( '/test/gutenberg', '/cache' ); + + const expected = { + development: { + ...DEFAULT_CONFIG.env.development, + phpmyadminPort: 9001, + }, + tests: DEFAULT_CONFIG.env.tests, + }; + expect( parsed.env ).toEqual( expected ); + } ); + + it( 'should ignore root-level configuration for phpmyadmin', async () => { + readRawConfigFile.mockImplementation( async ( configFile ) => { + if ( configFile === '/test/gutenberg/.wp-env.json' ) { + return { + core: 'WordPress/WordPress#Test', + phpVersion: '1.0', + lifecycleScripts: { + afterStart: 'test', + }, + phpmyadminPort: 9001, + }; + } + } ); + + const parsed = await parseConfig( '/test/gutenberg', '/cache' ); + + const expected = { + development: DEFAULT_CONFIG.env.development, + tests: DEFAULT_CONFIG.env.tests, + }; + expect( parsed.env ).toEqual( expected ); + } ); } );