Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP Env PHP Unit debugging #47077

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@
"test:unit:debug": "wp-scripts --inspect-brk test-unit-js --runInBand --no-cache --verbose --config test/unit/jest.config.js ",
"test:unit:profile": "wp-scripts --cpu-prof test-unit-js --runInBand --no-cache --verbose --config test/unit/jest.config.js ",
"pretest:unit:php": "wp-env start",
"pretest:unit:php:debug": "wp-env start --xdebug",
"test:unit:php": "wp-env run tests-wordpress /var/www/html/wp-content/plugins/gutenberg/vendor/bin/phpunit -c /var/www/html/wp-content/plugins/gutenberg/phpunit.xml.dist --verbose",
"test:unit:php:debug": "wp-env run tests-wordpress /var/www/html/wp-content/plugins/gutenberg/vendor/bin/phpunit -c /var/www/html/wp-content/plugins/gutenberg/phpunit.xml.dist --verbose",
"pretest:unit:php:multisite": "wp-env start",
"test:unit:php:multisite": "wp-env run tests-wordpress /var/www/html/wp-content/plugins/gutenberg/vendor/bin/phpunit -c /var/www/html/wp-content/plugins/gutenberg/phpunit/multisite.xml --verbose",
"test:unit:update": "npm run test:unit -- --updateSnapshot",
Expand Down
10 changes: 6 additions & 4 deletions packages/env/lib/build-docker-compose-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ module.exports = function buildDockerComposeConfig( config ) {
const developmentWpImage = `wordpress${
developmentPhpVersion ? ':php' + developmentPhpVersion : ''
}`;
const testsWpImage = `wordpress${
testsPhpVersion ? ':php' + testsPhpVersion : ''
}`;
const testsWpImage = testsPhpVersion
? {
image: `wordpress:php${ testsPhpVersion }`,
}
: { build: '.' };
// Set the WordPress CLI images with the PHP version tag.
const developmentWpCliImage = `wordpress:cli${
! developmentPhpVersion || developmentPhpVersion.length === 0
Expand Down Expand Up @@ -237,8 +239,8 @@ module.exports = function buildDockerComposeConfig( config ) {
volumes: developmentMounts,
},
'tests-wordpress': {
...testsWpImage,
depends_on: [ 'tests-mysql' ],
image: testsWpImage,
ports: [ testsPorts ],
environment: {
...dbEnv.credentials,
Expand Down
2 changes: 2 additions & 0 deletions packages/env/lib/init-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ function installXdebug( enableXdebug ) {

return `
# Install Xdebug:
RUN apt-get update -y && apt-get -y install iproute2
RUN if [ -z "$(pecl list | grep xdebug)" ] ; then pecl install xdebug ; fi
RUN docker-php-ext-enable xdebug
RUN echo 'xdebug.start_with_request=yes' >> /usr/local/etc/php/php.ini
RUN echo 'xdebug.mode=${ enableXdebug }' >> /usr/local/etc/php/php.ini
RUN echo '${ clientDetectSettings }' >> /usr/local/etc/php/php.ini
RUN HOST_IP=$(ip route | awk '/default/ { print $3 }'); echo "xdebug.client_host=\"$HOST_IP\"" >> /usr/local/etc/php/php.ini
Copy link
Member

Choose a reason for hiding this comment

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

What does this change do?

Copy link
Member Author

Choose a reason for hiding this comment

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

When PHPUnit is used, xdebug is not able to detect host ip within the docker container. It can do it through the browser because of some HTTP fields, but that doesn't exist in the CLI. The host IP is the "default gateway" within the docker container, which can be seen using the ip route command. This extracts the default gateway from the ip route command and sets it as host ip.

`;
}