Skip to content

Commit

Permalink
wp-env phpMyAdmin support: use config keys, support both envs
Browse files Browse the repository at this point in the history
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: {
      ...
    }
  }
}
  • Loading branch information
mcsf committed Dec 6, 2024
1 parent 54bbfcd commit fc7cad4
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 29 deletions.
18 changes: 16 additions & 2 deletions packages/env/lib/build-docker-compose-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -271,14 +275,24 @@ module.exports = function buildDockerComposeConfig( config ) {
},
phpmyadmin: {
image: 'phpmyadmin',
ports: [ phpmyadminPorts ],
ports: [ developmentPhpmyadminPorts ],
environment: {
PMA_PORT: 3306,
PMA_HOST: 'mysql',
PMA_USER: 'root',
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: {} } ),
Expand Down
6 changes: 0 additions & 6 deletions packages/env/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
38 changes: 28 additions & 10 deletions packages/env/lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
} ) {
Expand Down Expand Up @@ -182,7 +180,7 @@ module.exports = async function start( {
}
);

if ( phpmyadmin ) {
if ( config.env.development.phpmyadminPort ) {
await dockerCompose.upOne( 'phpmyadmin', {
...dockerComposeConfig,
commandOptions: shouldConfigureWp
Expand All @@ -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 } );
Expand Down Expand Up @@ -248,18 +255,29 @@ 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 }` : '.' ),
'WordPress test site started' +
( 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' );
Expand Down
4 changes: 4 additions & 0 deletions packages/env/lib/config/get-config-from-environment-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<string, string>} lifecycleScripts An override for various lifecycle scripts.
Expand All @@ -43,6 +44,9 @@ module.exports = function getConfigFromEnvironmentVars( cacheDirectoryPath ) {
phpmyadminPort: getPortFromEnvironmentVariable(
'WP_ENV_PHPMYADMIN_PORT'
),
testsPhpmyadminPort: getPortFromEnvironmentVariable(
'WP_ENV_TESTS_PHPMYADMIN_PORT'
),
lifecycleScripts: getLifecycleScriptOverrides(),
};

Expand Down
4 changes: 2 additions & 2 deletions packages/env/lib/config/parse-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<string, WPSource>} 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.
Expand Down Expand Up @@ -88,7 +88,7 @@ const DEFAULT_ENVIRONMENT_CONFIG = {
port: 8888,
testsPort: 8889,
mysqlPort: null,
phpmyadminPort: 9000,
phpmyadminPort: null,
mappings: {},
config: {
FS_METHOD: 'direct',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down Expand Up @@ -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": [],
Expand Down Expand Up @@ -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": [],
Expand Down Expand Up @@ -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": [],
Expand Down Expand Up @@ -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": [],
Expand Down Expand Up @@ -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": [],
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
55 changes: 54 additions & 1 deletion packages/env/lib/config/test/parse-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const DEFAULT_CONFIG = {
port: 8888,
testsPort: 8889,
mysqlPort: null,
phpmyadminPort: 9000,
phpmyadminPort: null,
phpVersion: null,
coreSource: {
type: 'git',
Expand Down Expand Up @@ -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 );
} );
} );

0 comments on commit fc7cad4

Please sign in to comment.