From 87fd3508a997f7627e10854e2a63d600d9c94970 Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Thu, 8 Feb 2024 11:52:15 +0000 Subject: [PATCH 01/18] chore: adds integration test --- .env.dist | 7 ++ .gitignore | 3 + .husky/pre-commit | 4 +- bin/install-wp-tests.sh | 155 +++++++++++++++++++++++ composer.json | 79 +++++++----- composer.lock | 174 +++++++++++++++++++++----- docker-compose.yml | 30 +++++ docker/php/Dockerfile | 33 +++++ includes/Api/Flags.php | 7 +- jest.config.js | 1 + local | 8 ++ phpcs.xml | 1 + phpunit.xml | 3 + tests/bootstrap.php | 49 ++++++++ tests/integration/HelloTest.php | 13 ++ tests/integration/wp-tests-config.php | 70 +++++++++++ 16 files changed, 563 insertions(+), 74 deletions(-) create mode 100644 .env.dist create mode 100755 bin/install-wp-tests.sh create mode 100644 docker-compose.yml create mode 100644 docker/php/Dockerfile create mode 100755 local create mode 100644 tests/integration/HelloTest.php create mode 100644 tests/integration/wp-tests-config.php diff --git a/.env.dist b/.env.dist new file mode 100644 index 0000000..27ed36e --- /dev/null +++ b/.env.dist @@ -0,0 +1,7 @@ +# Basic Wordpress Data + +DB_HOST=mysql +DB_PORT=3306 +DB_PASSWORD=p@55w0rd +DB_NAME=wordpress +DB_USER=root diff --git a/.gitignore b/.gitignore index 61c9e30..6e7640d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ vendor yarn-error.log .phpunit.result.cache .DS_Store +testingdb +.vscode +.env \ No newline at end of file diff --git a/.husky/pre-commit b/.husky/pre-commit index 4985860..fdbbd24 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -2,5 +2,5 @@ . "$(dirname -- "$0")/_/husky.sh" yarn lint:js && yarn test:js -composer run lint -composer run test +composer lint +composer test:unit diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh new file mode 100755 index 0000000..26836f7 --- /dev/null +++ b/bin/install-wp-tests.sh @@ -0,0 +1,155 @@ +#!/usr/bin/env bash + +if [ $# -lt 3 ]; then + echo "usage: $0 [db-host] [wp-version] [skip-database-creation]" + exit 1 +fi + +DB_NAME=$1 +DB_USER=$2 +DB_PASS=$3 +DB_HOST=${4-localhost} +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/} + +download() { + if [ `which curl` ]; then + curl -s "$1" > "$2"; + elif [ `which wget` ]; then + wget -nv -O "$2" "$1" + fi +} + +if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then + WP_BRANCH=${WP_VERSION%\-*} + WP_TESTS_TAG="branches/$WP_BRANCH" + +elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then + WP_TESTS_TAG="branches/$WP_VERSION" +elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then + if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then + # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x + WP_TESTS_TAG="tags/${WP_VERSION%??}" + else + WP_TESTS_TAG="tags/$WP_VERSION" + fi +elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then + WP_TESTS_TAG="trunk" +else + # http serves a single offer, whereas https serves multiple. we only want one + download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json + grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json + LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//') + if [[ -z "$LATEST_VERSION" ]]; then + echo "Latest WordPress version could not be found" + exit 1 + fi + WP_TESTS_TAG="tags/$LATEST_VERSION" +fi +set -ex + +install_wp() { + + if [ -d $WP_CORE_DIR ]; then + return; + fi + + 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 + else + 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. + download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json + if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then + # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x + LATEST_VERSION=${WP_VERSION%??} + else + # otherwise, scan the releases and get the most up to date minor version of the major release + local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'` + LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1) + fi + if [[ -z "$LATEST_VERSION" ]]; then + local ARCHIVE_NAME="wordpress-$WP_VERSION" + else + local ARCHIVE_NAME="wordpress-$LATEST_VERSION" + fi + else + local ARCHIVE_NAME="wordpress-$WP_VERSION" + fi + download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz + tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR + fi + + download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php +} + +install_test_suite() { + # portable in-place argument for both GNU sed and Mac OSX sed + if [[ $(uname -s) == 'Darwin' ]]; then + local ioption='-i.bak' + else + local ioption='-i' + fi + + # set up testing suite if it doesn't yet exist + if [ ! -d $WP_TESTS_DIR ]; then + # set up testing suite + mkdir -p $WP_TESTS_DIR + svn co --ignore-externals --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes + svn co --ignore-externals --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data + fi + + if [ ! -f wp-tests-config.php ]; then + download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php + # 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/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 + sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php + fi + +} + +install_db() { + + if [ ${SKIP_DB_CREATE} = "true" ]; then + return 0 + fi + + # parse DB_HOST for port or socket references + local PARTS=(${DB_HOST//\:/ }) + local DB_HOSTNAME=${PARTS[0]}; + local DB_SOCK_OR_PORT=${PARTS[1]}; + local EXTRA="" + + if ! [ -z $DB_HOSTNAME ] ; then + if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then + EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" + elif ! [ -z $DB_SOCK_OR_PORT ] ; then + EXTRA=" --socket=$DB_SOCK_OR_PORT" + elif ! [ -z $DB_HOSTNAME ] ; then + EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" + fi + fi + + # create database + mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA +} + +install_wp +install_test_suite +install_db diff --git a/composer.json b/composer.json index 087827e..73e5d80 100644 --- a/composer.json +++ b/composer.json @@ -1,37 +1,48 @@ { - "name": "mr/feature-flags", - "description": "Allows developers to enable / disable features based on flags.", - "type": "wordpress-plugin", - "license": "proprietary", - "require-dev": { - "phpunit/phpunit": "^9.4", - "brain/monkey": "^2.6", - "newsuk/nuk-wp-phpcs-config": "^0.1.0", - "newsuk/nuk-wp-phpstan-config": "^0.1.0", - "newsuk/nuk-wp-phpmd-config": "^0.1.0" - }, - "autoload": { - "psr-4": { - "MR\\FeatureFlags\\": ["includes/"] - } - }, - "autoload-dev": { - "psr-4": { - "MR\\FeatureFlags\\Tests\\": "tests/unit/" - } - }, + "name": "mr/feature-flags", + "description": "Allows developers to enable / disable features based on flags.", + "type": "wordpress-plugin", + "license": "proprietary", + "repositories": [ + { + "type": "vcs", + "url": "ssh://git@github.com/WordPress/wordpress-develop.git" + } + ], + "require-dev": { + "wordpress/wordpress": "^6.4", + "phpunit/phpunit": "^9.4", + "brain/monkey": "^2.6", + "newsuk/nuk-wp-phpcs-config": "^0.1.0", + "newsuk/nuk-wp-phpstan-config": "^0.1.0", + "newsuk/nuk-wp-phpmd-config": "^0.1.0", + "yoast/phpunit-polyfills": "^2.0" + }, + "autoload": { + "psr-4": { + "MR\\FeatureFlags\\": [ + "includes/" + ] + } + }, + "autoload-dev": { + "psr-4": { + "MR\\FeatureFlags\\Tests\\": "tests/unit/" + } + }, "config": { - "allow-plugins": { - "dealerdirect/phpcodesniffer-composer-installer": true, - "phpstan/extension-installer": true - } - }, - "scripts": { - "lint": "phpcs .", - "lint:fix": "phpcbf .", - "test": "phpunit --dont-report-useless-tests --configuration ./phpunit.xml --testdox", - "phpstan": "phpstan analyse --memory-limit=2048M", - "phpstan-baseline": "phpstan analyse -b --allow-empty-baseline --memory-limit=2048M", - "phpmd": "phpmd plugin.php,includes text phpmd.xml.dist --color" - } + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true, + "phpstan/extension-installer": true + } + }, + "scripts": { + "lint": "phpcs .", + "lint:fix": "phpcbf .", + "test:unit": "phpunit --dont-report-useless-tests --configuration ./phpunit.xml --testsuite unit --testdox", + "test:integration": "phpunit --dont-report-useless-tests --configuration ./phpunit.xml --testsuite integration --testdox", + "phpstan": "phpstan analyse --memory-limit=2048M", + "phpstan-baseline": "phpstan analyse -b --allow-empty-baseline --memory-limit=2048M", + "phpmd": "phpmd plugin.php,includes text phpmd.xml.dist --color" + } } diff --git a/composer.lock b/composer.lock index 77f8f56..6f0497b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,21 +4,21 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "10c64252fc2a1288e32dd26c50255f8b", + "content-hash": "043347b5f36d3162d628e8778d85b036", "packages": [], "packages-dev": [ { "name": "antecedent/patchwork", - "version": "2.1.27", + "version": "2.1.28", "source": { "type": "git", "url": "https://github.com/antecedent/patchwork.git", - "reference": "16a1ab81559aabf14acb616141e801b32777f085" + "reference": "6b30aff81ebadf0f2feb9268d3e08385cebcc08d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/antecedent/patchwork/zipball/16a1ab81559aabf14acb616141e801b32777f085", - "reference": "16a1ab81559aabf14acb616141e801b32777f085", + "url": "https://api.github.com/repos/antecedent/patchwork/zipball/6b30aff81ebadf0f2feb9268d3e08385cebcc08d", + "reference": "6b30aff81ebadf0f2feb9268d3e08385cebcc08d", "shasum": "" }, "require": { @@ -51,9 +51,9 @@ ], "support": { "issues": "https://github.com/antecedent/patchwork/issues", - "source": "https://github.com/antecedent/patchwork/tree/2.1.27" + "source": "https://github.com/antecedent/patchwork/tree/2.1.28" }, - "time": "2023-12-03T18:46:49+00:00" + "time": "2024-02-06T09:26:11+00:00" }, { "name": "automattic/vipwpcs", @@ -3873,16 +3873,16 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.28.0", + "version": "v1.29.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "ea208ce43cbb04af6867b4fdddb1bdbf84cc28cb" + "reference": "ef4d7e442ca910c4764bce785146269b30cb5fc4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/ea208ce43cbb04af6867b4fdddb1bdbf84cc28cb", - "reference": "ea208ce43cbb04af6867b4fdddb1bdbf84cc28cb", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/ef4d7e442ca910c4764bce785146269b30cb5fc4", + "reference": "ef4d7e442ca910c4764bce785146269b30cb5fc4", "shasum": "" }, "require": { @@ -3896,9 +3896,6 @@ }, "type": "library", "extra": { - "branch-alias": { - "dev-main": "1.28-dev" - }, "thanks": { "name": "symfony/polyfill", "url": "https://github.com/symfony/polyfill" @@ -3935,7 +3932,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.28.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.29.0" }, "funding": [ { @@ -3951,20 +3948,20 @@ "type": "tidelift" } ], - "time": "2023-01-26T09:26:14+00:00" + "time": "2024-01-29T20:11:03+00:00" }, { "name": "symfony/polyfill-mbstring", - "version": "v1.28.0", + "version": "v1.29.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "42292d99c55abe617799667f454222c54c60e229" + "reference": "9773676c8a1bb1f8d4340a62efe641cf76eda7ec" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/42292d99c55abe617799667f454222c54c60e229", - "reference": "42292d99c55abe617799667f454222c54c60e229", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/9773676c8a1bb1f8d4340a62efe641cf76eda7ec", + "reference": "9773676c8a1bb1f8d4340a62efe641cf76eda7ec", "shasum": "" }, "require": { @@ -3978,9 +3975,6 @@ }, "type": "library", "extra": { - "branch-alias": { - "dev-main": "1.28-dev" - }, "thanks": { "name": "symfony/polyfill", "url": "https://github.com/symfony/polyfill" @@ -4018,7 +4012,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.28.0" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.29.0" }, "funding": [ { @@ -4034,20 +4028,20 @@ "type": "tidelift" } ], - "time": "2023-07-28T09:04:16+00:00" + "time": "2024-01-29T20:11:03+00:00" }, { "name": "symfony/polyfill-php73", - "version": "v1.28.0", + "version": "v1.29.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php73.git", - "reference": "fe2f306d1d9d346a7fee353d0d5012e401e984b5" + "reference": "21bd091060673a1177ae842c0ef8fe30893114d2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/fe2f306d1d9d346a7fee353d0d5012e401e984b5", - "reference": "fe2f306d1d9d346a7fee353d0d5012e401e984b5", + "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/21bd091060673a1177ae842c0ef8fe30893114d2", + "reference": "21bd091060673a1177ae842c0ef8fe30893114d2", "shasum": "" }, "require": { @@ -4055,9 +4049,6 @@ }, "type": "library", "extra": { - "branch-alias": { - "dev-main": "1.28-dev" - }, "thanks": { "name": "symfony/polyfill", "url": "https://github.com/symfony/polyfill" @@ -4097,7 +4088,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php73/tree/v1.28.0" + "source": "https://github.com/symfony/polyfill-php73/tree/v1.29.0" }, "funding": [ { @@ -4113,7 +4104,7 @@ "type": "tidelift" } ], - "time": "2023-01-26T09:26:14+00:00" + "time": "2024-01-29T20:11:03+00:00" }, { "name": "symfony/service-contracts", @@ -4384,6 +4375,61 @@ ], "time": "2023-11-20T00:12:19+00:00" }, + { + "name": "wordpress/wordpress", + "version": "6.4.3", + "source": { + "type": "git", + "url": "ssh://git@github.com/WordPress/wordpress-develop.git", + "reference": "9e9559d6d6bd2327dee822c305b2905b65a91ef2" + }, + "require": { + "php": ">=7.0" + }, + "require-dev": { + "phpcompatibility/phpcompatibility-wp": "~2.1.3", + "squizlabs/php_codesniffer": "3.7.2", + "wp-coding-standards/wpcs": "~3.0.1", + "yoast/phpunit-polyfills": "^1.1.0" + }, + "suggest": { + "ext-dom": "*" + }, + "type": "library", + "scripts": { + "compat": [ + "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --standard=phpcompat.xml.dist --report=summary,source" + ], + "format": [ + "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --report=summary,source" + ], + "lint": [ + "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary,source" + ], + "lint:errors": [ + "@lint -n" + ], + "test": [ + "Composer\\Config::disableProcessTimeout", + "@php ./vendor/phpunit/phpunit/phpunit" + ] + }, + "license": [ + "GPL-2.0-or-later" + ], + "description": "WordPress is open source software you can use to create a beautiful website, blog, or app.", + "homepage": "https://wordpress.org", + "keywords": [ + "blog", + "cms", + "wordpress", + "wp" + ], + "support": { + "issues": "https://core.trac.wordpress.org/" + }, + "time": "2024-01-30T19:25:29+00:00" + }, { "name": "wp-coding-standards/wpcs", "version": "3.0.1", @@ -4449,6 +4495,66 @@ } ], "time": "2023-09-14T07:06:09+00:00" + }, + { + "name": "yoast/phpunit-polyfills", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/Yoast/PHPUnit-Polyfills.git", + "reference": "c758753e8f9dac251fed396a73c8305af3f17922" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Yoast/PHPUnit-Polyfills/zipball/c758753e8f9dac251fed396a73c8305af3f17922", + "reference": "c758753e8f9dac251fed396a73c8305af3f17922", + "shasum": "" + }, + "require": { + "php": ">=5.6", + "phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0" + }, + "require-dev": { + "yoast/yoastcs": "^2.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.x-dev" + } + }, + "autoload": { + "files": [ + "phpunitpolyfills-autoload.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Team Yoast", + "email": "support@yoast.com", + "homepage": "https://yoast.com" + }, + { + "name": "Contributors", + "homepage": "https://github.com/Yoast/PHPUnit-Polyfills/graphs/contributors" + } + ], + "description": "Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests", + "homepage": "https://github.com/Yoast/PHPUnit-Polyfills", + "keywords": [ + "phpunit", + "polyfill", + "testing" + ], + "support": { + "issues": "https://github.com/Yoast/PHPUnit-Polyfills/issues", + "source": "https://github.com/Yoast/PHPUnit-Polyfills" + }, + "time": "2023-06-06T20:28:24+00:00" } ], "aliases": [], diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..155b23a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,30 @@ +version: "3.7" + +services: + php: + build: + context: . + dockerfile: docker/php/Dockerfile + user: 1000:1000 + env_file: .env + environment: + WORDPRESS_DB_HOST: ${DB_HOST} + WORDPRESS_DB_PASSWORD: ${DB_PASSWORD} + WORDPRESS_DB_NAME: ${DB_NAME} + volumes: + - .:/usr/src + - ~/.composer:/var/cache/composer + links: + - mysql + depends_on: + - mysql + mysql: + image: mysql:8 + env_file: .env + environment: + MYSQL_ROOT_PASSWORD: ${DB_PASSWORD} + MYSQL_DATABASE: ${DB_NAME} + volumes: + - ./testingdb:/var/lib/mysql +volumes: + testingdb: diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile new file mode 100644 index 0000000..f62906c --- /dev/null +++ b/docker/php/Dockerfile @@ -0,0 +1,33 @@ +FROM php:8.3 + +# Container + PHP Setup +RUN pecl install xdebug +RUN docker-php-ext-install mysqli +RUN docker-php-ext-enable xdebug + +# Install Composer +RUN curl -s https://getcomposer.org/installer | php && \ + mv composer.phar /usr/local/bin/composer + +RUN mkdir /var/cache/composer +VOLUME /var/cache/composer +RUN chown -R www-data:www-data /var/cache/composer +ENV COMPOSER_HOME=/var/cache/composer + +# WP CLI Setup +ENV WORDPRESS_CLI_VERSION 2.9.0 + +RUN set -ex; \ + curl -o /usr/local/bin/wp -fSL "https://github.com/wp-cli/wp-cli/releases/download/v${WORDPRESS_CLI_VERSION}/wp-cli-${WORDPRESS_CLI_VERSION}.phar"; \ + curl -o /usr/local/bin/wp.sha512 -fSL "https://github.com/wp-cli/wp-cli/releases/download/v${WORDPRESS_CLI_VERSION}/wp-cli-${WORDPRESS_CLI_VERSION}.phar.sha512"; \ + \ + echo "$(cat /usr/local/bin/wp.sha512) /usr/local/bin/wp" | sha512sum -c -; \ + chmod +x /usr/local/bin/wp; \ + \ + wp --allow-root --version + +ENV WP_TESTS_DIR /usr/src/vendor/wordpress/wordpress/tests +ENV WP_TESTS_CONFIG_FILE_PATH /usr/src/tests/functional + +VOLUME /usr/src +WORKDIR /usr/src diff --git a/includes/Api/Flags.php b/includes/Api/Flags.php index 4f48f6f..b8ea6cf 100644 --- a/includes/Api/Flags.php +++ b/includes/Api/Flags.php @@ -71,7 +71,6 @@ function () { */ public function get_all_flags() { $flags = get_option( self::$option_name, [] ); - return rest_ensure_response( $flags ); } @@ -102,12 +101,12 @@ public function post_flags( WP_REST_Request $request ) { /** * Validates flag input from POST method. * - * @param WP_REST_Request $param Request object. + * @param WP_REST_Request $request Request object. * * @return bool */ - public function validate_flag_input( $param ) { - $input_data = $param->get_json_params(); + public function validate_flag_input( $request ) { + $input_data = $request->get_json_params(); $valid_keys = [ 'id', 'name', 'enabled' ]; if ( 0 === count( $input_data ) ) { diff --git a/jest.config.js b/jest.config.js index b9904ca..c981e6f 100644 --- a/jest.config.js +++ b/jest.config.js @@ -3,5 +3,6 @@ module.exports = { moduleNameMapper: { '@wordpress/(.*)$': '/node_modules/@wordpress/$1', }, + modulePathIgnorePatterns: ['/vendor/'], testEnvironment: 'jsdom', }; diff --git a/local b/local new file mode 100755 index 0000000..4ecfcde --- /dev/null +++ b/local @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +if [ ! -f ".env" ]; then + cp .env.dist .env +fi + +docker-compose -p plugin-testing up -d mysql +docker-compose -p plugin-testing run --entrypoint bash php diff --git a/phpcs.xml b/phpcs.xml index d9ac96e..395ec0b 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -8,4 +8,5 @@ + /vendor \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index c9abd89..860818c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -5,5 +5,8 @@ ./tests/Unit/ + + ./tests/integration/ + diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 2ac6fae..9f7e5f0 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,3 +9,52 @@ if ( file_exists( __DIR__ . '/../vendor/autoload.php' ) ) { include __DIR__ . '/../vendor/autoload.php'; } + +// Manually determine what test suites we're running to prevent running functional test scripts if not required. +$args = $_SERVER['argv']; + +$test_suites = []; +for ( $i = 0; $i < count($args); $i++ ) { + if ( preg_match( '/--testsuite=(.+)/', $args[$i], $matches ) ) { + $test_suites[] = $matches[1]; + } + if ( $args[$i] === '--testsuite' ) { + $test_suites[] = $args[$i + 1]; + $i++; + } +} + +if ( ! empty( $test_suites ) && ! in_array( 'integration', $test_suites ) ) { + return; +} + +$_tests_dir = getenv( 'WP_TESTS_DIR' ); + +if ( ! $_tests_dir ) { + $_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; +} + +if ( ! file_exists( $_tests_dir . '/phpunit/includes/functions.php' ) ) { + echo "Could not find $_tests_dir/phpunit/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + exit( 1 ); +} + +// Give access to tests_add_filter() function. +require_once $_tests_dir . '/phpunit/includes/functions.php'; + +/** + * Manually load the plugin being tested. + */ +function _manually_load_plugin() { + require __DIR__ . '/../plugin.php'; +} +tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); + +if ( $_tests_dir === '/usr/src/vendor/wordpress/wordpress/tests' ) { + define( 'WP_TESTS_CONFIG_FILE_PATH', __DIR__ . '/../tests/integration/wp-tests-config.php' ); +} + +define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', __DIR__ . '/vendor/yoast/phpunit-polyfills' ); + +// Start up the WP testing environment. +require $_tests_dir . '/phpunit/includes/bootstrap.php'; diff --git a/tests/integration/HelloTest.php b/tests/integration/HelloTest.php new file mode 100644 index 0000000..c476d75 --- /dev/null +++ b/tests/integration/HelloTest.php @@ -0,0 +1,13 @@ +assertTrue( post_type_exists( 'page' ) ); + } + +} diff --git a/tests/integration/wp-tests-config.php b/tests/integration/wp-tests-config.php new file mode 100644 index 0000000..428ee39 --- /dev/null +++ b/tests/integration/wp-tests-config.php @@ -0,0 +1,70 @@ + Date: Sat, 10 Feb 2024 05:59:24 +0000 Subject: [PATCH 02/18] fix(CPNT-2234): use yoast test utils in unit tests --- composer.json | 2 +- composer.lock | 85 +++++++++++++++++++++++++++++++++++---- phpstan.neon.dist | 4 +- tests/Unit/FlagTest.php | 22 +++++----- tests/Unit/FlagsTest.php | 36 +++++++---------- tests/Unit/HelperTest.php | 19 ++++----- 6 files changed, 112 insertions(+), 56 deletions(-) diff --git a/composer.json b/composer.json index 73e5d80..2045a7f 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "newsuk/nuk-wp-phpcs-config": "^0.1.0", "newsuk/nuk-wp-phpstan-config": "^0.1.0", "newsuk/nuk-wp-phpmd-config": "^0.1.0", - "yoast/phpunit-polyfills": "^2.0" + "yoast/wp-test-utils": "^1.2" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 6f0497b..f1865b7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "043347b5f36d3162d628e8778d85b036", + "content-hash": "9984fe8c649f8b4c071f5e1d9a741178", "packages": [], "packages-dev": [ { @@ -4498,21 +4498,21 @@ }, { "name": "yoast/phpunit-polyfills", - "version": "2.0.0", + "version": "1.1.0", "source": { "type": "git", "url": "https://github.com/Yoast/PHPUnit-Polyfills.git", - "reference": "c758753e8f9dac251fed396a73c8305af3f17922" + "reference": "224e4a1329c03d8bad520e3fc4ec980034a4b212" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Yoast/PHPUnit-Polyfills/zipball/c758753e8f9dac251fed396a73c8305af3f17922", - "reference": "c758753e8f9dac251fed396a73c8305af3f17922", + "url": "https://api.github.com/repos/Yoast/PHPUnit-Polyfills/zipball/224e4a1329c03d8bad520e3fc4ec980034a4b212", + "reference": "224e4a1329c03d8bad520e3fc4ec980034a4b212", "shasum": "" }, "require": { - "php": ">=5.6", - "phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0" + "php": ">=5.4", + "phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0" }, "require-dev": { "yoast/yoastcs": "^2.3.0" @@ -4554,7 +4554,76 @@ "issues": "https://github.com/Yoast/PHPUnit-Polyfills/issues", "source": "https://github.com/Yoast/PHPUnit-Polyfills" }, - "time": "2023-06-06T20:28:24+00:00" + "time": "2023-08-19T14:25:08+00:00" + }, + { + "name": "yoast/wp-test-utils", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/Yoast/wp-test-utils.git", + "reference": "2e0f62e0281e4859707c5f13b7da1422aa1c8f7b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Yoast/wp-test-utils/zipball/2e0f62e0281e4859707c5f13b7da1422aa1c8f7b", + "reference": "2e0f62e0281e4859707c5f13b7da1422aa1c8f7b", + "shasum": "" + }, + "require": { + "brain/monkey": "^2.6.1", + "php": ">=5.6", + "yoast/phpunit-polyfills": "^1.1.0" + }, + "require-dev": { + "yoast/yoastcs": "^2.3.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-develop": "1.x-dev", + "dev-main": "1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ], + "exclude-from-classmap": [ + "/src/WPIntegration/TestCase.php", + "/src/WPIntegration/TestCaseNoPolyfills.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Team Yoast", + "email": "support@yoast.com", + "homepage": "https://yoast.com" + }, + { + "name": "Contributors", + "homepage": "https://github.com/Yoast/wp-test-utils/graphs/contributors" + } + ], + "description": "PHPUnit cross-version compatibility layer for testing plugins and themes build for WordPress", + "homepage": "https://github.com/Yoast/wp-test-utils/", + "keywords": [ + "brainmonkey", + "integration-testing", + "phpunit", + "testing", + "unit-testing", + "wordpress" + ], + "support": { + "issues": "https://github.com/Yoast/wp-test-utils/issues", + "source": "https://github.com/Yoast/wp-test-utils" + }, + "time": "2023-09-27T10:25:08+00:00" } ], "aliases": [], diff --git a/phpstan.neon.dist b/phpstan.neon.dist index f4993c4..76aeced 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -5,4 +5,6 @@ parameters: level: max paths: - plugin.php - - includes/ \ No newline at end of file + - includes/ + excludePaths: + - tests/* \ No newline at end of file diff --git a/tests/Unit/FlagTest.php b/tests/Unit/FlagTest.php index e62ecbb..e8be5ee 100644 --- a/tests/Unit/FlagTest.php +++ b/tests/Unit/FlagTest.php @@ -1,20 +1,17 @@ 1, 'name'=>'Test','enabled'=>true]]; @@ -26,9 +23,8 @@ public function test_is_enabled_method_should_return_true_if_flag_name_present_a } public function test_is_enabled_method_should_return_false_if_no_flags_exist() { - $mock_option_value = ''; - \Brain\Monkey\Functions\when('get_option')->justReturn($mock_option_value); + \Brain\Monkey\Functions\when('get_option')->justReturn([]); $result = Flag::is_enabled('Test'); $this->assertFalse($result); @@ -43,7 +39,7 @@ public function test_is_enabled_method_should_return_false_if_flag_name_present_ $this->assertFalse($result); } - public function test_is_enabled_method_should_return_false_if_flag_name_nor_present() { + public function test_is_enabled_method_should_return_false_if_flag_name_not_exist() { $mock_option_value = [['id'=>1, 'name'=>'Test','enabled'=>false]]; \Brain\Monkey\Functions\when('get_option')->justReturn($mock_option_value); diff --git a/tests/Unit/FlagsTest.php b/tests/Unit/FlagsTest.php index c981626..696d686 100644 --- a/tests/Unit/FlagsTest.php +++ b/tests/Unit/FlagsTest.php @@ -1,21 +1,18 @@ 1, 'name'=>'Test','enabled'=>true]]; @@ -29,12 +26,8 @@ public function test_get_all_flags_method_should_return_all_flags_from_options_t } public function test_get_all_flags_method_should_return_empty_array_if_value_is_not_set() { - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - $mock_option_value = ''; - - \Brain\Monkey\Functions\when('get_option')->justReturn($mock_option_value); + + \Brain\Monkey\Functions\when('get_option')->justReturn([]); \Brain\Monkey\Functions\when('rest_ensure_response')->returnArg(); $flags = new Flags(); @@ -54,19 +47,18 @@ public function test_get_all_flags_method_should_return_multiple_flags_from_opti } public function test_post_flags_methods_should_return_success_if_input_is_array() { - - $this->markTestIncomplete( + $this->markTestIncomplete( 'This test has not been implemented yet.' ); $request_mock = \Mockery::mock('WP_Request'); - $request_mock->shouldReceive('get_json_params')->andReturn(['param1' => 'value1']); + $request_mock->shouldReceive('get_json_params')->andReturn(['param1' => 'value1']); \Brain\Monkey\Functions\when('update_option')->justReturn(true); \Brain\Monkey\Functions\when('rest_ensure_response')->returnArg(); - global $wp; - $wp = new \stdClass(); - $wp->request = $request_mock; + global $wp; + $wp = new \stdClass(); + $wp->request = $request_mock; $flags = new Flags(); $result = $flags->post_flags($request_mock); diff --git a/tests/Unit/HelperTest.php b/tests/Unit/HelperTest.php index 0978f77..d5a7a6e 100644 --- a/tests/Unit/HelperTest.php +++ b/tests/Unit/HelperTest.php @@ -1,19 +1,16 @@ Date: Sat, 10 Feb 2024 06:03:37 +0000 Subject: [PATCH 03/18] remove unused setup, teardown methods --- tests/Unit/FlagTest.php | 7 ------- tests/Unit/FlagsTest.php | 8 -------- tests/Unit/HelperTest.php | 7 ------- 3 files changed, 22 deletions(-) diff --git a/tests/Unit/FlagTest.php b/tests/Unit/FlagTest.php index e8be5ee..eb36615 100644 --- a/tests/Unit/FlagTest.php +++ b/tests/Unit/FlagTest.php @@ -5,13 +5,6 @@ class FlagTest extends TestCase { - protected function set_up() { - parent::set_up(); - } - - protected function tear_down() { - parent::tear_down(); - } public function test_is_enabled_method_should_return_true_if_flag_name_present_and_enabled() { $mock_option_value = [['id'=>1, 'name'=>'Test','enabled'=>true]]; diff --git a/tests/Unit/FlagsTest.php b/tests/Unit/FlagsTest.php index 696d686..d9c663b 100644 --- a/tests/Unit/FlagsTest.php +++ b/tests/Unit/FlagsTest.php @@ -6,14 +6,6 @@ class FlagsTest extends TestCase { - protected function set_up() { - parent::set_up(); - } - - protected function tear_down() { - parent::tear_down(); - } - public function test_get_all_flags_method_should_return_all_flags_from_options_table() { $mock_option_value = [['id'=>1, 'name'=>'Test','enabled'=>true]]; diff --git a/tests/Unit/HelperTest.php b/tests/Unit/HelperTest.php index d5a7a6e..d73b97a 100644 --- a/tests/Unit/HelperTest.php +++ b/tests/Unit/HelperTest.php @@ -4,13 +4,6 @@ use Yoast\WPTestUtils\BrainMonkey\TestCase; class HelperTest extends TestCase { - protected function set_up() { - parent::set_up(); - } - - protected function tear_down() { - parent::tear_down(); - } public function test_search_flag_method_should_return_true_if_flag_name_present_and_enabled() { $helper = new Helper(); From 59c01774c63379837863aebe24a4cbb8f7a139fe Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sat, 10 Feb 2024 07:16:25 +0000 Subject: [PATCH 04/18] adds yoast integration test setup --- composer.json | 3 +- docker/php/Dockerfile | 2 +- phpunit-integration-multisite.xml | 12 +++++++ phpunit-integration.xml | 9 +++++ phpunit.xml | 7 ++-- tests/Unit/bootstrap.php | 13 +++++++ tests/bootstrap.php | 60 ------------------------------- tests/integration/HelloTest.php | 5 ++- tests/integration/bootstrap.php | 34 ++++++++++++++++++ 9 files changed, 77 insertions(+), 68 deletions(-) create mode 100644 phpunit-integration-multisite.xml create mode 100644 phpunit-integration.xml create mode 100644 tests/Unit/bootstrap.php delete mode 100644 tests/bootstrap.php create mode 100644 tests/integration/bootstrap.php diff --git a/composer.json b/composer.json index 2045a7f..a8203dc 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,8 @@ "lint": "phpcs .", "lint:fix": "phpcbf .", "test:unit": "phpunit --dont-report-useless-tests --configuration ./phpunit.xml --testsuite unit --testdox", - "test:integration": "phpunit --dont-report-useless-tests --configuration ./phpunit.xml --testsuite integration --testdox", + "test:integration": "phpunit --dont-report-useless-tests --configuration ./phpunit-integration.xml --testsuite integration --testdox", + "test:multisite": "phpunit --dont-report-useless-tests --configuration ./phpunit-integration-multisite.xml --testsuite integration --testdox", "phpstan": "phpstan analyse --memory-limit=2048M", "phpstan-baseline": "phpstan analyse -b --allow-empty-baseline --memory-limit=2048M", "phpmd": "phpmd plugin.php,includes text phpmd.xml.dist --color" diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile index f62906c..98ceda1 100644 --- a/docker/php/Dockerfile +++ b/docker/php/Dockerfile @@ -26,7 +26,7 @@ RUN set -ex; \ \ wp --allow-root --version -ENV WP_TESTS_DIR /usr/src/vendor/wordpress/wordpress/tests +ENV WP_TESTS_DIR /usr/src/vendor/wordpress/wordpress/tests/phpunit ENV WP_TESTS_CONFIG_FILE_PATH /usr/src/tests/functional VOLUME /usr/src diff --git a/phpunit-integration-multisite.xml b/phpunit-integration-multisite.xml new file mode 100644 index 0000000..259f198 --- /dev/null +++ b/phpunit-integration-multisite.xml @@ -0,0 +1,12 @@ + + + + + + + + + ./tests/integration/ + + + diff --git a/phpunit-integration.xml b/phpunit-integration.xml new file mode 100644 index 0000000..c58c9c6 --- /dev/null +++ b/phpunit-integration.xml @@ -0,0 +1,9 @@ + + + + + + ./tests/integration/ + + + diff --git a/phpunit.xml b/phpunit.xml index 860818c..72cfc17 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,12 +1,9 @@ - + - ./tests/Unit/ + ./tests/unit/ - - ./tests/integration/ - diff --git a/tests/Unit/bootstrap.php b/tests/Unit/bootstrap.php new file mode 100644 index 0000000..733abc3 --- /dev/null +++ b/tests/Unit/bootstrap.php @@ -0,0 +1,13 @@ +assertTrue( post_type_exists( 'page' ) ); } diff --git a/tests/integration/bootstrap.php b/tests/integration/bootstrap.php new file mode 100644 index 0000000..28fa251 --- /dev/null +++ b/tests/integration/bootstrap.php @@ -0,0 +1,34 @@ + Date: Sat, 10 Feb 2024 07:40:30 +0000 Subject: [PATCH 05/18] adds phpunit coverage for unit test --- composer.json | 2 +- docker/php/Dockerfile | 9 ++++++--- phpunit.xml | 6 +++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index a8203dc..b508e56 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "scripts": { "lint": "phpcs .", "lint:fix": "phpcbf .", - "test:unit": "phpunit --dont-report-useless-tests --configuration ./phpunit.xml --testsuite unit --testdox", + "test:unit": "phpunit --dont-report-useless-tests --configuration ./phpunit.xml --testsuite unit --testdox --coverage-text", "test:integration": "phpunit --dont-report-useless-tests --configuration ./phpunit-integration.xml --testsuite integration --testdox", "test:multisite": "phpunit --dont-report-useless-tests --configuration ./phpunit-integration-multisite.xml --testsuite integration --testdox", "phpstan": "phpstan analyse --memory-limit=2048M", diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile index 98ceda1..da834bd 100644 --- a/docker/php/Dockerfile +++ b/docker/php/Dockerfile @@ -1,9 +1,12 @@ FROM php:8.3 # Container + PHP Setup -RUN pecl install xdebug -RUN docker-php-ext-install mysqli -RUN docker-php-ext-enable xdebug +ARG XDEBUG_INI="/usr/local/etc/php/conf.d/xdebug.ini" +# Install, enable and configure xdebug +RUN pecl install xdebug \ + && docker-php-ext-enable xdebug \ + && echo "[xdebug]" > $XDEBUG_INI \ + && echo "xdebug.mode = coverage" >> $XDEBUG_INI # Install Composer RUN curl -s https://getcomposer.org/installer | php && \ diff --git a/phpunit.xml b/phpunit.xml index 72cfc17..5cc10c3 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,6 +1,10 @@ - + + + ./includes + + ./tests/unit/ From 0b848e799813b03ba748ef6705835f4f5668e919 Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sat, 10 Feb 2024 15:36:28 +0000 Subject: [PATCH 06/18] adds phpunit coverage for unit test --- phpunit.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 5cc10c3..1b4e982 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,10 +1,10 @@ - - - ./includes - - + + + ./includes + + ./tests/unit/ From 0d5ce612b1e9b7765f700869f75b1f2bb7254864 Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sat, 10 Feb 2024 17:24:48 +0000 Subject: [PATCH 07/18] adds integration test for GET method --- composer.json | 2 +- docker/php/Dockerfile | 2 + includes/Api/Flags.php | 56 +++++++------ phpunit-integration.xml | 6 +- plugin.php | 2 +- tests/integration/FlagsApiTest.php | 121 +++++++++++++++++++++++++++++ tests/integration/HelloTest.php | 16 ---- 7 files changed, 161 insertions(+), 44 deletions(-) create mode 100644 tests/integration/FlagsApiTest.php delete mode 100644 tests/integration/HelloTest.php diff --git a/composer.json b/composer.json index b508e56..6f4cc70 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ "lint": "phpcs .", "lint:fix": "phpcbf .", "test:unit": "phpunit --dont-report-useless-tests --configuration ./phpunit.xml --testsuite unit --testdox --coverage-text", - "test:integration": "phpunit --dont-report-useless-tests --configuration ./phpunit-integration.xml --testsuite integration --testdox", + "test:integration": "phpunit --dont-report-useless-tests --configuration ./phpunit-integration.xml --testsuite integration --testdox --coverage-text", "test:multisite": "phpunit --dont-report-useless-tests --configuration ./phpunit-integration-multisite.xml --testsuite integration --testdox", "phpstan": "phpstan analyse --memory-limit=2048M", "phpstan-baseline": "phpstan analyse -b --allow-empty-baseline --memory-limit=2048M", diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile index da834bd..6c1a60c 100644 --- a/docker/php/Dockerfile +++ b/docker/php/Dockerfile @@ -8,6 +8,8 @@ RUN pecl install xdebug \ && echo "[xdebug]" > $XDEBUG_INI \ && echo "xdebug.mode = coverage" >> $XDEBUG_INI +RUN docker-php-ext-install mysqli + # Install Composer RUN curl -s https://getcomposer.org/installer | php && \ mv composer.phar /usr/local/bin/composer diff --git a/includes/Api/Flags.php b/includes/Api/Flags.php index b8ea6cf..6a35f6a 100644 --- a/includes/Api/Flags.php +++ b/includes/Api/Flags.php @@ -32,35 +32,41 @@ class Flags { /** * Register feature flag endpoints. * - * @return void * @since 1.0.0 */ - public function register_flags_endpoints() { + public function register(): void { add_action( 'rest_api_init', - function () { - register_rest_route( - 'feature-flags/v1', - 'flags', - [ - [ - 'methods' => WP_REST_Server::READABLE, - 'callback' => [ $this, 'get_all_flags' ], - 'permission_callback' => function () { - return current_user_can( 'manage_options' ); - }, - ], - [ - 'methods' => WP_REST_Server::EDITABLE, - 'callback' => [ $this, 'post_flags' ], - 'permission_callback' => function () { - return current_user_can( 'manage_options' ); - }, - 'validate_callback' => [ $this, 'validate_flag_input' ], - ], - ] - ); - } + [ $this, 'register_routes' ] + ); + } + + /** + * Register routes. + * + * * @since 1.0.0 + */ + public function register_routes(): void { + register_rest_route( + 'feature-flags/v1', + 'flags', + [ + [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => [ $this, 'get_all_flags' ], + 'permission_callback' => function () { + return current_user_can( 'manage_options' ); + }, + ], + [ + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => [ $this, 'post_flags' ], + 'permission_callback' => function () { + return current_user_can( 'manage_options' ); + }, + 'validate_callback' => [ $this, 'validate_flag_input' ], + ], + ] ); } diff --git a/phpunit-integration.xml b/phpunit-integration.xml index c58c9c6..ba53656 100644 --- a/phpunit-integration.xml +++ b/phpunit-integration.xml @@ -1,6 +1,10 @@ - + + + ./includes/Api + + ./tests/integration/ diff --git a/plugin.php b/plugin.php index 6d356d4..5724cfa 100644 --- a/plugin.php +++ b/plugin.php @@ -124,7 +124,7 @@ function mr_feature_flags_scripts_enqueue(): void { // Registers feature flags API's. $mr_feature_flags_register_api = new Flags(); -$mr_feature_flags_register_api->register_flags_endpoints(); +$mr_feature_flags_register_api->register(); // Displays setting page link in plugin page. diff --git a/tests/integration/FlagsApiTest.php b/tests/integration/FlagsApiTest.php new file mode 100644 index 0000000..b2bbd1e --- /dev/null +++ b/tests/integration/FlagsApiTest.php @@ -0,0 +1,121 @@ +user->create( + array( + 'role' => 'editor', + ) + ); + + self::$admin = $factory->user->create( + array( + 'role' => 'administrator', + ) + ); + } + + public static function wpTearDownAfterClass() { + self::delete_user( self::$editor ); + } + + public function set_up() { + parent::set_up(); + $this->instance = new Flags(); + $this->instance->register(); + } + + public function test_register() { + $this->assertSame( 10, has_action( 'rest_api_init', array( $this->instance, 'register_routes' ) ) ); + } + + + public function test_register_routes() { + $this->instance->register_routes(); + $routes = rest_get_server()->get_routes(); + $this->assertArrayHasKey( self::$api_endpoint, $routes ); + } + + public function test_get_items_no_permission() { + wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', self::$api_endpoint ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_forbidden', $response, 401 ); + + wp_set_current_user( self::$editor ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_forbidden', $response, 403 ); + } + + public function test_get_items_as_admin_returns_200() { + wp_set_current_user( self::$admin ); + $request = new WP_REST_Request( 'GET', self::$api_endpoint ); + $response = rest_get_server()->dispatch( $request ); + $this->assertSame( 200, $response->get_status() ); + $this->assertEquals([], $response->get_data()); + } + + public function test_get_items() { + wp_set_current_user( self::$admin ); + $flags = [['id'=>1, 'name'=>'test', 'enabled'=>true], ['id'=>2, 'name'=>'test2', 'enabled'=>false]]; + update_option( Flags::$option_name, $flags ); + + $request = new WP_REST_Request( 'GET', self::$api_endpoint ); + $response = rest_get_server()->dispatch( $request ); + $this->assertSame( 200, $response->get_status() ); + $this->assertSame($flags, $response->get_data()); + } + + /** + * @doesNotPerformAssertions + */ + public function test_create_item() {} + + /** + * @doesNotPerformAssertions + */ + public function test_context_param() {} + + /** + * @doesNotPerformAssertions + */ + public function test_get_item() {} + + /** + * @doesNotPerformAssertions + */ + public function test_update_item() {} + + /** + * @doesNotPerformAssertions + */ + public function test_prepare_item() {} + + /** + * @doesNotPerformAssertions + */ + public function test_get_item_schema() {} + + /** + * @doesNotPerformAssertions + */ + public function test_delete_item() {} +} diff --git a/tests/integration/HelloTest.php b/tests/integration/HelloTest.php deleted file mode 100644 index 1ea7698..0000000 --- a/tests/integration/HelloTest.php +++ /dev/null @@ -1,16 +0,0 @@ -assertTrue( post_type_exists( 'page' ) ); - } - -} From e1a270c4bf0005d91587e4e66a2675b1bc499433 Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sat, 10 Feb 2024 19:24:45 +0000 Subject: [PATCH 08/18] adds integration test for POST method --- composer.json | 2 +- includes/Api/Flags.php | 14 ++++--- tests/integration/FlagsApiTest.php | 62 +++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 6f4cc70..a6ac3c7 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "repositories": [ { "type": "vcs", - "url": "ssh://git@github.com/WordPress/wordpress-develop.git" + "url": "https://git@github.com/WordPress/wordpress-develop" } ], "require-dev": { diff --git a/includes/Api/Flags.php b/includes/Api/Flags.php index 6a35f6a..508f67f 100644 --- a/includes/Api/Flags.php +++ b/includes/Api/Flags.php @@ -89,10 +89,10 @@ public function get_all_flags() { * @phpstan-param WP_REST_Request $request */ public function post_flags( WP_REST_Request $request ) { - $flags = $request->get_json_params(); + $input_data = $request->get_json_params(); - if ( count( $flags ) > 0 ) { - update_option( self::$option_name, $flags ); + if ( is_array( $input_data['flags'] ) ) { + update_option( self::$option_name, $input_data['flags'] ); return rest_ensure_response( array( 'status' => 200, @@ -113,13 +113,17 @@ public function post_flags( WP_REST_Request $request ) { */ public function validate_flag_input( $request ) { $input_data = $request->get_json_params(); + + if ( ! isset( $input_data['flags'] ) || gettype( $input_data['flags'] ) !== 'array' ) { + return false; + } $valid_keys = [ 'id', 'name', 'enabled' ]; - if ( 0 === count( $input_data ) ) { + if ( 0 === count( $input_data['flags'] ) ) { return true; } - foreach ( $input_data as $flag ) { + foreach ( $input_data['flags'] as $flag ) { foreach ( $valid_keys as $value ) { if ( ! array_key_exists( $value, $flag ) ) { return false; diff --git a/tests/integration/FlagsApiTest.php b/tests/integration/FlagsApiTest.php index b2bbd1e..17f47ab 100644 --- a/tests/integration/FlagsApiTest.php +++ b/tests/integration/FlagsApiTest.php @@ -34,7 +34,8 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { } public static function wpTearDownAfterClass() { - self::delete_user( self::$editor ); + wp_delete_user( self::$editor ); + delete_option( Flags::$option_name ); } public function set_up() { @@ -84,10 +85,61 @@ public function test_get_items() { $this->assertSame($flags, $response->get_data()); } - /** - * @doesNotPerformAssertions - */ - public function test_create_item() {} + public function test_create_item() { + wp_set_current_user( self::$admin ); + $flags = [['id'=>1, 'name'=>'test', 'enabled'=>true], ['id'=>2, 'name'=>'test2', 'enabled'=>false]]; + + $request = new WP_REST_Request( 'POST', self::$api_endpoint ); + $request->add_header( 'Content-Type', 'application/json' ); + $request->set_body( wp_json_encode( ['flags' => $flags] ) ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status() ); + $this->assertTrue( $response->get_data()['success'] ); + + $options = get_option(Flags::$option_name); + $this->assertSame($options, $flags); + } + + public function test_create_item_with_empty_array() { + wp_set_current_user( self::$admin ); + $flags = []; + + $request = new WP_REST_Request( 'POST', self::$api_endpoint ); + $request->add_header( 'Content-Type', 'application/json' ); + $request->set_body( wp_json_encode( ['flags' => $flags] ) ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status() ); + $this->assertTrue( $response->get_data()['success'] ); + + $options = get_option(Flags::$option_name); + $this->assertSame($options, $flags); + } + + public function test_create_item_with_invalid_input_array() { + wp_set_current_user( self::$admin ); + $flags = [['id'=>1, 'name'=>'test', 'enabled'=>true]]; + + $request = new WP_REST_Request( 'POST', self::$api_endpoint ); + $request->add_header( 'Content-Type', 'application/json' ); + $request->set_body( wp_json_encode( ['invalid' => $flags] ) ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_invalid_params', $response, 400 ); + + } + + public function test_create_item_without_input() { + wp_set_current_user( self::$admin ); + + $request = new WP_REST_Request( 'POST', self::$api_endpoint ); + $request->add_header( 'Content-Type', 'application/json' ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_invalid_params', $response, 400 ); + + } /** * @doesNotPerformAssertions From e551f83e531148551a308f91bf08ed696750aaa2 Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sun, 11 Feb 2024 14:09:49 +0000 Subject: [PATCH 09/18] adds integration tests for validate callback --- includes/Api/Flags.php | 20 ++++++++++++++++---- tests/integration/FlagsApiTest.php | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/includes/Api/Flags.php b/includes/Api/Flags.php index 508f67f..1fd5438 100644 --- a/includes/Api/Flags.php +++ b/includes/Api/Flags.php @@ -119,17 +119,29 @@ public function validate_flag_input( $request ) { } $valid_keys = [ 'id', 'name', 'enabled' ]; + // handle delete all feature flags. if ( 0 === count( $input_data['flags'] ) ) { return true; } foreach ( $input_data['flags'] as $flag ) { - foreach ( $valid_keys as $value ) { - if ( ! array_key_exists( $value, $flag ) ) { - return false; - } + // validate if the input contains allowed values. + if ( count( array_diff( $valid_keys, array_keys( $flag ) ) ) > 0 ) { + return false; + } + + foreach ( $valid_keys as $key ) { + $value = isset( $flag[ $key ] ) ? $flag[ $key ] : null; + + match ( $key ) { + 'id' => is_int( $value ), + 'name' => is_string( $value ), + 'enabled' => is_bool( $value ), + default => false, + }; } } + return true; } } diff --git a/tests/integration/FlagsApiTest.php b/tests/integration/FlagsApiTest.php index 17f47ab..92154b1 100644 --- a/tests/integration/FlagsApiTest.php +++ b/tests/integration/FlagsApiTest.php @@ -85,6 +85,29 @@ public function test_get_items() { $this->assertSame($flags, $response->get_data()); } + public function flagsDataProvider() { + return [ + 'invalid input' => [['invalid' => []], false], + 'valid empty input' => [['flags' => []], true], + 'valid input' => [['flags' => [['id'=>1, 'name'=>'test', 'enabled'=>true], ['id'=>2, 'name'=>'test2', 'enabled'=>false]]], true], + ]; + } + + /** + * @dataProvider flagsDataProvider + */ + public function testValidateFlags($inputData, $expectedResult) { + wp_set_current_user(self::$admin); + + $request = new WP_REST_Request('POST', self::$api_endpoint); + $request->add_header('Content-Type', 'application/json'); + $request->set_body(wp_json_encode($inputData)); + + $result = $this->instance->validate_flag_input($request); + + $this->assertSame($expectedResult, $result); + } + public function test_create_item() { wp_set_current_user( self::$admin ); $flags = [['id'=>1, 'name'=>'test', 'enabled'=>true], ['id'=>2, 'name'=>'test2', 'enabled'=>false]]; From 6376560040238f88ec0f8bf522109b573a66d14b Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sun, 11 Feb 2024 16:17:06 +0000 Subject: [PATCH 10/18] composer update --- composer.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.lock b/composer.lock index f1865b7..8aa3dc8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9984fe8c649f8b4c071f5e1d9a741178", + "content-hash": "1afbaeb104a5222a664f1628b3f4668e", "packages": [], "packages-dev": [ { @@ -4380,7 +4380,7 @@ "version": "6.4.3", "source": { "type": "git", - "url": "ssh://git@github.com/WordPress/wordpress-develop.git", + "url": "https://git@github.com/WordPress/wordpress-develop", "reference": "9e9559d6d6bd2327dee822c305b2905b65a91ef2" }, "require": { From 66043fbe209219dfca93a09656e81b5d46824f5b Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sun, 11 Feb 2024 16:21:05 +0000 Subject: [PATCH 11/18] update composer test command --- .github/workflows/php.yml | 51 ++++++++++++++++++++------------------- composer.json | 4 +-- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index bc952ac..2048a64 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -2,34 +2,35 @@ name: PHP on: [push] jobs: - lint-test: - name: ${{ matrix.php-versions }} Lint & Test - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - php-versions: ['8.3'] + lint-test: + name: ${{ matrix.php-versions }} Lint & Test + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php-versions: ['8.3'] - steps: - - name: Checkout - uses: actions/checkout@v3 + steps: + - name: Checkout + uses: actions/checkout@v3 - - name: Cache Composer dependencies - uses: actions/cache@v3 - with: - path: /tmp/composer-cache - key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} + - name: Cache Composer dependencies + uses: actions/cache@v3 + with: + path: /tmp/composer-cache + key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} - - name: Setup PHP - uses: php-actions/composer@v6 - with: - php_version: ${{ matrix.php-versions }} + - name: Setup PHP + uses: php-actions/composer@v6 + with: + php_version: ${{ matrix.php-versions }} + command: composer update - - name: Validate Composer - run: composer validate --strict + - name: Validate Composer + run: composer validate --strict - - name: PHP Lint - run: composer run lint + - name: PHP Lint + run: composer run lint - - name: PHP test - run: composer run test + - name: PHP unit test + run: composer run test:unit diff --git a/composer.json b/composer.json index a6ac3c7..f4925df 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require-dev": { - "wordpress/wordpress": "^6.4", + "wordpress/wordpress": "^6.4", "phpunit/phpunit": "^9.4", "brain/monkey": "^2.6", "newsuk/nuk-wp-phpcs-config": "^0.1.0", @@ -41,7 +41,7 @@ "lint:fix": "phpcbf .", "test:unit": "phpunit --dont-report-useless-tests --configuration ./phpunit.xml --testsuite unit --testdox --coverage-text", "test:integration": "phpunit --dont-report-useless-tests --configuration ./phpunit-integration.xml --testsuite integration --testdox --coverage-text", - "test:multisite": "phpunit --dont-report-useless-tests --configuration ./phpunit-integration-multisite.xml --testsuite integration --testdox", + "test:multisite": "phpunit --dont-report-useless-tests --configuration ./phpunit-integration-multisite.xml --testsuite integration --testdox --coverage-text", "phpstan": "phpstan analyse --memory-limit=2048M", "phpstan-baseline": "phpstan analyse -b --allow-empty-baseline --memory-limit=2048M", "phpmd": "phpmd plugin.php,includes text phpmd.xml.dist --color" From 3c87b86ce166f165498396f847195c5b3969d027 Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sun, 11 Feb 2024 16:25:11 +0000 Subject: [PATCH 12/18] update composer test command --- .github/workflows/php.yml | 52 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 2048a64..825240a 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -2,35 +2,35 @@ name: PHP on: [push] jobs: - lint-test: - name: ${{ matrix.php-versions }} Lint & Test - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - php-versions: ['8.3'] + lint-test: + name: ${{ matrix.php-versions }} Lint & Test + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php-versions: ['8.3'] - steps: - - name: Checkout - uses: actions/checkout@v3 + steps: + - name: Checkout + uses: actions/checkout@v3 - - name: Cache Composer dependencies - uses: actions/cache@v3 - with: - path: /tmp/composer-cache - key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} + - name: Cache Composer dependencies + uses: actions/cache@v3 + with: + path: /tmp/composer-cache + key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} - - name: Setup PHP - uses: php-actions/composer@v6 - with: - php_version: ${{ matrix.php-versions }} - command: composer update + - name: Setup PHP + uses: php-actions/composer@v6 + with: + php_version: ${{ matrix.php-versions }} + command: composer update - - name: Validate Composer - run: composer validate --strict + - name: Validate Composer + run: composer validate --strict - - name: PHP Lint - run: composer run lint + - name: PHP Lint + run: composer run lint - - name: PHP unit test - run: composer run test:unit + - name: PHP test + run: composer run test:unit From a6c5ab7f61eb3d9e51d1f1c7ca1b4d53f0d9ce1b Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sun, 11 Feb 2024 16:27:05 +0000 Subject: [PATCH 13/18] update composer test command --- .github/workflows/php.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 825240a..97acf96 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -24,13 +24,13 @@ jobs: uses: php-actions/composer@v6 with: php_version: ${{ matrix.php-versions }} - command: composer update + command: update - name: Validate Composer run: composer validate --strict - name: PHP Lint - run: composer run lint + run: composer lint - name: PHP test - run: composer run test:unit + run: composer test:unit From f82b24a450cc176f9568b321d0542526caf7a25e Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sun, 11 Feb 2024 16:36:07 +0000 Subject: [PATCH 14/18] set working dir --- .github/workflows/php.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 97acf96..97e2413 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -34,3 +34,4 @@ jobs: - name: PHP test run: composer test:unit + working-directory: ${{ github.workspace }} From 9fcbe76f9096bc57362c5c59bc2bc384df0ed8db Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sun, 11 Feb 2024 16:45:59 +0000 Subject: [PATCH 15/18] set working dir --- phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 1b4e982..dcb34e9 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ - + ./includes From 0f52720296bc563c0312b84ce05af7f07c8f8143 Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sun, 11 Feb 2024 16:48:31 +0000 Subject: [PATCH 16/18] set working dir --- .github/workflows/php.yml | 3 +-- phpunit.xml | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 97e2413..75df221 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -33,5 +33,4 @@ jobs: run: composer lint - name: PHP test - run: composer test:unit - working-directory: ${{ github.workspace }} + run: composer test:unit \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index dcb34e9..1b4e982 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ - + ./includes From 951a121a307d5f271edf69db40b176800a504bdb Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sun, 11 Feb 2024 16:51:30 +0000 Subject: [PATCH 17/18] rename unit folder --- phpunit.xml | 4 ++-- tests/{Unit => units}/FlagTest.php | 0 tests/{Unit => units}/FlagsTest.php | 0 tests/{Unit => units}/HelperTest.php | 0 tests/{Unit => units}/bootstrap.php | 0 5 files changed, 2 insertions(+), 2 deletions(-) rename tests/{Unit => units}/FlagTest.php (100%) rename tests/{Unit => units}/FlagsTest.php (100%) rename tests/{Unit => units}/HelperTest.php (100%) rename tests/{Unit => units}/bootstrap.php (100%) diff --git a/phpunit.xml b/phpunit.xml index 1b4e982..09777e5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ - + ./includes @@ -7,7 +7,7 @@ - ./tests/unit/ + ./tests/units/ diff --git a/tests/Unit/FlagTest.php b/tests/units/FlagTest.php similarity index 100% rename from tests/Unit/FlagTest.php rename to tests/units/FlagTest.php diff --git a/tests/Unit/FlagsTest.php b/tests/units/FlagsTest.php similarity index 100% rename from tests/Unit/FlagsTest.php rename to tests/units/FlagsTest.php diff --git a/tests/Unit/HelperTest.php b/tests/units/HelperTest.php similarity index 100% rename from tests/Unit/HelperTest.php rename to tests/units/HelperTest.php diff --git a/tests/Unit/bootstrap.php b/tests/units/bootstrap.php similarity index 100% rename from tests/Unit/bootstrap.php rename to tests/units/bootstrap.php From 1cc8322b525e29e10cca81914a223be133f2f26b Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Sun, 11 Feb 2024 16:56:28 +0000 Subject: [PATCH 18/18] remove unused tests --- tests/units/FlagsTest.php | 48 +-------------------------------------- 1 file changed, 1 insertion(+), 47 deletions(-) diff --git a/tests/units/FlagsTest.php b/tests/units/FlagsTest.php index d9c663b..ec43a99 100644 --- a/tests/units/FlagsTest.php +++ b/tests/units/FlagsTest.php @@ -37,51 +37,5 @@ public function test_get_all_flags_method_should_return_multiple_flags_from_opti $result = $flags->get_all_flags(); $this->assertEquals($result, $mock_option_value); } - - public function test_post_flags_methods_should_return_success_if_input_is_array() { - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - $request_mock = \Mockery::mock('WP_Request'); - $request_mock->shouldReceive('get_json_params')->andReturn(['param1' => 'value1']); - - \Brain\Monkey\Functions\when('update_option')->justReturn(true); - \Brain\Monkey\Functions\when('rest_ensure_response')->returnArg(); - - global $wp; - $wp = new \stdClass(); - $wp->request = $request_mock; - - $flags = new Flags(); - $result = $flags->post_flags($request_mock); - - $this->assertEquals(['status'=>200, 'success' => true], $result); - - unset($GLOBALS['wp']); - } - - public function test_post_flags_methods_should_throw_error_if_input_is_not_an_array() { - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - $request_mock = \Mockery::mock('WP_Request'); - $request_mock->shouldReceive('get_json_params')->andReturn('test'); - - global $wp; - $wp = new \stdClass(); - $wp->request = $request_mock; - - $error_mock = \Mockery::mock('WP_Error'); - - \Brain\Monkey\Functions\expect('post_flags')->andReturn($error_mock); - - - $flags = new Flags(); - $result = $flags->post_flags($request_mock); - - $this->assertInstanceOf('WP_Error', $result); - - } - - + }