|
| 1 | +name: Test |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - trunk |
| 8 | + paths: |
| 9 | + - .github/workflows/test.yml |
| 10 | + pull_request: |
| 11 | + types: |
| 12 | + - opened |
| 13 | + - synchronize |
| 14 | + - ready_for_review |
| 15 | + |
| 16 | +# Cancels all previous workflow runs for pull requests that have not completed. |
| 17 | +concurrency: |
| 18 | + # The concurrency group contains the workflow name and the branch name for pull requests |
| 19 | + # or the commit hash for any other events. |
| 20 | + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }} |
| 21 | + cancel-in-progress: true |
| 22 | + |
| 23 | +jobs: |
| 24 | + # Runs the PHP coding standards checks. |
| 25 | + # |
| 26 | + # Violations are reported inline with annotations. |
| 27 | + # |
| 28 | + # Performs the following steps: |
| 29 | + # - Checks out the repository. |
| 30 | + # - Sets up PHP. |
| 31 | + # - Configures caching for PHPCS scans. |
| 32 | + # - Installs Composer dependencies. |
| 33 | + # - Make Composer packages available globally. |
| 34 | + # - Runs PHPCS on the full codebase. |
| 35 | + # - Generate a report for displaying issues as pull request annotations. |
| 36 | + phpcs: |
| 37 | + name: Run PHPCS coding standards checks |
| 38 | + runs-on: ubuntu-24.04 |
| 39 | + permissions: |
| 40 | + contents: read |
| 41 | + timeout-minutes: 20 |
| 42 | + |
| 43 | + steps: |
| 44 | + - name: Checkout repository |
| 45 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 46 | + with: |
| 47 | + show-progress: ${{ runner.debug == '1' && 'true' || 'false' }} |
| 48 | + persist-credentials: false |
| 49 | + |
| 50 | + - name: Set up PHP |
| 51 | + uses: shivammathur/setup-php@9e72090525849c5e82e596468b86eb55e9cc5401 # v2.32.0 |
| 52 | + with: |
| 53 | + php-version: '8.3' |
| 54 | + coverage: none |
| 55 | + tools: cs2pr |
| 56 | + |
| 57 | + # This date is used to ensure that the PHPCS cache is cleared at least once every week. |
| 58 | + # http://man7.org/linux/man-pages/man1/date.1.html |
| 59 | + - name: "Get last Monday's date" |
| 60 | + id: get-date |
| 61 | + run: echo "date=$(/bin/date -u --date='last Mon' "+%F")" >> "$GITHUB_OUTPUT" |
| 62 | + |
| 63 | + - name: Cache PHPCS scan cache |
| 64 | + uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 |
| 65 | + with: |
| 66 | + path: tests/_output/phpcs-cache.json |
| 67 | + key: ${{ runner.os }}-date-${{ steps.get-date.outputs.date }}-phpcs-cache-${{ hashFiles('**/composer.json', 'phpcs.xml.dist') }} |
| 68 | + |
| 69 | + # Since Composer dependencies are installed using `composer update` and no lock file is in version control, |
| 70 | + # passing a custom cache suffix ensures that the cache is flushed at least once per week. |
| 71 | + - name: Install Composer dependencies |
| 72 | + uses: ramsey/composer-install@a2636af0004d1c0499ffca16ac0b4cc94df70565 # v3.1.0 |
| 73 | + with: |
| 74 | + custom-cache-suffix: ${{ steps.get-date.outputs.date }} |
| 75 | + |
| 76 | + - name: Make Composer packages available globally |
| 77 | + run: echo "${PWD}/vendor/bin" >> "$GITHUB_PATH" |
| 78 | + |
| 79 | + - name: Run PHPCS |
| 80 | + id: phpcs |
| 81 | + run: composer lint -- --report-full --report-checkstyle=./tests/_output/phpcs-report.xml |
| 82 | + |
| 83 | + - name: Show PHPCS results in PR |
| 84 | + if: ${{ always() && steps.phpcs.outcome == 'failure' }} |
| 85 | + run: cs2pr ./tests/_output/phpcs-report.xml |
| 86 | + |
| 87 | + # Runs PHP static analysis tests. |
| 88 | + # |
| 89 | + # Violations are reported inline with annotations. |
| 90 | + # |
| 91 | + # Performs the following steps: |
| 92 | + # - Checks out the repository. |
| 93 | + # - Sets up PHP. |
| 94 | + # - Installs Composer dependencies. |
| 95 | + # - Configures caching for PHP static analysis scans. |
| 96 | + # - Make Composer packages available globally. |
| 97 | + # - Runs PHPStan static analysis (with Pull Request annotations). |
| 98 | + # - Saves the PHPStan result cache. |
| 99 | + # - Ensures version-controlled files are not modified or deleted. |
| 100 | + phpstan: |
| 101 | + name: Run PHP static analysis |
| 102 | + runs-on: ubuntu-24.04 |
| 103 | + permissions: |
| 104 | + contents: read |
| 105 | + timeout-minutes: 20 |
| 106 | + |
| 107 | + steps: |
| 108 | + - name: Checkout repository |
| 109 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 110 | + with: |
| 111 | + show-progress: ${{ runner.debug == '1' && 'true' || 'false' }} |
| 112 | + persist-credentials: false |
| 113 | + |
| 114 | + - name: Set up PHP |
| 115 | + uses: shivammathur/setup-php@9e72090525849c5e82e596468b86eb55e9cc5401 # v2.32.0 |
| 116 | + with: |
| 117 | + php-version: 8.3 |
| 118 | + coverage: none |
| 119 | + tools: cs2pr |
| 120 | + |
| 121 | + # This date is used to ensure that the Composer cache is cleared at least once every week. |
| 122 | + # http://man7.org/linux/man-pages/man1/date.1.html |
| 123 | + - name: "Get last Monday's date" |
| 124 | + id: get-date |
| 125 | + run: echo "date=$(/bin/date -u --date='last Mon' "+%F")" >> "$GITHUB_OUTPUT" |
| 126 | + |
| 127 | + # Since Composer dependencies are installed using `composer update` and no lock file is in version control, |
| 128 | + # passing a custom cache suffix ensures that the cache is flushed at least once per week. |
| 129 | + - name: Install Composer dependencies |
| 130 | + uses: ramsey/composer-install@a2636af0004d1c0499ffca16ac0b4cc94df70565 # v3.1.0 |
| 131 | + with: |
| 132 | + custom-cache-suffix: ${{ steps.get-date.outputs.date }} |
| 133 | + |
| 134 | + - name: Make Composer packages available globally |
| 135 | + run: echo "${PWD}/vendor/bin" >> "$GITHUB_PATH" |
| 136 | + |
| 137 | + - name: Cache PHP Static Analysis scan cache |
| 138 | + uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 |
| 139 | + with: |
| 140 | + path: tests/_output # This is defined in the base.neon file. |
| 141 | + key: 'phpstan-result-cache-${{ github.run_id }}' |
| 142 | + restore-keys: | |
| 143 | + phpstan-result-cache- |
| 144 | +
|
| 145 | + - name: Run PHP static analysis tests |
| 146 | + id: phpstan |
| 147 | + run: phpstan analyse -vvv --error-format=checkstyle | cs2pr |
| 148 | + |
| 149 | + - name: 'Save result cache' |
| 150 | + uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 |
| 151 | + if: ${{ !cancelled() }} |
| 152 | + with: |
| 153 | + path: tests/_output |
| 154 | + key: 'phpstan-result-cache-${{ github.run_id }}' |
| 155 | + |
| 156 | + # Runs the PHPUnit tests for WordPress. |
| 157 | + # |
| 158 | + # Performs the following steps: |
| 159 | + # - Sets environment variables. |
| 160 | + # - Checks out the repository. |
| 161 | + # - Sets up Node.js. |
| 162 | + # - Sets up PHP. |
| 163 | + # - Installs Composer dependencies. |
| 164 | + # - Installs npm dependencies |
| 165 | + # - Logs general debug information about the runner. |
| 166 | + # - Logs Docker debug information (about the Docker installation within the runner). |
| 167 | + # - Starts the WordPress Docker container. |
| 168 | + # - Logs the running Docker containers. |
| 169 | + # - Logs debug information about what's installed within the WordPress Docker containers. |
| 170 | + # - Install WordPress within the Docker container. |
| 171 | + # - Run the PHPUnit tests. |
| 172 | + # - Upload the code coverage report to Codecov.io. |
| 173 | + # - Upload the HTML code coverage report as an artifact. |
| 174 | + # - Ensures version-controlled files are not modified or deleted. |
| 175 | + # - Checks out the WordPress Test reporter repository. |
| 176 | + # - Submit the test results to the WordPress.org host test results. |
| 177 | + phpunit: |
| 178 | + name: Test PHP ${{ matrix.php }} WP ${{ matrix.wp }}${{ matrix.coverage && ' with coverage' || '' }} |
| 179 | + runs-on: ubuntu-24.04 |
| 180 | + strategy: |
| 181 | + matrix: |
| 182 | + php: ['8.4', '8.3', '8.2', '8.1', '8.0', '7.4'] |
| 183 | + wp: [latest, trunk, '6.7'] |
| 184 | + coverage: [false] |
| 185 | + include: |
| 186 | + - php: '8.4' |
| 187 | + wp: latest |
| 188 | + coverage: true |
| 189 | + env: |
| 190 | + WP_ENV_PHP_VERSION: ${{ matrix.php }} |
| 191 | + WP_ENV_CORE: ${{ matrix.wp == 'trunk' && 'WordPress/WordPress' || format( 'https://wordpress.org/wordpress-{0}.zip', matrix.wp ) }} |
| 192 | + |
| 193 | + steps: |
| 194 | + - name: Configure environment variables |
| 195 | + run: | |
| 196 | + echo "PHP_FPM_UID=$(id -u)" >> "$GITHUB_ENV" |
| 197 | + echo "PHP_FPM_GID=$(id -g)" >> "$GITHUB_ENV" |
| 198 | +
|
| 199 | + - name: Checkout repository |
| 200 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 201 | + with: |
| 202 | + show-progress: ${{ runner.debug == '1' && 'true' || 'false' }} |
| 203 | + persist-credentials: false |
| 204 | + |
| 205 | + ## |
| 206 | + # This allows Composer dependencies to be installed using a single step. |
| 207 | + # |
| 208 | + # Since the tests are currently run within the Docker containers where the PHP version varies, |
| 209 | + # the same PHP version needs to be configured for the action runner machine so that the correct |
| 210 | + # dependency versions are installed and cached. |
| 211 | + ## |
| 212 | + - name: Set up PHP |
| 213 | + uses: shivammathur/setup-php@9e72090525849c5e82e596468b86eb55e9cc5401 # v2.32.0 |
| 214 | + with: |
| 215 | + php-version: '${{ matrix.php }}' |
| 216 | + coverage: none |
| 217 | + |
| 218 | + # Since Composer dependencies are installed using `composer update` and no lock file is in version control, |
| 219 | + # passing a custom cache suffix ensures that the cache is flushed at least once per week. |
| 220 | + - name: Install Composer dependencies |
| 221 | + uses: ramsey/composer-install@a2636af0004d1c0499ffca16ac0b4cc94df70565 # v3.1.0 |
| 222 | + with: |
| 223 | + custom-cache-suffix: $(/bin/date -u --date='last Mon' "+%F") |
| 224 | + |
| 225 | + - name: Setup Node |
| 226 | + uses: actions/setup-node@v4 |
| 227 | + with: |
| 228 | + cache: 'npm' |
| 229 | + node-version-file: '.nvmrc' |
| 230 | + |
| 231 | + - name: Install NPM dependencies |
| 232 | + run: npm ci |
| 233 | + |
| 234 | + - name: Start the Docker testing environment |
| 235 | + uses: nick-fields/retry@ce71cc2ab81d554ebbe88c79ab5975992d79ba08 # v3.0.2 |
| 236 | + with: |
| 237 | + timeout_minutes: 10 |
| 238 | + max_attempts: 3 |
| 239 | + command: | |
| 240 | + if [ "${{ matrix.coverage }}" == "true" ]; then |
| 241 | + npm run wp-env start -- --xdebug=coverage |
| 242 | + else |
| 243 | + npm run wp-env start |
| 244 | + fi |
| 245 | +
|
| 246 | + - name: Log versions |
| 247 | + run: | |
| 248 | + npm run wp-env -- run cli php -- -v |
| 249 | + npm run wp-env -- run cli wp core version |
| 250 | +
|
| 251 | + - name: Run PHPUnit tests${{ matrix.coverage && ' with coverage report' || '' }} |
| 252 | + continue-on-error: true |
| 253 | + id: phpunit |
| 254 | + run: | |
| 255 | + npm run test:php |
| 256 | +
|
| 257 | + - name: Upload code coverage report |
| 258 | + continue-on-error: true |
| 259 | + if: ${{ matrix.coverage }} |
| 260 | + uses: codecov/codecov-action@0565863a31f2c772f9f0395002a31e3f06189574 # v5.4.0 |
| 261 | + with: |
| 262 | + token: ${{ secrets.CODECOV_TOKEN }} |
| 263 | + files: tests/_output/php-coverage.xml |
| 264 | + flags: unit |
| 265 | + fail_ci_if_error: true |
| 266 | + |
| 267 | + - name: Upload HTML coverage report as artifact |
| 268 | + if: ${{ matrix.coverage }} |
| 269 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 |
| 270 | + with: |
| 271 | + name: wp-code-coverage-${{ matrix.php }}-${{ matrix.wp }} |
| 272 | + path: tests/_output/html |
| 273 | + overwrite: true |
0 commit comments