|
| 1 | +name: PHP Compatibility WordPress Test |
| 2 | + |
| 3 | +on: |
| 4 | + # Run on pushes to main branch and on all pull requests |
| 5 | + push: |
| 6 | + branches: [ main ] |
| 7 | + pull_request: |
| 8 | + # Allow manually triggering the workflow |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +# Cancels all previous workflow runs for the same branch that have not yet completed |
| 12 | +concurrency: |
| 13 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 14 | + cancel-in-progress: true |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + |
| 19 | +jobs: |
| 20 | + php-compatibility-test: |
| 21 | + name: PHP ${{ matrix.php-version }} with WordPress Latest |
| 22 | + runs-on: ubuntu-latest |
| 23 | + strategy: |
| 24 | + matrix: |
| 25 | + php-version: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] |
| 26 | + fail-fast: false |
| 27 | + |
| 28 | + services: |
| 29 | + mysql: |
| 30 | + image: mysql:5.7 |
| 31 | + env: |
| 32 | + MYSQL_ALLOW_EMPTY_PASSWORD: false |
| 33 | + MYSQL_ROOT_PASSWORD: root |
| 34 | + MYSQL_DATABASE: wordpress_test |
| 35 | + ports: |
| 36 | + - 3306:3306 |
| 37 | + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 |
| 38 | + |
| 39 | + steps: |
| 40 | + - name: Checkout code |
| 41 | + uses: actions/checkout@v4 |
| 42 | + |
| 43 | + - name: Setup PHP ${{ matrix.php-version }} |
| 44 | + uses: shivammathur/setup-php@v2 |
| 45 | + with: |
| 46 | + php-version: ${{ matrix.php-version }} |
| 47 | + extensions: mysqli, curl, zip, intl, gd, mbstring, fileinfo, xml |
| 48 | + coverage: none |
| 49 | + tools: composer:v2 |
| 50 | + |
| 51 | + - name: Install Subversion |
| 52 | + run: sudo apt-get update && sudo apt-get install -y subversion |
| 53 | + |
| 54 | + - name: Remove the PHP platform requirement |
| 55 | + run: composer config --unset platform.php |
| 56 | + |
| 57 | + - name: Install Composer dependencies |
| 58 | + uses: ramsey/composer-install@v3 |
| 59 | + with: |
| 60 | + dependency-versions: highest |
| 61 | + composer-options: "--prefer-dist --no-progress" |
| 62 | + |
| 63 | + - name: Prepare Database |
| 64 | + run: | |
| 65 | + # Make sure database doesn't exist before creating it |
| 66 | + mysql -u root --password=root --host=127.0.0.1 --port=3306 -e "DROP DATABASE IF EXISTS wordpress_test;" |
| 67 | + # Force creating a fresh database |
| 68 | + mysqladmin -u root --password=root --host=127.0.0.1 --port=3306 --force create wordpress_test |
| 69 | + |
| 70 | + - name: Create tests directory structure |
| 71 | + run: | |
| 72 | + mkdir -p tests/bin |
| 73 | + mkdir -p tests/bootstrap |
| 74 | + |
| 75 | + - name: Create WP tests install script |
| 76 | + run: | |
| 77 | + cat > tests/bin/install-wp-tests.sh << 'EOF' |
| 78 | + #!/usr/bin/env bash |
| 79 | + |
| 80 | + if [ $# -lt 3 ]; then |
| 81 | + echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]" |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | + |
| 85 | + DB_NAME=$1 |
| 86 | + DB_USER=$2 |
| 87 | + DB_PASS=$3 |
| 88 | + DB_HOST=${4-localhost} |
| 89 | + WP_VERSION=${5-latest} |
| 90 | + SKIP_DB_CREATE=${6-false} |
| 91 | + |
| 92 | + WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib} |
| 93 | + WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/} |
| 94 | + |
| 95 | + download() { |
| 96 | + if [ $(which curl) ]; then |
| 97 | + curl -s "$1" > "$2"; |
| 98 | + elif [ $(which wget) ]; then |
| 99 | + wget -nv -O "$2" "$1" |
| 100 | + fi |
| 101 | + } |
| 102 | + |
| 103 | + if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then |
| 104 | + WP_TESTS_TAG="tags/$WP_VERSION" |
| 105 | + elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then |
| 106 | + WP_TESTS_TAG="trunk" |
| 107 | + else |
| 108 | + # http serves a single offer, whereas https serves multiple. we only want one |
| 109 | + download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json |
| 110 | + LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//') |
| 111 | + if [[ -z "$LATEST_VERSION" ]]; then |
| 112 | + echo "Latest WordPress version could not be found" |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | + WP_TESTS_TAG="tags/$LATEST_VERSION" |
| 116 | + fi |
| 117 | + |
| 118 | + set -ex |
| 119 | + |
| 120 | + install_wp() { |
| 121 | + |
| 122 | + if [ -d $WP_CORE_DIR ]; then |
| 123 | + return; |
| 124 | + fi |
| 125 | + |
| 126 | + mkdir -p $WP_CORE_DIR |
| 127 | + |
| 128 | + if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then |
| 129 | + mkdir -p /tmp/wordpress-nightly |
| 130 | + download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip |
| 131 | + unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/ |
| 132 | + mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR |
| 133 | + else |
| 134 | + if [ $WP_VERSION == 'latest' ]; then |
| 135 | + local ARCHIVE_NAME='latest' |
| 136 | + else |
| 137 | + local ARCHIVE_NAME="wordpress-$WP_VERSION" |
| 138 | + fi |
| 139 | + download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz |
| 140 | + tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR |
| 141 | + fi |
| 142 | + |
| 143 | + download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php |
| 144 | + } |
| 145 | + |
| 146 | + install_test_suite() { |
| 147 | + # portable in-place argument for both GNU sed and Mac OSX sed |
| 148 | + if [[ $(uname -s) == 'Darwin' ]]; then |
| 149 | + local ioption='-i.bak' |
| 150 | + else |
| 151 | + local ioption='-i' |
| 152 | + fi |
| 153 | + |
| 154 | + # set up testing suite if it doesn't yet exist |
| 155 | + if [ ! -d $WP_TESTS_DIR ]; then |
| 156 | + # set up testing suite |
| 157 | + mkdir -p $WP_TESTS_DIR |
| 158 | + svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes |
| 159 | + svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data |
| 160 | + fi |
| 161 | + |
| 162 | + if [ ! -f wp-tests-config.php ]; then |
| 163 | + download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php |
| 164 | + # remove all forward slashes in the end |
| 165 | + WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") |
| 166 | + sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php |
| 167 | + sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php |
| 168 | + sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php |
| 169 | + sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php |
| 170 | + sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php |
| 171 | + fi |
| 172 | + } |
| 173 | + |
| 174 | + install_db() { |
| 175 | + if [ ${SKIP_DB_CREATE} = "true" ]; then |
| 176 | + return 0 |
| 177 | + fi |
| 178 | + |
| 179 | + # parse DB_HOST for port or socket references |
| 180 | + local PARTS=(${DB_HOST//\:/ }) |
| 181 | + local DB_HOSTNAME=${PARTS[0]}; |
| 182 | + local DB_SOCK_OR_PORT=${PARTS[1]}; |
| 183 | + local EXTRA="" |
| 184 | + |
| 185 | + if ! [ -z $DB_HOSTNAME ] ; then |
| 186 | + if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then |
| 187 | + EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" |
| 188 | + elif ! [ -z $DB_SOCK_OR_PORT ] ; then |
| 189 | + EXTRA=" --socket=$DB_SOCK_OR_PORT" |
| 190 | + elif ! [ -z $DB_HOSTNAME ] ; then |
| 191 | + EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" |
| 192 | + fi |
| 193 | + fi |
| 194 | + |
| 195 | + # First, ensure database doesn't exist (ignore errors) |
| 196 | + mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA -e "DROP DATABASE IF EXISTS $DB_NAME" || true |
| 197 | + # Now create fresh database with force flag |
| 198 | + mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA --force |
| 199 | + } |
| 200 | + |
| 201 | + install_wp |
| 202 | + install_test_suite |
| 203 | + install_db |
| 204 | + EOF |
| 205 | + chmod +x tests/bin/install-wp-tests.sh |
| 206 | + |
| 207 | + - name: Create Bootstrap File |
| 208 | + run: | |
| 209 | + mkdir -p tests |
| 210 | + cat > tests/bootstrap.php << 'EOF' |
| 211 | + <?php |
| 212 | + /** |
| 213 | + * PHPUnit bootstrap file for plugin tests. |
| 214 | + * |
| 215 | + * @package Simple_WP_Optimizer |
| 216 | + */ |
| 217 | + |
| 218 | + // Give access to tests_add_filter() function. |
| 219 | + require_once '/tmp/wordpress-tests-lib/includes/functions.php'; |
| 220 | + |
| 221 | + /** |
| 222 | + * Manually load the plugin being tested. |
| 223 | + */ |
| 224 | + function _manually_load_plugin() { |
| 225 | + // Make sure widget registration won't throw errors |
| 226 | + add_filter('widgets_init', function() { |
| 227 | + // Empty the action to prevent widget registration errors |
| 228 | + return; |
| 229 | + }, 0); |
| 230 | + |
| 231 | + require dirname( dirname( __FILE__ ) ) . '/simple-wp-optimizer.php'; |
| 232 | + } |
| 233 | + |
| 234 | + // Start up the WP testing environment. |
| 235 | + tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); |
| 236 | + |
| 237 | + require '/tmp/wordpress-tests-lib/includes/bootstrap.php'; |
| 238 | + EOF |
| 239 | + |
| 240 | + - name: Create Test File |
| 241 | + run: | |
| 242 | + cat > tests/test-plugin.php << 'EOF' |
| 243 | + <?php |
| 244 | + /** |
| 245 | + * Class Test_Simple_WP_Optimizer |
| 246 | + * |
| 247 | + * @package Simple_WP_Optimizer |
| 248 | + */ |
| 249 | + |
| 250 | + /** |
| 251 | + * Simple test case for Simple WP Optimizer plugin. |
| 252 | + */ |
| 253 | + class Test_Simple_WP_Optimizer extends WP_UnitTestCase { |
| 254 | + /** |
| 255 | + * Test that the plugin can be loaded correctly. |
| 256 | + * |
| 257 | + * This test simply checks that the plugin loads in WordPress |
| 258 | + * without causing any errors. |
| 259 | + */ |
| 260 | + public function test_plugin_loaded() { |
| 261 | + // Check for at least one function to verify the plugin loaded |
| 262 | + $this->assertTrue(function_exists('es_optimizer_init_settings'), 'Plugin was not loaded correctly'); |
| 263 | + } |
| 264 | + } |
| 265 | + EOF |
| 266 | + |
| 267 | + - name: Create PHPUnit Config |
| 268 | + run: | |
| 269 | + cat > phpunit.xml << 'EOF' |
| 270 | + <?xml version="1.0"?> |
| 271 | + <phpunit |
| 272 | + bootstrap="tests/bootstrap.php" |
| 273 | + backupGlobals="false" |
| 274 | + colors="true" |
| 275 | + convertErrorsToExceptions="true" |
| 276 | + convertNoticesToExceptions="true" |
| 277 | + convertWarningsToExceptions="true" |
| 278 | + > |
| 279 | + <testsuites> |
| 280 | + <testsuite name="Simple WP Optimizer"> |
| 281 | + <directory prefix="test-" suffix=".php">./tests/</directory> |
| 282 | + </testsuite> |
| 283 | + </testsuites> |
| 284 | + </phpunit> |
| 285 | + EOF |
| 286 | + |
| 287 | + - name: Setup WP Tests |
| 288 | + run: | |
| 289 | + bash tests/bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 latest |
| 290 | + |
| 291 | + - name: Run plugin test |
| 292 | + run: vendor/bin/phpunit --config phpunit.xml |
| 293 | + |
| 294 | + - name: Report test status |
| 295 | + if: ${{ always() }} |
| 296 | + run: | |
| 297 | + if [ ${{ job.status }} == 'success' ]; then |
| 298 | + echo "✅ Tests passed successfully on PHP ${{ matrix.php-version }} with the latest WordPress version" |
| 299 | + else |
| 300 | + echo "❌ Tests failed on PHP ${{ matrix.php-version }} with the latest WordPress version" |
| 301 | + exit 1 |
| 302 | + fi |
0 commit comments