From b8c7b109925587c5d5901fe420e76d2af2cd230a Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Thu, 25 Jul 2019 00:04:32 -0400 Subject: [PATCH] Codeception configuration heavily refactored --- .dockerignore | 3 + .env.dist | 21 ++++-- .gitignore | 8 +- .travis.yml | 22 ++++-- Dockerfile | 69 +++++++---------- Dockerfile.tester | 55 -------------- bin/entrypoint.sh | 70 ------------------ bin/install-wp-tests.sh | 20 +---- bin/testing-entrypoint.sh | 31 ++++---- ...ception.docker.yml => codeception.dist.yml | 38 +++++----- codeception.yml | 74 ------------------- docker-compose.yml | 16 ++-- includes/mutation/class-cart-add-item.php | 10 ++- tests/functional/QLSessionHandlerCest.php | 5 +- vendor/composer/ClassLoader.php | 2 +- wp-graphql-woocommerce.php | 3 + 16 files changed, 126 insertions(+), 321 deletions(-) create mode 100644 .dockerignore delete mode 100644 Dockerfile.tester delete mode 100644 bin/entrypoint.sh rename codeception.docker.yml => codeception.dist.yml (72%) delete mode 100644 codeception.yml 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 acfb4cb15..db45227c9 100644 --- a/.env.dist +++ b/.env.dist @@ -5,16 +5,23 @@ DB_PASSWORD=wordpress WP_TABLE_PREFIX=wp_ WP_URL=http://localhost WP_DOMAIN=localhost -ADMIN_EMAIL=admin@localhost +ADMIN_EMAIL=admin@example.com ADMIN_USERNAME=admin ADMIN_PASSWORD=password ADMIN_PATH=/wp-admin -SKIP_DB_CREATE=false -TEST_WP_ROOT_FOLDER=/tmp/wordpress -TEST_DB_NAME=wpgraphql_woocommerce_test +TEST_DB_NAME=woographql_tests TEST_DB_HOST=127.0.0.1 -TEST_DB_USER=root -TEST_DB_PASSWORD= +TEST_DB_USER=wordpress +TEST_DB_PASSWORD=wordpress TEST_WP_TABLE_PREFIX=wp_ -TEST_SITE_ADMIN_EMAIL=admin@wp.test \ No newline at end of file + +SKIP_DB_CREATE=false +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 6a9e78f83..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,8 +19,8 @@ vendor/* !vendor/composer vendor/composer/installed.json vendor/composer/*/ -!/tests -/tests/*.suite.yml +!tests +tests/*.suite.yml build/ coverage/* schema.graphql @@ -26,4 +28,4 @@ phpunit.xml docker-output composer.lock c3.php -.log/ \ No newline at end of file +.log/ diff --git a/.travis.yml b/.travis.yml index 2dfc9eca8..45d4497b3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,7 +36,7 @@ env: - PHP_VERSION=7.1 WP_VERSION=5.2.2 - PHP_VERSION=7.0 WP_VERSION=5.2.2 - PHP_VERSION=5.6 WP_VERSION=4.9 - - PHP_VERSION=5.6 WP_VERSION=5.2.2 DEBUG=1 + - PHP_VERSION=5.6 WP_VERSION=5.0.2 DEBUG=1 before_install: - sudo rm /usr/local/bin/docker-compose @@ -78,7 +78,10 @@ before_script: # 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 @@ -87,13 +90,20 @@ script: # Execute unit tests with coverage if specified, otherwise without coverage - | if [ ! -z "$WP_VERSION" ]; then - docker-compose run -e SUITE=functional -e COVERAGE=${COVERAGE:-0} testing --rm testing --scale app=0 - docker-compose run -e SUITE=acceptance -e COVERAGE=${COVERAGE:-0} testing --rm testing --scale app=0 - docker-compose run -e SUITE=wpunit -e COVERAGE=${COVERAGE:-0} testing --rm testing --scale app=0 + 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 diff --git a/Dockerfile b/Dockerfile index a767813ae..bc9d55186 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,59 +1,42 @@ -############################################################################### -# Pre-configured WordPress Installation w/ WooCommerce, WPGraphQL, WooGraphQL # -# For testing only, use in production not recommended. # -############################################################################### +############################################################################ +# Container for running Codeception tests on a WooGraphQL Docker instance. # +############################################################################ # Using the 'DESIRED_' prefix to avoid confusion with environment variables of the same name. ARG DESIRED_WP_VERSION ARG DESIRED_PHP_VERSION -FROM wordpress:${DESIRED_WP_VERSION}-php${DESIRED_PHP_VERSION}-apache +FROM kidunot89/woographql-app:wp${DESIRED_WP_VERSION:-5.2.2}-php${DESIRED_PHP_VERSION:-7.3} LABEL author=kidunot89 LABEL author_uri=https://github.com/kidunot89 SHELL [ "/bin/bash", "-c" ] -# Install system packages -RUN apt-get update && \ - apt-get -y install \ - # CircleCI depedencies - git \ - ssh \ - tar \ - gzip \ - wget \ - mariadb-client - -# Install Dockerize -ENV DOCKERIZE_VERSION v0.6.1 -RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ - && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ - && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz - -# Install WP-CLI -RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \ - && chmod +x wp-cli.phar \ - && mv wp-cli.phar /usr/local/bin/wp - -# Set project environmental variables -ENV WP_ROOT_FOLDER="/var/www/html" -ENV WORDPRESS_DB_HOST=${DB_HOST} -ENV WORDPRESS_DB_USER=${DB_USER} -ENV WORDPRESS_DB_PASSWORD=${DB_PASSWORD} -ENV WORDPRESS_DB_NAME=${DB_NAME} -ENV PLUGINS_DIR="${WP_ROOT_FOLDER}/wp-content/plugins" -ENV PROJECT_DIR="${PLUGINS_DIR}/wp-graphql-woocommerce" +# Install php extensions +RUN docker-php-ext-install pdo_mysql -# Remove exec statement from base entrypoint script. -RUN sed -i '$d' /usr/local/bin/docker-entrypoint.sh +# Install Xdebug +RUN yes | pecl install xdebug \ + && 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 -# Set up Apache -RUN echo 'ServerName localhost' >> /etc/apache2/apache2.conf +# 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/entrypoint.sh /usr/local/bin/app-entrypoint.sh -RUN chmod 755 /usr/local/bin/app-entrypoint.sh -ENTRYPOINT ["app-entrypoint.sh"] -CMD ["apache2-foreground"] \ No newline at end of file +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/Dockerfile.tester b/Dockerfile.tester deleted file mode 100644 index 0e8a22322..000000000 --- a/Dockerfile.tester +++ /dev/null @@ -1,55 +0,0 @@ -############################################################################ -# Container for running Codeception tests on a WooGraphQL Docker instance. # -############################################################################ - -# 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:-5.2.2}-php${DESIRED_PHP_VERSION:-7.3} - -LABEL author=kidunot89 -LABEL author_uri=https://github.com/kidunot89 - -SHELL [ "/bin/bash", "-c" ] - -# Install php extensions -RUN docker-php-ext-install pdo_mysql - -# Install pcov -RUN pecl install pcov \ - && docker-php-ext-enable pcov - -# Install composer -ENV COMPOSER_ALLOW_SUPERUSER=1 - -RUN curl -sS https://getcomposer.org/installer | php -- \ - --filename=composer \ - --install-dir=/usr/local/bin - -# Install tool to speed up composer installations -RUN composer global require --optimize-autoloader \ - "hirak/prestissimo" - -# Install wp-browser globally -RUN composer global require \ - phpunit/phpunit:8.1 \ - lucatume/wp-browser:^2.2 \ - league/factory-muffin:^3.0 \ - league/factory-muffin-faker:^2.0 - -# Add composer global binaries to PATH -ENV PATH "$PATH:~/.composer/vendor/bin" - -# Set up wp-browser / codeception -WORKDIR /var/www/config -COPY codeception.docker.yml codeception.dist.yml - -# 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/bin/entrypoint.sh b/bin/entrypoint.sh deleted file mode 100644 index 5fb54cacc..000000000 --- a/bin/entrypoint.sh +++ /dev/null @@ -1,70 +0,0 @@ -#!/bin/bash - -# Run WordPress docker entrypoint. -. docker-entrypoint.sh 'apache2' - -# Ensure mysql is loaded -dockerize -wait tcp://${DB_HOST}:${DB_HOST_PORT:-3306} -timeout 1m - -# Config WordPress -if [ ! -f "${WP_ROOT_FOLDER}/wp-config.php" ]; then - wp config create \ - --path="${WP_ROOT_FOLDER}" \ - --dbname="${DB_NAME}" \ - --dbuser="${DB_USER}" \ - --dbpass="${DB_PASSWORD}" \ - --dbhost="${DB_HOST}" \ - --dbprefix="${WP_TABLE_PREFIX}" \ - --skip-check \ - --quiet \ - --allow-root -fi - -# Install WP if not yet installed -if ! $( wp core is-installed --allow-root ); then - wp core install \ - --path="${WP_ROOT_FOLDER}" \ - --url="${WP_URL}" \ - --title='Test' \ - --admin_user="${ADMIN_USERNAME}" \ - --admin_password="${ADMIN_PASSWORD}" \ - --admin_email="${ADMIN_EMAIL}" \ - --allow-root -fi - -# Install and activate WooCommerce -if [ ! -f "${PLUGINS_DIR}/woocommerce/woocommerce.php" ]; then - wp plugin install woocommerce --activate --allow-root -fi - -# Install and activate WPGraphQL -if [ ! -f "${PLUGINS_DIR}/wp-graphql/wp-graphql.php" ]; then - wp plugin install \ - https://github.com/wp-graphql/wp-graphql/archive/master.zip \ - --activate --allow-root -fi - -# Install and activate WPGraphQL JWT Authentication -if [ ! -f "${PLUGINS_DIR}/wp-graphql-jwt-authentication/wp-graphql-jwt-authentication.php" ]; then - wp plugin install \ - https://github.com/wp-graphql/wp-graphql-jwt-authentication/archive/master.zip \ - --activate --allow-root -fi - -if [ ! -z "$INCLUDE_WPGRAPHIQL" ]; then - if [ ! -f "${PLUGINS_DIR}/wp-graphiql/wp-graphiql.php" ]; then - wp plugin install \ - https://github.com/wp-graphql/wp-graphiql/archive/master.zip \ - --activate --allow-root - fi -fi - -# Activate WooGraphQL -wp plugin activate wp-graphql-woocommerce --allow-root - -# Set pretty permalinks. -wp rewrite structure '/%year%/%monthnum%/%postname%/' --allow-root - -wp db export "${PROJECT_DIR}/tests/_data/dump.sql" --allow-root - -exec "$@" \ No newline at end of file diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index f072d796a..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." @@ -40,7 +40,7 @@ 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 @@ -198,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 index c63df5ef7..0c744fcaf 100644 --- a/bin/testing-entrypoint.sh +++ b/bin/testing-entrypoint.sh @@ -9,27 +9,28 @@ service apache2 start # Ensure everything is loaded dockerize \ -wait tcp://${TEST_DB_HOST}:${TEST_DB_HOST_PORT:-3306} \ - -wait http://localhost \ + -wait ${WP_URL} \ -timeout 1m - # Download c3 for testing. -if [ ! -f "${PROJECT_DIR}/c3.php" ]; then +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 -# Link codeception config if not yet linked -if [ ! -e codeception.dist.yml ]; then - ln -s /var/www/config/codeception.dist.yml /var/www/html/codeception.dist.yml -fi - # Run the tests -if [ "$COVERAGE" == "1" ]; then - codecept run ${SUITE} --debug --coverage --coverage-xml +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 - codecept run ${SUITE} --debug + vendor/bin/codecept run ${SUITE} --debug else - codecept run ${SUITE} -fi - -exec "$@" \ No newline at end of file + vendor/bin/codecept run ${SUITE} +fi \ No newline at end of file diff --git a/codeception.docker.yml b/codeception.dist.yml similarity index 72% rename from codeception.docker.yml rename to codeception.dist.yml index ac2aa145f..8b51ce3d3 100644 --- a/codeception.docker.yml +++ b/codeception.dist.yml @@ -1,11 +1,12 @@ paths: - tests: tests - log: 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 @@ -15,10 +16,10 @@ coverage: 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 @@ -33,25 +34,26 @@ extensions: modules: config: WPDb: - dsn: 'mysql:host=%TEST_DB_HOST%;dbname=%TEST_DB_NAME%' - user: '%TEST_DB_USER%' - password: '%TEST_DB_PASSWORD%' + dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%' + user: '%DB_USER%' + password: '%DB_PASSWORD%' populator: 'mysql -u $user -p$password -h $host $dbname < $dump' dump: 'tests/_data/dump.sql' populate: true cleanup: true - url: 'http://localhost' + waitlock: 0 + url: '%WP_URL%' urlReplacement: true - tablePrefix: '%TEST_WP_TABLE_PREFIX%' + 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' @@ -60,8 +62,8 @@ modules: uploads: '/wp-content/uploads' WPLoader: wpRootFolder: '%WP_ROOT_FOLDER%' - dbName: 'wordpress_tests' - dbHost: 'testing-db' + dbName: '%DB_NAME%' + dbHost: '%DB_HOST%' dbUser: '%DB_USER%' dbPassword: '%DB_PASSWORD%' tablePrefix: '%WP_TABLE_PREFIX%' diff --git a/codeception.yml b/codeception.yml deleted file mode 100644 index 16e28dcb4..000000000 --- a/codeception.yml +++ /dev/null @@ -1,74 +0,0 @@ -paths: - tests: tests - log: tests/_output - data: tests/_data - support: tests/_support - envs: tests/_envs -actor_suffix: Tester -settings: - colors: true - memory_limit: 1024M -coverage: - enabled: true - remote: 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_DB_HOST%;dbname=%DB_NAME%' - user: '%DB_USER%' - password: '%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: '%WP_URL%' - urlReplacement: true #replace the hardcoded dump URL with the one above - tablePrefix: '%WP_TABLE_PREFIX%' - WPBrowser: - url: '%WP_URL%' - adminUsername: '%ADMIN_USERNAME%' - adminPassword: '%ADMIN_PASSWORD%' - adminPath: '%ADMIN_PATH%' - REST: - depends: WPBrowser - url: '%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: "%TEST_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_WP_DOMAIN%" - adminEmail: "%TEST_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/docker-compose.yml b/docker-compose.yml index eb1c8823b..7f3173eea 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,7 +8,7 @@ services: volumes: - '.:/var/www/html/wp-content/plugins/wp-graphql-woocommerce' - './.log/app:/var/log/apache2' - env_file: .env + env_file: .env.dist environment: WP_URL: 'http://localhost:8001' WP_DOMAIN: 'localhost:8001' @@ -38,24 +38,20 @@ services: - testing_db build: context: . - dockerfile: Dockerfile.tester + 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' - - './tests:/var/www/html/tests' - './.log/testing:/var/log/apache2' - env_file: .env + env_file: .env.dist environment: + WPGRAPHQL_WOOCOMMERCE_AUTOLOAD: 1 TEST_DB_HOST: testing_db - TEST_DB_USER: wordpress - TEST_DB_PASSWORD: wordpress DB_HOST: app_db - DB_USER: wordpress - DB_PASSWORD: wordpress - WPGRAPHQL_WOOCOMMERCE_AUTOLOAD: 1 + WP_URL: 'http://localhost' networks: testing: @@ -63,7 +59,7 @@ services: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: root - MYSQL_DATABASE: wordpress_tests + MYSQL_DATABASE: woographql_tests MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress ports: 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 53464420a..5e57dbcb7 100644 --- a/tests/functional/QLSessionHandlerCest.php +++ b/tests/functional/QLSessionHandlerCest.php @@ -7,7 +7,7 @@ public function _before( FunctionalTester $I ) { // 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' => '', @@ -44,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/vendor/composer/ClassLoader.php b/vendor/composer/ClassLoader.php index 95f7e0978..fce8549f0 100644 --- a/vendor/composer/ClassLoader.php +++ b/vendor/composer/ClassLoader.php @@ -279,7 +279,7 @@ public function isClassMapAuthoritative() */ public function setApcuPrefix($apcuPrefix) { - $this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null; + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; } /** diff --git a/wp-graphql-woocommerce.php b/wp-graphql-woocommerce.php index 3ce42aa65..4bec32d81 100644 --- a/wp-graphql-woocommerce.php +++ b/wp-graphql-woocommerce.php @@ -25,6 +25,9 @@ * 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'; }