diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..dae90b1a8 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.env +.github_changelog_generator +.travis.yml \ No newline at end of file diff --git a/.env.dist b/.env.dist index 73054db02..db45227c9 100644 --- a/.env.dist +++ b/.env.dist @@ -1,19 +1,27 @@ -TEST_DB_NAME="wpgraphql_woocommerce_test" -TEST_DB_HOST="127.0.0.1" -TEST_DB_USER="root" -TEST_DB_PASSWORD="" -WP_VERSION=latest +DB_NAME=wordpress +DB_HOST=127.0.0.1 +DB_USER=wordpress +DB_PASSWORD=wordpress +WP_TABLE_PREFIX=wp_ +WP_URL=http://localhost +WP_DOMAIN=localhost +ADMIN_EMAIL=admin@example.com +ADMIN_USERNAME=admin +ADMIN_PASSWORD=password +ADMIN_PATH=/wp-admin + +TEST_DB_NAME=woographql_tests +TEST_DB_HOST=127.0.0.1 +TEST_DB_USER=wordpress +TEST_DB_PASSWORD=wordpress +TEST_WP_TABLE_PREFIX=wp_ + SKIP_DB_CREATE=false -WP_ROOT_FOLDER="/tmp/wordpress" -TEST_SITE_WP_ADMIN_PATH="/wp-admin" -TEST_SITE_DB_NAME="wpgraphql_woocommerce_test" -TEST_SITE_DB_HOST="127.0.0.1" -TEST_SITE_DB_USER="root" -TEST_SITE_DB_PASSWORD="" -TEST_SITE_TABLE_PREFIX="wp_" -TEST_TABLE_PREFIX="wp_" -TEST_SITE_WP_URL="http://localhost" -TEST_SITE_WP_DOMAIN="localhost" -TEST_SITE_ADMIN_EMAIL="admin@localhost" -TEST_SITE_ADMIN_USERNAME="admin" -TEST_SITE_ADMIN_PASSWORD="password" \ No newline at end of file +TEST_WP_ROOT_FOLDER=/tmp/wordpress +TEST_ADMIN_EMAIL=admin@wp.test + +TESTS_DIR=tests +TESTS_OUTPUT=tests/_output +TESTS_DATA=tests/_data +TESTS_SUPPORT=tests/_support +TESTS_ENVS=tests/_envs \ No newline at end of file diff --git a/.gitignore b/.gitignore index a65b338c0..b6f77baa3 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ node_modules/ *.tar.gz *.zip .env +.env.* +!.env.dist .idea .vscode .github_changelog_generator @@ -17,11 +19,13 @@ vendor/* !vendor/composer vendor/composer/installed.json vendor/composer/*/ -!/tests -/tests/*.suite.yml +!tests +tests/*.suite.yml build/ coverage/* schema.graphql phpunit.xml docker-output composer.lock +c3.php +.log/ diff --git a/.travis.yml b/.travis.yml index 56ee88402..95893060e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,9 @@ dist: trusty language: php +services: + - docker + notifications: email: on_success: never @@ -11,9 +14,10 @@ notifications: branches: only: - develop - - release-v0.1.1 + - master - release-v0.1.2 - release-v0.2.0 + - release-v0.2.1 cache: apt: true @@ -21,29 +25,41 @@ cache: - vendor - $HOME/.composer/cache -# Set the global environment variables -env: - global: - - WP_CORE_DIR: /tmp/wordpress - - COVERAGE: false - - DEBUG: false - matrix: include: + - php: 7.3 + env: PHP_VERSION=7.3 WP_VERSION=5.2 COVERAGE=1 + - php: 7.3 + env: PHP_VERSION=7.3 PHPCS=1 - php: 7.2 - env: WP_VERSION=latest COVERAGE=1 + env: PHP_VERSION=7.2 WP_VERSION=5.2 - php: 7.2 - env: PHPCS=1 + env: PHP_VERSION=7.2 WP_VERSION=4.9 + - php: 7.1 + env: PHP_VERSION=7.1 WP_VERSION=5.2 - php: 7.1 - env: WP_VERSION=latest + env: PHP_VERSION=7.1 WP_VERSION=4.9 - php: 7.0 - env: WP_VERSION=latest - - php: 5.6 - env: WP_VERSION=4.5 + env: PHP_VERSION=7.0 WP_VERSION=5.0 + - php: 7.0 + env: PHP_VERSION=7.0 WP_VERSION=4.9 - php: 5.6 - env: WP_VERSION=latest DEBUG=1 + env: PHP_VERSION=5.6 WP_VERSION=5.0 DEBUG=1 - php: 5.6 - env: WP_VERSION=trunk + env: PHP_VERSION=5.6 WP_VERSION=4.9 DEBUG=1 + +before_install: + - sudo rm /usr/local/bin/docker-compose + - curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` > docker-compose + - chmod +x docker-compose + - sudo mv docker-compose /usr/local/bin + - | + # Remove Xdebug for a huge performance increase: + if [ -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ]; then + phpenv config-rm xdebug.ini + else + echo "xdebug.ini does not exist" + fi install: - | @@ -57,19 +73,24 @@ before_script: if [ ! -z "$WP_VERSION" ]; then # Install and config Codeception cp .env.dist .env - composer install-wp-tests COMPOSER_MEMORY_LIMIT=-1 travis_retry composer install --prefer-source --no-interaction if [ "$COVERAGE" == "1" ]; then # Install Coveralls mkdir -p build/logs COMPOSER_MEMORY_LIMIT=-1 travis_retry composer require php-coveralls/php-coveralls fi - ls -al + docker-compose build \ + --build-arg DESIRED_PHP_VERSION=${PHP_VERSION} \ + --build-arg DESIRED_WP_VERSION=${WP_VERSION} \ + testing fi # Install PHP CodeSniffer and WPCS. - | if [ "$PHPCS" == "1" ]; then - COMPOSER_MEMORY_LIMIT=-1 travis_retry composer require squizlabs/php_codesniffer phpcompatibility/phpcompatibility-wp wp-coding-standards/wpcs dealerdirect/phpcodesniffer-composer-installer + COMPOSER_MEMORY_LIMIT=-1 travis_retry composer require \ + squizlabs/php_codesniffer \ + phpcompatibility/phpcompatibility-wp wp-coding-standards/wpcs \ + dealerdirect/phpcodesniffer-composer-installer COMPOSER_MEMORY_LIMIT=-1 travis_retry composer install --no-dev ls -al fi @@ -78,22 +99,25 @@ script: # Execute unit tests with coverage if specified, otherwise without coverage - | if [ ! -z "$WP_VERSION" ]; then - if [ "$COVERAGE" == "1" ]; then - vendor/bin/codecept run wpunit --coverage --coverage-xml - elif [ "$DEBUG" == "1" ]; then - vendor/bin/codecept run wpunit --debug - else - vendor/bin/codecept run wpunit - fi + declare -a suites=( 'acceptance', 'functional', 'wpunit' ) + for i in "${suites[@]}"; do + docker-compose run \ + -e SUITE=${i} -e COVERAGE=${COVERAGE:-0} -e DEBUG=${DEBUG:-0} testing \ + --rm testing --scale app=0 + done fi - | if [ "$PHPCS" == "1" ]; then - vendor/bin/phpcs wp-graphql-woocommerce.php access-functions.php class-inflect.php includes/*.php --standard=WordPress + vendor/bin/phpcs \ + wp-graphql-woocommerce.php \ + access-functions.php \ + class-inflect.php \ + includes/*.php --standard=WordPress fi after_success: # Runs Coveralls.io client - | if [ "$COVERAGE" == "1" ]; then - travis_retry php vendor/bin/php-coveralls -v + travis_retry vendor/bin/php-coveralls -v fi diff --git a/Dockerfile b/Dockerfile index 9472ac3d8..e96ee2a4b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,46 @@ -FROM ryanshoover/wp-browser +############################################################################ +# Container for running Codeception tests on a WooGraphQL Docker instance. # +############################################################################ -RUN a2enmod rewrite && \ -service apache2 restart \ No newline at end of file +# Using the 'DESIRED_' prefix to avoid confusion with environment variables of the same name. +ARG DESIRED_WP_VERSION +ARG DESIRED_PHP_VERSION + +FROM kidunot89/woographql-app:wp${DESIRED_WP_VERSION}-php${DESIRED_PHP_VERSION} + + +LABEL author=kidunot89 +LABEL author_uri=https://github.com/kidunot89 + +SHELL [ "/bin/bash", "-c" ] + +ARG DESIRED_WP_VERSION +ARG DESIRED_PHP_VERSION + +# Install php extensions +RUN docker-php-ext-install pdo_mysql + +# Install Xdebug +RUN if [ "$DESIRED_PHP_VERSION" == "5.6" ]; then yes | pecl install xdebug-2.5.5; else yes | pecl install xdebug; fi \ + && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \ + && echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \ + && echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini + +# Install composer +ENV COMPOSER_ALLOW_SUPERUSER=1 + +RUN curl -sS https://getcomposer.org/installer | php -- \ + --filename=composer \ + --install-dir=/usr/local/bin + +# Add composer global binaries to PATH +ENV PATH "$PATH:~/.composer/vendor/bin" + +# Remove exec statement from base entrypoint script. +RUN sed -i '$d' /usr/local/bin/app-entrypoint.sh + +# Set up entrypoint +WORKDIR /var/www/html +COPY bin/testing-entrypoint.sh /usr/local/bin/testing-entrypoint.sh +RUN chmod 755 /usr/local/bin/testing-entrypoint.sh +ENTRYPOINT ["testing-entrypoint.sh"] \ No newline at end of file diff --git a/README.md b/README.md index a99259742..5139fe555 100644 --- a/README.md +++ b/README.md @@ -46,12 +46,9 @@ Until the documentation is in full effect, it's recommended that a [GraphiQL](ht TEST_DB_PASSWORD="" # Install script - WP_VERSION=latest SKIP_DB_CREATE=false - WP_GRAPHQL_BRANCH=develop # Codeception - WP_ROOT_FOLDER="/tmp/wordpress" TEST_SITE_WP_ADMIN_PATH="/wp-admin" TEST_SITE_DB_NAME="wpgraphql_woocommerce_test" TEST_SITE_DB_HOST="127.0.0.1" @@ -59,9 +56,9 @@ Until the documentation is in full effect, it's recommended that a [GraphiQL](ht TEST_SITE_DB_PASSWORD="" TEST_SITE_TABLE_PREFIX="wp_" TEST_TABLE_PREFIX="wp_" - TEST_SITE_WP_URL="http://wp.test" - TEST_SITE_WP_DOMAIN="wp.test" - TEST_SITE_ADMIN_EMAIL="admin@wp.test" + TEST_SITE_WP_URL="http://localhost" + TEST_SITE_WP_DOMAIN="localhost" + TEST_SITE_ADMIN_EMAIL="admin@localhost" TEST_SITE_ADMIN_USERNAME="admin" TEST_SITE_ADMIN_PASSWORD="password" ``` @@ -69,7 +66,6 @@ Until the documentation is in full effect, it's recommended that a [GraphiQL](ht - `Install script` variables are specified to the `install-wp-tests` script, and most likely won't changed. I've listed their meaning below. - `WP_VERSION` WordPress version used for testing - `SKIP_DB_CREATE` Should database creation be skipped? - - `WP_GRAPHQL_BRANCH` The branch in the `WPGraphQL` repository the tests should be run again. Ex. `origin/feature/model-layer` - `Codeception` variables are specified to the **Codeception** configuration. View the config files and Codeception's [Docs](https://codeception.com/docs/reference/Configuration#Suite-Configuration) for more info on them. 4. Once you have finish modifying the `.env` file. Run `composer install-wp-tests` from the project directory. @@ -81,6 +77,17 @@ If you use the command with at least a `suite` specified, **Codeception** will r To learn more about the usage of Codeception with WordPress view the [Documentation](https://codeception.com/for/wordpress) +## Functional and Acceptance Tests (Docker/Docker-Compose required) +It's possible to run functional and acceptance tests, but is very limited at the moment. The script docker entrypoint script runs all three suites (acceptance, functional, and wpunit) at once. This will change eventually, however as of right now, this is the limitation. + +### Running tests +Even though the two suite use a Docker environment to run, the docker environment relies on a few environmental variables defined in `.env` and a volume source provided by the test install script. +0. Ensure that you can copy `.env.dist` to `.env`. +1. First you must run `composer install-wp-tests` to ensure the required dependencies are available. +2. Next run `docker-compose build` from the terminal in the project root directory, to build the docker image for test environment. +3. And now you're ready to run the tests. Running `docker-compose run --rm wpbrowser` does just that. +You can rerun the tests by simply repeating step 3. + ## HTTP Error 500 :construction: If you get HTTP 500 error upon activation or accessing the `endpoint` and have **CMD/Terminal** access with **Composer** installed. - Try deleting the `vendor` directory `rm -rf vendor` and regenerating the autoloading files `composer dumpautoload -o` in the `wp-graphql-woocommerce` directory in your WordPress installation's `plugins` directory. diff --git a/README.txt b/README.txt index eb6e7c8b7..7cf43a476 100644 --- a/README.txt +++ b/README.txt @@ -1,11 +1,13 @@ === WP GraphQL WooCommerce === Contributors: kidunot89 -Tags: GraphQL, WooCommerce -Requires at least: 4.7.0 -Tested up to: 5.1 +Tags: GraphQL, WooCommerce, WPGraphQL +Requires at least: 4.9 +Tested up to: 5.2 +Requires PHP: 5.6 +Stable tag: 0.2.1 License: GPL-3 License URI: https://www.gnu.org/licenses/gpl-3.0.html -Maintained at: https://github.com/kidunot89/wp-graphql-woocommerce +Maintained at: https://github.com/wp-graphql/wp-graphql-woocommerce == Description == -Adds WooCommerce Types to WPGraphQL schema +Adds WooCommerce functionality to the WPGraphQL schema. diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index be5f24441..9d863e475 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -export $(cat .env | xargs) +source .env print_usage_instruction() { echo "Ensure that .env file exist in project root directory exists." @@ -30,25 +30,17 @@ if [[ -z "$TEST_DB_HOST" ]]; then else DB_HOST=$TEST_DB_HOST fi -if [ -z "$WP_VERSION" ]; then - WP_VERSION=latest -fi if [ -z "$SKIP_DB_CREATE" ]; then SKIP_DB_CREATE=false fi -if [[ -z "$TEST_SITE_WP_URL" ]]; then - echo "TEST_SITE_WP_URL not found" - print_usage_instruction -else - DB_NAME=$TEST_DB_NAME -fi +WP_VERSION=${WP_VERSION-latest} 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/} PLUGIN_DIR=$(pwd) -DB_SERVE_NAME=${DB_SERVE_NAME-wpgraphql_serve} +DB_SERVE_NAME=${DB_SERVE_NAME-woographql_serve} download() { if [ `which curl` ]; then @@ -189,15 +181,13 @@ install_db() { configure_wordpress() { cd $WP_CORE_DIR wp config create --dbname="$DB_NAME" --dbuser="$DB_USER" --dbpass="$DB_PASS" --dbhost="$DB_HOST" --skip-check --force=true - wp core install --url="$TEST_SITE_WP_URL" --title="WPGraphQL WooCommerce Tests" --admin_user=admin --admin_password=password --admin_email=admin@wp.test + wp core install --url=wp.test --title="WPGraphQL WooCommerce Tests" --admin_user=admin --admin_password=password --admin_email=admin@wp.test wp rewrite structure '/%year%/%monthnum%/%postname%/' } setup_woocommerce() { echo "Installing & Activating WooCommerce" wp plugin install woocommerce --activate - echo "Upgrading database" - wp wc update echo "Installing & Activating WordPress Importer" wp plugin install wordpress-importer wp plugin activate wordpress-importer @@ -208,26 +198,14 @@ setup_woocommerce() { setup_wpgraphql() { if [ ! -d $WP_CORE_DIR/wp-content/plugins/wp-graphql ]; then echo "Cloning WPGraphQL" - git clone https://github.com/wp-graphql/wp-graphql.git $WP_CORE_DIR/wp-content/plugins/wp-graphql + wp plugin install https://github.com/wp-graphql/wp-graphql/archive/master.zip fi - - cd $WP_CORE_DIR/wp-content/plugins/wp-graphql - git checkout develop - git pull origin develop - - if [ ! -z "$WP_GRAPHQL_BRANCH" ]; then - echo "Checking out WPGraphQL branch - $WP_GRAPHQL_BRANCH" - git checkout --track origin/$WP_GRAPHQL_BRANCH - fi - - - cd $WP_CORE_DIR echo "Activating WPGraphQL" wp plugin activate wp-graphql if [ ! -d $WP_CORE_DIR/wp-content/plugins/wp-graphql-jwt-authentication ]; then echo "Cloning WPGraphQL-JWT-Authentication" - git clone https://github.com/wp-graphql/wp-graphql-jwt-authentication.git $WP_CORE_DIR/wp-content/plugins/wp-graphql-jwt-authentication + wp plugin install https://github.com/wp-graphql/wp-graphql-jwt-authentication/archive/master.zip fi echo "Activating WPGraphQL-JWT-Authentication" wp plugin activate wp-graphql-jwt-authentication diff --git a/bin/testing-entrypoint.sh b/bin/testing-entrypoint.sh new file mode 100644 index 000000000..0c744fcaf --- /dev/null +++ b/bin/testing-entrypoint.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# Run app entrypoint script. +. app-entrypoint.sh + +# Ensure Apache is running +service apache2 start + +# Ensure everything is loaded +dockerize \ + -wait tcp://${TEST_DB_HOST}:${TEST_DB_HOST_PORT:-3306} \ + -wait ${WP_URL} \ + -timeout 1m + +# Download c3 for testing. +if [ ! -f "${PROJECT_DIR}/c3.php" ]; then + echo 'Downloading c3.php' + curl -L 'https://raw.github.com/Codeception/c3/2.0/c3.php' > "${PROJECT_DIR}/c3.php" +fi + +# Run the tests +echo 'Moving to WooGraphQL directory.' +cd ${PROJECT_DIR} + +echo 'Setting Codeception output directory permissions'. +chmod 777 ${TESTS_OUTPUT} + +if [ "$COVERAGE" == "1" -a "$DEBUG" == "1" ]; then + vendor/bin/codecept run ${SUITE} --debug --coverage --coverage-xml +elif [ "$COVERAGE" == "1" ]; then + vendor/bin/codecept run ${SUITE} --coverage --coverage-xml +elif [ "$DEBUG" == "1" ]; then + vendor/bin/codecept run ${SUITE} --debug +else + vendor/bin/codecept run ${SUITE} +fi \ No newline at end of file diff --git a/codeception.docker.yml b/codeception.dist.yml similarity index 67% rename from codeception.docker.yml rename to codeception.dist.yml index 1592e97b2..8b51ce3d3 100644 --- a/codeception.docker.yml +++ b/codeception.dist.yml @@ -1,23 +1,25 @@ paths: - tests: tests - output: tests/_output - data: tests/_data - support: tests/_support - envs: tests/_envs + tests: '%TESTS_DIR%' + output: '%TESTS_OUTPUT%' + data: '%TESTS_DATA%' + support: '%TESTS_SUPPORT%' + envs: '%TESTS_ENVS%' params: - env + - .env.dist actor_suffix: Tester settings: colors: true memory_limit: 1024M coverage: enabled: true + c3_url: http://localhost/wp-content/plugins/wp-graphql-woocommerce/wp-graphql-woocommerce.php whitelist: include: - - wp-content/plugins/wp-graphql-woocommerce/wp-graphql-woocommerce.php - - wp-content/plugins/wp-graphql-woocommerce/access-functions.php - - wp-content/plugins/wp-graphql-woocommerce/class-inflect.php - - wp-content/plugins/wp-graphql-woocommerce/includes/*.php + - wp-graphql-woocommerce.php + - access-functions.php + - class-inflect.php + - includes/*.php extensions: enabled: - Codeception\Extension\RunFailed @@ -39,18 +41,19 @@ modules: dump: 'tests/_data/dump.sql' populate: true cleanup: true + waitlock: 0 url: '%WP_URL%' urlReplacement: true tablePrefix: '%WP_TABLE_PREFIX%' WPBrowser: - url: 'http://localhost' + url: '%WP_URL%' wpRootFolder: '%WP_ROOT_FOLDER%' adminUsername: '%ADMIN_USERNAME%' adminPassword: '%ADMIN_PASSWORD%' adminPath: '/wp-admin' REST: depends: WPBrowser - url: 'http://localhost' + url: '%WP_URL%' WPFilesystem: wpRootFolder: '%WP_ROOT_FOLDER%' plugins: '/wp-content/plugins' @@ -58,15 +61,15 @@ modules: themes: '/wp-content/themes' uploads: '/wp-content/uploads' WPLoader: - wpRootFolder: "%WP_ROOT_FOLDER%" - dbName: "%DB_NAME%" - dbHost: "%DB_HOST%" - dbUser: "%DB_USER%" - dbPassword: "%DB_PASSWORD%" - tablePrefix: "%WP_TABLE_PREFIX%" - domain: "%WP_DOMAIN%" - adminEmail: "%ADMIN_EMAIL%" - title: "Test" + wpRootFolder: '%WP_ROOT_FOLDER%' + dbName: '%DB_NAME%' + dbHost: '%DB_HOST%' + dbUser: '%DB_USER%' + dbPassword: '%DB_PASSWORD%' + tablePrefix: '%WP_TABLE_PREFIX%' + domain: '%WP_DOMAIN%' + adminEmail: '%ADMIN_EMAIL%' + title: 'Test' plugins: ['woocommerce/woocommerce.php', 'wp-graphql/wp-graphql.php', 'wp-graphql-woocommerce/wp-graphql-woocommerce.php'] activatePlugins: ['woocommerce/woocommerce.php', 'wp-graphql/wp-graphql.php', 'wp-graphql-woocommerce/wp-graphql-woocommerce.php'] - configFile: "tests/_data/config.php" + configFile: 'tests/_data/config.php' diff --git a/codeception.yml b/codeception.yml deleted file mode 100644 index 25924fa0c..000000000 --- a/codeception.yml +++ /dev/null @@ -1,73 +0,0 @@ -paths: - tests: tests - output: tests/_output - data: tests/_data - support: tests/_support - envs: tests/_envs -actor_suffix: Tester -settings: - colors: true - memory_limit: 1024M -coverage: - enabled: true - whitelist: - include: - - wp-graphql-woocommerce.php - - access-functions.php - - class-inflect.php - - includes/*.php -extensions: - enabled: - - Codeception\Extension\RunFailed - commands: - - Codeception\Command\GenerateWPUnit - - Codeception\Command\GenerateWPRestApi - - Codeception\Command\GenerateWPRestController - - Codeception\Command\GenerateWPRestPostTypeController - - Codeception\Command\GenerateWPAjax - - Codeception\Command\GenerateWPCanonical - - Codeception\Command\GenerateWPXMLRPC -params: - - .env -modules: - config: - WPDb: - dsn: 'mysql:host=%TEST_SITE_DB_HOST%;dbname=%TEST_SITE_DB_NAME%' - user: '%TEST_SITE_DB_USER%' - password: '%TEST_SITE_DB_PASSWORD%' - dump: 'tests/_data/dump.sql' - #import the dump before the tests; this means the test site database will be repopulated before the tests. - populate: true - # re-import the dump between tests; this means the test site database will be repopulated between the tests. - cleanup: true - waitlock: 0 - url: '%TEST_SITE_WP_URL%' - urlReplacement: true #replace the hardcoded dump URL with the one above - tablePrefix: '%TEST_SITE_TABLE_PREFIX%' - WPBrowser: - url: '%TEST_SITE_WP_URL%' - adminUsername: '%TEST_SITE_ADMIN_USERNAME%' - adminPassword: '%TEST_SITE_ADMIN_PASSWORD%' - adminPath: '%TEST_SITE_WP_ADMIN_PATH%' - REST: - depends: WPBrowser - url: '%TEST_SITE_WP_URL%' - WPFilesystem: - wpRootFolder: '%WP_ROOT_FOLDER%' - plugins: '/wp-content/plugins' - mu-plugins: '/wp-content/mu-plugins' - themes: '/wp-content/themes' - uploads: '/wp-content/uploads' - WPLoader: - wpRootFolder: "%WP_ROOT_FOLDER%" - dbName: "%TEST_DB_NAME%" - dbHost: "%TEST_DB_HOST%" - dbUser: "%TEST_DB_USER%" - dbPassword: "%TEST_DB_PASSWORD%" - tablePrefix: "%TEST_TABLE_PREFIX%" - domain: "%TEST_SITE_WP_DOMAIN%" - adminEmail: "%TEST_SITE_ADMIN_EMAIL%" - title: "Test" - plugins: ['woocommerce/woocommerce.php', 'wp-graphql/wp-graphql.php', 'wp-graphql-woocommerce/wp-graphql-woocommerce.php'] - activatePlugins: ['woocommerce/woocommerce.php', 'wp-graphql/wp-graphql.php', 'wp-graphql-woocommerce/wp-graphql-woocommerce.php'] - configFile: "tests/_data/config.php" diff --git a/composer.json b/composer.json index 840d916a7..f3c6bd5f1 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "wp-graphql/wp-graphql-woocommerce", "description": "WooCommerce bindings for wp-graphql", - "version": "0.2.0", + "version": "0.2.1", "type": "wordpress-plugin", "keywords": [ "wordpress", @@ -21,8 +21,11 @@ "role": "Lead developer" } ], + "require": { + "php": ">=5.6.0" + }, "require-dev": { - "lucatume/wp-browser": "^2.2" + "lucatume/wp-browser": ">=2.2.1 <2.2.8" }, "config": { "optimize-autoloader": true @@ -35,9 +38,6 @@ "includes/" ] }, - "require": { - "php": ">=5.6.0" - }, "scripts": { "install-wp-tests": "bash bin/install-wp-tests.sh", "test": "vendor/bin/codecept run", diff --git a/docker-compose.yml b/docker-compose.yml index d8c29724b..7f3173eea 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,32 +1,72 @@ version: '3.3' services: - - wpbrowser: - build: . - image: kidunot89/wp-browser - links: - - db:mysql + app: + depends_on: + - app_db + image: "kidunot89/woographql-app:wp${WP_VERSION:-5.2.2}-php${PHP_VERSION:-7.2}" volumes: - - "/tmp/wordpress/wp-content:/var/www/html/wp-content" - - "./tests:/var/www/html/tests" - - "./codeception.docker.yml:/var/www/config/codeception.dist.yml" - - ".:/var/www/html/wp-content/plugins/wp-graphql-woocommerce" - command: bash -c "codecept run functional --debug && codecept run acceptance && codecept run wpunit" + - '.:/var/www/html/wp-content/plugins/wp-graphql-woocommerce' + - './.log/app:/var/log/apache2' + env_file: .env.dist environment: - DB_NAME: wordpress - DB_HOST: db - DB_USER: wordpress - DB_PASSWORD: wordpress - WPGRAPHQL_WOOCOMMERCE_AUTOLOAD: 1 + WP_URL: 'http://localhost:8001' + WP_DOMAIN: 'localhost:8001' + DB_HOST: app_db + INCLUDE_WPGRAPHIQL: 1 ports: - - 8080:80 + - '8001:80' + networks: + local: - db: + app_db: image: mysql:5.7 - restart: always environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: wordpress MYSQL_USER: wordpress - MYSQL_PASSWORD: wordpress \ No newline at end of file + MYSQL_PASSWORD: wordpress + ports: + - '3306' + networks: + testing: + local: + + testing: + depends_on: + - app_db + - testing_db + build: + context: . + dockerfile: Dockerfile + args: + DESIRED_PHP_VERSION: "${PHP_VERSION:-7.2}" + DESIRED_WP_VERSION: "${WP_VERSION:-5.2.2}" + image: woographql-testing + volumes: + - '.:/var/www/html/wp-content/plugins/wp-graphql-woocommerce' + - './.log/testing:/var/log/apache2' + env_file: .env.dist + environment: + WPGRAPHQL_WOOCOMMERCE_AUTOLOAD: 1 + TEST_DB_HOST: testing_db + DB_HOST: app_db + WP_URL: 'http://localhost' + networks: + testing: + + testing_db: + image: mysql:5.7 + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: woographql_tests + MYSQL_USER: wordpress + MYSQL_PASSWORD: wordpress + ports: + - '3306' + networks: + testing: + +networks: + local: + testing: \ No newline at end of file diff --git a/includes/mutation/class-cart-add-item.php b/includes/mutation/class-cart-add-item.php index 557112834..2e3bf8674 100644 --- a/includes/mutation/class-cart-add-item.php +++ b/includes/mutation/class-cart-add-item.php @@ -75,8 +75,7 @@ public static function get_output_fields() { 'cartItem' => array( 'type' => 'CartItem', 'resolve' => function ( $payload ) { - $cart = WC()->cart; - $item = $cart->get_cart_item( $payload['key'] ); + $item = \WC()->cart->get_cart_item( $payload['key'] ); return $item; }, @@ -95,6 +94,9 @@ public static function mutate_and_get_payload() { if ( empty( $input['productId'] ) ) { throw new UserError( __( 'No product ID provided', 'wp-graphql-woocommerce' ) ); } + if ( ! \wc_get_product( $input['productId'] ) ) { + throw new UserError( __( 'No product found matching the ID provided', 'wp-graphql-woocommerce' ) ); + } // Prepare args for "add_to_cart" from input data. $cart_item_args = Cart_Mutation::prepare_cart_item( $input, $context, $info ); @@ -102,6 +104,10 @@ public static function mutate_and_get_payload() { // Add item to cart and get item key. $cart_item_key = \WC()->cart->add_to_cart( ...$cart_item_args ); + if ( ! $cart_item_key ) { + throw new UserError( __( 'Failed to add cart item. Please check input.', 'wp-graphql-woocommerce' ) ); + } + // Return payload. return array( 'key' => $cart_item_key ); }; diff --git a/tests/functional/QLSessionHandlerCest.php b/tests/functional/QLSessionHandlerCest.php index 88be09082..5e57dbcb7 100644 --- a/tests/functional/QLSessionHandlerCest.php +++ b/tests/functional/QLSessionHandlerCest.php @@ -4,29 +4,10 @@ class QLSessionHandlerCest { private $product_id; public function _before( FunctionalTester $I ) { - // Activate plugins - $I->loginAsAdmin(); - $I->amOnPluginsPage(); - $I->activatePlugin( - array( - 'woocommerce', - 'wp-graphql', - 'wpgraphql-jwt-authentication', - 'wp-graphql-woocommerce', - ) - ); - $I->seePluginActivated( 'woocommerce' ); - $I->seePluginActivated( 'wp-graphql' ); - $I->seePluginActivated( 'wpgraphql-jwt-authentication' ); - $I->seePluginActivated( 'wp-graphql-woocommerce' ); - $I->amOnAdminPage('options-permalink.php'); - $I->click('#submit'); - $I->amOnPage( '/' ); - // Create Product $this->product_id = $I->havePostInDatabase( array( 'post_type' => 'product', - 'post_title' => 't-shirt', + 'post_name' => 't-shirt', 'meta_input' => array( '_visibility' => 'visible', '_sku' => '', @@ -63,6 +44,9 @@ public function _before( FunctionalTester $I ) { '_wc_review_count' => 0, ), )); + $term_id = $I->grabTermIdFromDatabase( [ 'name' => 'simple', 'slug' => 'simple' ] ); + $term_taxonomy_id = $I->grabTermTaxonomyIdFromDatabase( [ 'term_id' => $term_id, 'taxonomy' => 'product_type' ] ); + $I->haveTermRelationshipInDatabase( $this->product_id, $term_id ); } // tests diff --git a/tests/wpunit/CartMutationsTest.php b/tests/wpunit/CartMutationsTest.php index 52a02f41e..944ab179a 100644 --- a/tests/wpunit/CartMutationsTest.php +++ b/tests/wpunit/CartMutationsTest.php @@ -8,7 +8,7 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase { private $variation; private $cart; - public function setUp(): void { + public function setUp() { parent::setUp(); $this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) ); @@ -22,7 +22,7 @@ public function setUp(): void { WC()->cart->empty_cart( true ); } - public function tearDown(): void { + public function tearDown() { \WC()->cart->empty_cart(); parent::tearDown(); diff --git a/tests/wpunit/CartQueriesTest.php b/tests/wpunit/CartQueriesTest.php index b01e6a8a0..a2daa7edd 100644 --- a/tests/wpunit/CartQueriesTest.php +++ b/tests/wpunit/CartQueriesTest.php @@ -8,7 +8,7 @@ class CartQueriesTest extends \Codeception\TestCase\WPTestCase { private $coupon_helper; private $helper; - public function setUp(): void { + public function setUp() { // before parent::setUp(); @@ -23,7 +23,7 @@ public function setUp(): void { WC()->cart->empty_cart( true ); } - public function tearDown(): void { + public function tearDown() { // your tear down methods here // then diff --git a/tests/wpunit/CheckoutMutationTest.php b/tests/wpunit/CheckoutMutationTest.php index 0c749f1e4..f2e9be3de 100644 --- a/tests/wpunit/CheckoutMutationTest.php +++ b/tests/wpunit/CheckoutMutationTest.php @@ -3,7 +3,7 @@ use WPGraphQL\Type\WPEnumType; class CheckoutMutationTest extends \Codeception\TestCase\WPTestCase { - public function setUp(): void { + public function setUp() { // before parent::setUp(); @@ -69,7 +69,7 @@ function() { wp_logout(); } - public function tearDown(): void { + public function tearDown() { // your tear down methods here // then diff --git a/tests/wpunit/CouponQueriesTest.php b/tests/wpunit/CouponQueriesTest.php index 945517fe6..1b1968c96 100644 --- a/tests/wpunit/CouponQueriesTest.php +++ b/tests/wpunit/CouponQueriesTest.php @@ -7,7 +7,7 @@ class CouponQueriesTest extends \Codeception\TestCase\WPTestCase { private $coupon; private $helper; - public function setUp(): void { + public function setUp() { parent::setUp(); $this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) ); @@ -22,7 +22,7 @@ public function setUp(): void { ); } - public function tearDown(): void { + public function tearDown() { // your tear down methods here // then parent::tearDown(); diff --git a/tests/wpunit/CustomerMutationsTest.php b/tests/wpunit/CustomerMutationsTest.php index 1adfaecea..f6a7bac88 100644 --- a/tests/wpunit/CustomerMutationsTest.php +++ b/tests/wpunit/CustomerMutationsTest.php @@ -1,7 +1,7 @@ new_customer = $this->helper->create(); } - public function tearDown(): void { + public function tearDown() { // your tear down methods here // then parent::tearDown(); diff --git a/tests/wpunit/OrderItemQueriesTest.php b/tests/wpunit/OrderItemQueriesTest.php index 94d219658..c867a768e 100644 --- a/tests/wpunit/OrderItemQueriesTest.php +++ b/tests/wpunit/OrderItemQueriesTest.php @@ -10,7 +10,7 @@ class OrderItemQueriesTest extends \Codeception\TestCase\WPTestCase { private $item_helper; private $order_helper; - public function setUp(): void { + public function setUp() { parent::setUp(); $this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) ); @@ -20,7 +20,7 @@ public function setUp(): void { $this->order = $this->order_helper->create(); } - public function tearDown(): void { + public function tearDown() { // your tear down methods here // then diff --git a/tests/wpunit/OrderMutationsTest.php b/tests/wpunit/OrderMutationsTest.php index 39167f324..b259f13cf 100644 --- a/tests/wpunit/OrderMutationsTest.php +++ b/tests/wpunit/OrderMutationsTest.php @@ -4,7 +4,7 @@ class OrderMutationsTest extends \Codeception\TestCase\WPTestCase { - public function setUp(): void { + public function setUp() { // before parent::setUp(); @@ -42,7 +42,7 @@ public function setUp(): void { $this->order_id = $this->order->create(); } - public function tearDown(): void { + public function tearDown() { // your tear down methods here // then diff --git a/tests/wpunit/OrderQueriesTest.php b/tests/wpunit/OrderQueriesTest.php index 75a72922f..50475d10d 100644 --- a/tests/wpunit/OrderQueriesTest.php +++ b/tests/wpunit/OrderQueriesTest.php @@ -10,7 +10,7 @@ class OrderQueriesTest extends \Codeception\TestCase\WPTestCase { private $product_helper; private $customer_helper; - public function setUp(): void { + public function setUp() { parent::setUp(); $this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) ); @@ -21,7 +21,7 @@ public function setUp(): void { $this->order = $this->order_helper->create(); } - public function tearDown(): void { + public function tearDown() { // your tear down methods here // then parent::tearDown(); diff --git a/tests/wpunit/ProductAttributeQueriesTest.php b/tests/wpunit/ProductAttributeQueriesTest.php index b31ab302b..11ddde08d 100644 --- a/tests/wpunit/ProductAttributeQueriesTest.php +++ b/tests/wpunit/ProductAttributeQueriesTest.php @@ -8,7 +8,7 @@ class ProductAttributeQueriesTest extends \Codeception\TestCase\WPTestCase { private $product_id; private $variation_ids; - public function setUp(): void { + public function setUp() { parent::setUp(); $this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) ); @@ -20,7 +20,7 @@ public function setUp(): void { } - public function tearDown(): void { + public function tearDown() { parent::tearDown(); } diff --git a/tests/wpunit/ProductQueriesTest.php b/tests/wpunit/ProductQueriesTest.php index 796cf809b..48b1069d4 100644 --- a/tests/wpunit/ProductQueriesTest.php +++ b/tests/wpunit/ProductQueriesTest.php @@ -9,7 +9,7 @@ class ProductQueriesTest extends \Codeception\TestCase\WPTestCase { private $product_tag; private $product_cat; - public function setUp(): void { + public function setUp() { // before parent::setUp(); @@ -44,7 +44,7 @@ public function setUp(): void { update_term_meta( $category_id, 'thumbnail_id', $this->image_id ); } - public function tearDown(): void { + public function tearDown() { // your tear down methods here $product = \WC()->product_factory->get_product( $this->product ); $product->delete( true ); diff --git a/tests/wpunit/ProductVariationQueriesTest.php b/tests/wpunit/ProductVariationQueriesTest.php index 500480ec8..5477f69fc 100644 --- a/tests/wpunit/ProductVariationQueriesTest.php +++ b/tests/wpunit/ProductVariationQueriesTest.php @@ -7,7 +7,7 @@ class ProductVariationQueriesTest extends \Codeception\TestCase\WPTestCase { private $customer; private $products; - public function setUp(): void { + public function setUp() { parent::setUp(); $this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) ); @@ -17,7 +17,7 @@ public function setUp(): void { $this->products = $this->helper->create( $this->product_helper->create_variable() ); } - public function tearDown(): void { + public function tearDown() { // your tear down methods here // then diff --git a/tests/wpunit/RefundQueriesTest.php b/tests/wpunit/RefundQueriesTest.php index a63af3c79..6c096d4c5 100644 --- a/tests/wpunit/RefundQueriesTest.php +++ b/tests/wpunit/RefundQueriesTest.php @@ -9,7 +9,7 @@ class RefundQueriesTest extends \Codeception\TestCase\WPTestCase { private $order; private $refund; - public function setUp(): void { + public function setUp() { parent::setUp(); $this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) ); @@ -20,7 +20,7 @@ public function setUp(): void { $this->refund = $this->refund_helper->create( $this->order ); } - public function tearDown(): void { + public function tearDown() { // your tear down methods here // then parent::tearDown(); diff --git a/tests/wpunit/ShippingMethodQueriesTest.php b/tests/wpunit/ShippingMethodQueriesTest.php index a30df21ab..dc8dab69b 100644 --- a/tests/wpunit/ShippingMethodQueriesTest.php +++ b/tests/wpunit/ShippingMethodQueriesTest.php @@ -8,7 +8,7 @@ class ShippingMethodQueriesTest extends \Codeception\TestCase\WPTestCase { private $method; private $helper; - public function setUp(): void { + public function setUp() { parent::setUp(); $this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) ); @@ -17,7 +17,7 @@ public function setUp(): void { $this->method = 'flat_rate'; } - public function tearDown(): void { + public function tearDown() { // your tear down methods here // then parent::tearDown(); diff --git a/tests/wpunit/TaxRateQueriesTest.php b/tests/wpunit/TaxRateQueriesTest.php index e680d014d..10f8e12e6 100644 --- a/tests/wpunit/TaxRateQueriesTest.php +++ b/tests/wpunit/TaxRateQueriesTest.php @@ -8,7 +8,7 @@ class TaxRateQueriesTest extends \Codeception\TestCase\WPTestCase { private $rate; private $helper; - public function setUp(): void { + public function setUp() { parent::setUp(); $this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) ); @@ -17,7 +17,7 @@ public function setUp(): void { $this->rate = $this->helper->create(); } - public function tearDown(): void { + public function tearDown() { // your tear down methods here // then parent::tearDown(); diff --git a/tests/wpunit/VariationAttributeQueriesTest.php b/tests/wpunit/VariationAttributeQueriesTest.php index 5c25c6d8b..74a6a7de1 100644 --- a/tests/wpunit/VariationAttributeQueriesTest.php +++ b/tests/wpunit/VariationAttributeQueriesTest.php @@ -6,7 +6,7 @@ class VariationAttributeQueriesTest extends \Codeception\TestCase\WPTestCase { private $product_id; private $variation_id; - public function setUp(): void { + public function setUp() { // before parent::setUp(); @@ -19,7 +19,7 @@ public function setUp(): void { $this->variation_id = $ids['variations'][0]; } - public function tearDown(): void { + public function tearDown() { // your tear down methods here // then diff --git a/wp-graphql-woocommerce.php b/wp-graphql-woocommerce.php index 0ffaadd49..4bec32d81 100644 --- a/wp-graphql-woocommerce.php +++ b/wp-graphql-woocommerce.php @@ -3,7 +3,7 @@ * Plugin Name: WP GraphQL WooCommerce * Plugin URI: https://github.com/kidunot89/wp-graphql-woocommerce * Description: Adds Woocommerce Functionality to WPGraphQL schema. - * Version: 0.2.0 + * Version: 0.2.1 * Author: kidunot89 * Author URI: https://axistaylor.com * Text Domain: wp-graphql-woocommerce @@ -19,14 +19,15 @@ // Exit if accessed directly. defined( 'ABSPATH' ) || exit; -defined( 'GRAPHQL_DEBUG' ) || define( 'GRAPHQL_DEBUG', true ); - /** * If the codeception remote coverage file exists, require it. * * This file should only exist locally or when CI bootstraps the environment for testing */ if ( file_exists( __DIR__ . '/c3.php' ) ) { + // Get tests output directory. + $test_dir = __DIR__ . '/tests/output'; + define( 'C3_CODECOVERAGE_ERROR_LOG_FILE', $test_dir . '/c3_error.log' ); require_once __DIR__ . '/c3.php'; } @@ -36,7 +37,7 @@ function wp_graphql_woocommerce_constants() { // Plugin version. if ( ! defined( 'WPGRAPHQL_WOOCOMMERCE_VERSION' ) ) { - define( 'WPGRAPHQL_WOOCOMMERCE_VERSION', '0.0.1' ); + define( 'WPGRAPHQL_WOOCOMMERCE_VERSION', '0.2.1' ); } // Plugin Folder Path. if ( ! defined( 'WPGRAPHQL_WOOCOMMERCE_PLUGIN_DIR' ) ) {