diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 000000000..70bee2cd4 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,132 @@ +name: Timber Tests + +on: + push: + branches: + - '*' + pull_request: + types: [opened, synchronize, ready_for_review] + +jobs: + phpunit: + runs-on: ubuntu-latest + + services: + mysql: + image: mariadb:latest + env: + MYSQL_ALLOW_EMPTY_PASSWORD: true + MYSQL_ROOT_PASSWORD: + MYSQL_DATABASE: wordpress_test + ports: + - 3306 + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 + + continue-on-error: ${{ matrix.experimental == true }} + strategy: + + fail-fast: false + matrix: + php: ['7.4'] + wp: ['5.3.6', 'latest'] + multisite: ['0', '1'] + dependency-version: [prefer-stable] # prefer-lowest + webp: [false] + coverage: [false] + extensions: ['gd'] + include: + # WP Trunk + - php: '7.4' + wp: 'trunk' + dependency-version: 'prefer-stable' + multisite: '0' + experimental: true + # PHP with Imagick + - php: '7.4' + wp: 'latest' + dependency-version: 'prefer-stable' + multisite: '0' + extensions: 'imagick' + # Coverage + - php: '7.4' + wp: 'latest' + dependency-version: 'prefer-stable' + multisite: '0' + coverage: true + + name: PHP ${{ matrix.php }} ${{ matrix.extensions == 'imagick' && ' (Imagick)' || '' }}${{ matrix.coverage && ' (with coverage)' || '' }} ${{ matrix.webp && ' (webp)' || '' }} | WP ${{ matrix.wp }}${{ matrix.multisite == 1 && ' (MS)' || '' }} | ${{ matrix.dependency-version }} + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: ${{ matrix.coverage && 'pcov' || 'none' }} + tools: composer:v2 + extensions: curl, date, dom, iconv, json, libxml, mysql, ${{ matrix.extensions }} + ini-values: pcov.directory=lib + + - name: Shutdown default MySQL service + run: sudo service mysql stop + + - name: Verify MariaDB connection + run: | + while ! mysqladmin ping -h"127.0.0.1" -P"${{ job.services.mysql.ports[3306] }}" --silent; do + sleep 1 + done + + - name: Setup problem matchers for PHP + run: echo "::add-matcher::${{ runner.tool_cache }}/php.json" + + - name: Setup problem matchers for PHPUnit + run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Get composer cache directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache composer dependencies + uses: actions/cache@v2 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-${{ matrix.dependency-version }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.dependency-version }}-${{ matrix.php }}-composer- + + - name: Install composer dependencies + run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-progress --no-interaction + + - name: Install tests + run: bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1:${{ job.services.mysql.ports['3306'] }} ${{ matrix.wp }} true + + - name: Setup PCOV + if: ${{ matrix.coverage == true }} + run: | + composer require --dev --ignore-platform-reqs pcov/clobber + vendor/bin/pcov clobber + + - name: Run tests + if: ${{ !matrix.webp }} + run: vendor/bin/phpunit -c phpunit-nocover.xml --verbose --colors=always + env: + WP_MULTISITE: ${{ matrix.multisite }} + + - name: Run tests with coverage + if: ${{ matrix.coverage == true }} + run: | + vendor/bin/phpunit -c phpunit.xml --verbose --colors=always + + - name: Upload coverage results to Coveralls + if: ${{ matrix.coverage == true }} + env: + COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + composer global require php-coveralls/php-coveralls + php-coveralls --coverage_clover=build/logs/clover.xml -v + + - name: Run webp tests + if: ${{ matrix.webp }} + run: vendor/phpunit/phpunit/phpunit -c phpunit.xml --verbose ./tests/test-timber-image-towebp.php diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 004632778..000000000 --- a/.travis.yml +++ /dev/null @@ -1,120 +0,0 @@ -# TravisCI configuration for timber/timber - -os: linux - -dist: bionic - -services: mysql - -language: php - -addons: - apt: - packages: - - libwebp-dev - - apt-transport-https - - apt-utils - -php: - - 7.2 - -env: - global: - - TMPDIR=/tmp - - COVERALLS_PARALLEL=true - jobs: - - WP_VERSION=4.9.8 WP_MULTISITE=0 - - WP_VERSION=4.9.8 WP_MULTISITE=1 - - WP_VERSION=latest WP_MULTISITE=0 - - WP_VERSION=latest WP_MULTISITE=1 - -jobs: - include: - - name: "Coverage: WP 4.9 on PHP 5.6" - php: 5.6 - dist: xenial - env: WP_VERSION=4.9.8 WP_MULTISITE=0 PREFER_LOWEST="--prefer-lowest --prefer-stable" - before_install: skip - install: - - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION - - composer update $PREFER_LOWEST --prefer-source - - composer require --dev 'satooshi/php-coveralls:1.0.*' - script: - - mkdir -p build/logs - - vendor/phpunit/phpunit/phpunit -c phpunit.xml --verbose --coverage-clover build/logs/clover.xml - after_success: - - vendor/bin/coveralls -v - - name: "Coverage: ImageMagick extension on PHP 7.4" - php: 7.4 - env: WP_VERSION=latest WP_MULTISITE=0 - before_install: - - phpenv config-rm xdebug.ini - - yes '' | pecl install pcov - # Install imagick PHP extension - - pear config-set preferred_state beta - - pecl channel-update pecl.php.net - - yes '' | pecl install imagick - install: - - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION - - composer update $PREFER_LOWEST --prefer-source - - composer require --dev 'pcov/clobber:^2.0' - - vendor/bin/pcov clobber - - composer require --dev 'satooshi/php-coveralls:1.0.*' - script: - - mkdir -p build/logs - - vendor/phpunit/phpunit/phpunit -c phpunit.xml --verbose --coverage-clover build/logs/clover.xml - after_success: - - vendor/bin/coveralls -v - - name: "Coverage: WP multisite on PHP 7.4" - php: 7.4 - env: WP_VERSION=latest WP_MULTISITE=1 - before_install: - - phpenv config-rm xdebug.ini - - yes '' | pecl install pcov - install: - - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION - - composer update $PREFER_LOWEST --prefer-source - - composer require --dev 'pcov/clobber:^2.0' - - vendor/bin/pcov clobber - - composer require --dev 'satooshi/php-coveralls:1.0.*' - script: - - mkdir -p build/logs - - vendor/phpunit/phpunit/phpunit -c phpunit.xml --verbose --coverage-clover build/logs/clover.xml - after_success: - - vendor/bin/coveralls -v - - name: "WebP support on PHP 7.4" - php: 7.4 - env: WP_VERSION=latest WP_MULTISITE=0 - before_install: - # Install PHP with WebP support - - LC_ALL=C.UTF-8 sudo add-apt-repository -y ppa:ondrej/php - - sudo apt-get update - - sudo apt-get install -y "php${TRAVIS_PHP_VERSION}-cli" "php${TRAVIS_PHP_VERSION}-gd" "php${TRAVIS_PHP_VERSION}-xml" "php${TRAVIS_PHP_VERSION}-mysql" - - export PATH="/usr/bin:${PATH}" - - php --rf imagewebp - install: - - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION - - composer update --prefer-source - script: - - vendor/phpunit/phpunit/phpunit -c phpunit.xml --verbose ./tests/test-timber-image-towebp.php - after_success: skip - -cache: - directories: - - "${HOME}/.composer/cache" - -before_install: - # phpenv config-add myconfig.ini --with-webp-dir - - phpenv config-rm xdebug.ini - -install: - - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION - - composer update $PREFER_LOWEST --prefer-source - -script: - - vendor/phpunit/phpunit/phpunit -c phpunit-nocover.xml --verbose - -notifications: - webhooks: - # https://docs.coveralls.io/parallel-build-webhook - - 'https://coveralls.io/webhook' diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh old mode 100644 new mode 100755 index b54cb1236..ee05775c8 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -13,17 +13,13 @@ WP_VERSION=${5-latest} SKIP_DB_CREATE=${6-false} TMPDIR=${TMPDIR-/tmp} +TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib} WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress} -# think 'untrailingslashit(), but in bash" -TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") -WP_TESTS_DIR=$(echo $WP_TESTS_DIR | sed -e "s/\/$//") -WP_CORE_DIR=$(echo $WP_CORE_DIR | sed -e "s/\/$//") - download() { if [ `which curl` ]; then - curl -sL "$1" > "$2"; + curl -s "$1" > "$2"; elif [ `which wget` ]; then wget -nv -O "$2" "$1" fi @@ -66,12 +62,12 @@ install_wp() { mkdir -p $WP_CORE_DIR if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then - mkdir -p $TMPDIR/wordpress-nightly - download https://wordpress.org/nightly-builds/wordpress-latest.zip $TMPDIR/wordpress-nightly/wordpress-nightly.zip - unzip -q $TMPDIR/wordpress-nightly/wordpress-nightly.zip -d $TMPDIR/wordpress-nightly/ - mv $TMPDIR/wordpress-nightly/wordpress/* $WP_CORE_DIR + mkdir -p $TMPDIR/wordpress-trunk + rm -rf $TMPDIR/wordpress-trunk/* + svn export --quiet https://core.svn.wordpress.org/trunk $TMPDIR/wordpress-trunk/wordpress + mv $TMPDIR/wordpress-trunk/wordpress/* $WP_CORE_DIR else - if [[ $WP_VERSION == 'latest' ]]; then + if [ $WP_VERSION == 'latest' ]; then local ARCHIVE_NAME='latest' elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then # https serves multiple offers, whereas http serves single. @@ -111,8 +107,9 @@ install_test_suite() { if [ ! -d $WP_TESTS_DIR ]; then # set up testing suite mkdir -p $WP_TESTS_DIR - svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes - svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data + rm -rf $WP_TESTS_DIR/{includes,data} + svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes + svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data fi if [ ! -f wp-tests-config.php ]; then @@ -120,6 +117,7 @@ install_test_suite() { # remove all forward slashes in the end WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s:__DIR__ . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php @@ -128,6 +126,23 @@ install_test_suite() { } +recreate_db() { + shopt -s nocasematch + if [[ $1 =~ ^(y|yes)$ ]] + then + mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA + create_db + echo "Recreated the database ($DB_NAME)." + else + echo "Leaving the existing database ($DB_NAME) in place." + fi + shopt -u nocasematch +} + +create_db() { + mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA +} + install_db() { if [ ${SKIP_DB_CREATE} = "true" ]; then @@ -151,7 +166,14 @@ install_db() { fi # create database - mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA + if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ] + then + echo "Reinstalling will delete the existing test database ($DB_NAME)" + read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB + recreate_db $DELETE_EXISTING_DB + else + create_db + fi } install_wp diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6440da958..e19704ba4 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,30 +1,47 @@ = 8 ) { + echo "The scaffolded tests cannot currently be run on PHP 8.0+. See https://github.com/wp-cli/scaffold-command/issues/285" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + exit( 1 ); +} + $_tests_dir = getenv( 'WP_TESTS_DIR' ); if ( ! $_tests_dir ) { - $_tests_dir = rtrim( getenv( 'TMPDIR' ), '/' ) . '/wordpress-tests-lib'; + $_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; +} + +if ( ! file_exists( "{$_tests_dir}/includes/functions.php" ) ) { + echo "Could not find {$_tests_dir}/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + exit( 1 ); } -require_once $_tests_dir . '/includes/functions.php'; +// Give access to tests_add_filter() function. +require_once "{$_tests_dir}/includes/functions.php"; +/** + * Manually load the plugin being tested. + */ function _manually_load_plugin() { global $timber; require dirname( __FILE__ ) . '/../vendor/autoload.php'; $timber = new \Timber\Timber(); + require dirname( __FILE__ ) . '/../wp-content/plugins/advanced-custom-fields/acf.php'; - require dirname( __FILE__ ) . '/../wp-content/plugins/co-authors-plus/co-authors-plus.php'; + if ( file_exists( dirname( __FILE__ ) . '/../wp-content/plugins/co-authors-plus/co-authors-plus.php') ) { + include dirname( __FILE__ ) . '/../wp-content/plugins/co-authors-plus/co-authors-plus.php'; + } } tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); -require $_tests_dir . '/includes/bootstrap.php'; +// Start up the WP testing environment. +require "{$_tests_dir}/includes/bootstrap.php"; require_once __DIR__.'/Timber_UnitTestCase.php'; -require_once __DIR__.'/TimberImage_UnitTestCase.php'; - -error_log('Use http://build.starter-theme.dev/ for testing with UI'); +require_once __DIR__.'/TimberAttachment_UnitTestCase.php'; +require_once __DIR__.'/timber-mock-classes.php'; if ( !function_exists('is_post_type_viewable') ) { function is_post_type_viewable( $post_type_object ) {