├── LICENSE.md ├── README.md └── setup-phpunit.sh /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2019 Andy Fragen 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Setup PHPUnit for Local 5.x or greater 2 | 3 | This is an updated version of gist [setup-phpunit.sh](https://gist.github.com/keesiemeijer/a888f3d9609478b310c2d952644891ba) 4 | 5 | I converted it to a repository to more easily update it. I couldn't seem to `git push` to the gist. 6 | 7 | ## Installation 8 | 9 | If you use a Mac please install [Homebrew](https://brew.sh) using the following command. 10 | 11 | `/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"` 12 | 13 | Install the `setup-phpunit.sh` script using the following. 14 | 15 | `curl -o setup-phpunit.sh https://raw.githubusercontent.com/afragen/setup-phpunit/master/setup-phpunit.sh` 16 | 17 | ## Usage 18 | 19 | `setup-phpunit.sh` is meant to reside in `/app` and be run from `/app/public` as `bash ../setup-phpunit.sh` or from `/app` as `bash setup-phpunit.sh` 20 | 21 | You must use `/usr/local/bin/phpunit` for your testing. 22 | 23 | ## What It Does 24 | 25 | This is meant as a primary replacement for the `install-wp-tests.sh` script created from plugin scaffolding with `wp scaffold plugin-tests your_plugin`. 26 | 27 | * Installs correct version of PHPUnit, with option to specify version. 28 | * Installs specified version of WordPress and WordPress Test Suite, with option to specify version. 29 | * Create a correct version of `wp-tests-config.php` for your installation. 30 | * Creates a test database. 31 | * Cleans up afterwards. 32 | -------------------------------------------------------------------------------- /setup-phpunit.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # =============================================================================== 4 | # Script to install PHPUnit in the Local 5.x by Flywheel Mac app 5 | # Modified from Kees Meijer's fabulous gist, https://gist.github.com/keesiemeijer/a888f3d9609478b310c2d952644891ba 6 | # These packages are installed 7 | # 8 | # PHPUnit, curl wget, rsync, git, subversion and composer. 9 | # 10 | # WordPress is installed in the `/tmp/wordpress` directory for use by PHPUnit. 11 | # The WordPress test suite is installed in the `/tmp/wordpress-tests-lib` directory. 12 | # 13 | # The WordPress and WP Test Suite paths are added to the ~/.bashrc file as environment 14 | # variables $WP_CORE_DIR and $WP_TESTS_DIR. 15 | # 16 | # That way plugins can make use of them for unit testing. Plugins that have their 17 | # tests scaffolded by WP-CLI also makes use of them. VVV also adds these enviroment 18 | # variables to the ~/.bashrc file by default. 19 | # 20 | # You only have to run this script once. PHPUnit (and the other packages) are 21 | # still available next time you ssh into your site. 22 | # You must use `/usr/local/bin/phpunit` for your testing. 23 | # 24 | # To update WordPress and the WP Test Suite re-run this script. 25 | # Use options to install specific versions for PHPUnit, WordPress or the WP_UnitTestCase. 26 | # 27 | # Note: This script doesn't install the packages globally in the Local by Flywheel app 28 | # Packages are only installed for the site where you've run this script. 29 | # =============================================================================== 30 | 31 | # =============================================================================== 32 | # Instructions 33 | # 34 | # 1 - Download this file (setup-phpunit.sh) inside your site's /app folder 35 | # curl -o setup-phpunit.sh https://raw.githubusercontent.com/afragen/setup-phpunit/master/setup-phpunit.sh 36 | # 37 | # 2 - Right click your site in the Local App and click Open Site SSH 38 | # A new terminal window will open 39 | # 40 | # 3 - Go to your site's /app folder: 41 | # cd /app 42 | # 43 | # 4 - Run this script 44 | # bash setup-phpunit.sh 45 | # 46 | # 5 - Reload the .bashrc file 47 | # source ~/.bashrc 48 | # 49 | # 6 - Check if PHPUnit is installed 50 | # phpunit --version 51 | # 52 | # =============================================================================== 53 | 54 | # =============================================================================== 55 | # Options 56 | # 57 | # Without options this script installs/updates PHPUnit, WordPress and the WP test suite. 58 | # 59 | # Install a specific PHPUnit version with the --phpunit-version option. 60 | # 61 | # bash setup-phpunit.sh --phpunit-version=7 62 | # 63 | # Install a specific WordPress version with the --wp-version option. This option 64 | # accepts a version number, 'latest', 'trunk' or 'nightly'. Default 'latest' 65 | # 66 | # bash setup-phpunit.sh --wp-version=5.0 67 | # 68 | # Install a specific WordPress Test Suite with the --wp-ts-version option. This option 69 | # accepts a version number, 'latest', 'trunk' or 'nightly'. Default 'latest' 70 | # 71 | # bash setup-phpunit.sh --wp-ts-version=trunk 72 | # 73 | # Update all packages (wget, curl etc) installed by this script with the --update-packages option. 74 | # 75 | # bash setup-phpunit.sh --update-packages 76 | # 77 | # Use the --help or -? option to see more information about this script. 78 | # 79 | # bash setup-phpunit.sh --help 80 | # 81 | # =============================================================================== 82 | 83 | # =============================================================================== 84 | # Default PHPUnit version 85 | # 86 | # The default installed PHPUnit version is similar to versions used in 87 | # WordPress travis.yaml file in trunk. 88 | # 89 | # See https://core.trac.wordpress.org/browser/trunk/.travis.yml#L64 90 | # 91 | # PHPUnit version 7 is installed for PHP version 7.1 and above 92 | # PHPUnit version 5 is installed for PHP version 7.0 93 | # PHPUnit version 4 for all other PHP versions 94 | # 95 | # See ticket https://core.trac.wordpress.org/ticket/39822 96 | # See ticket https://core.trac.wordpress.org/ticket/43218 97 | # 98 | # Run the command with a version if you need to test with a specific PHPUnit version 99 | # 100 | # bash setup-phpunit.sh --phpunit-version=7 101 | # 102 | # This example will install the latest PHPUnit from version 7 (e.g. 7.5.3) 103 | # 104 | # Available PHPUnit versions can be found here. 105 | # https://phar.phpunit.de 106 | # 107 | # PHPUnit versions compatible with PHP versions can be found here. 108 | # https://phpunit.de/supported-versions.html 109 | # 110 | # =============================================================================== 111 | 112 | # Strings used in error messages. 113 | readonly QUIT="Stopping script..." 114 | readonly CONNECTION="Make sure you're connected to the internet." 115 | readonly RED='\033[0;31m' # Red color. 116 | readonly RESET='\033[0m' # No color. 117 | 118 | # /app/public dir for Local installation. 119 | DIR=$(pwd) 120 | BASE_DIR=$(echo $DIR | grep -o "app.*$") 121 | if [[ "app/public" == $BASE_DIR ]]; then 122 | readonly LOCAL_PUBLIC=$DIR 123 | elif [[ "app" == $BASE_DIR ]]; then 124 | readonly LOCAL_PUBLIC=$DIR/public 125 | fi 126 | 127 | # Functions 128 | function download() { 129 | download=false 130 | if wget --spider "$1" >/dev/null 2>&1; then 131 | wget -q --show-progress -O "$2" "$1" && download=true 132 | 133 | # Check if file exists. 134 | if [[ -f "$2" && "$download" == true ]]; then 135 | return 0 136 | fi 137 | fi 138 | 139 | printf "${RED}WARNING${RESET} Could not download %s %s\n" "$1" "$CONNECTION" 140 | return 1 141 | } 142 | 143 | function download_test_suite() { 144 | local exit=0 145 | if wget --spider "https://develop.svn.wordpress.org/$1/tests/phpunit/includes/" >/dev/null 2>&1; then 146 | svn export --quiet --force "https://develop.svn.wordpress.org/$1/tests/phpunit/includes/" "/tmp/tmp-wordpress-tests-lib/includes/" 147 | svn export --quiet --force "https://develop.svn.wordpress.org/$1/tests/phpunit/data/" "/tmp/tmp-wordpress-tests-lib/data/" 148 | svn export --quiet --force "https://develop.svn.wordpress.org/$1/wp-tests-config-sample.php" "/tmp/tmp-wordpress-tests-lib/wp-tests-config.php" 149 | for path in includes data wp-tests-config.php; do 150 | # Check if path exists. 151 | [[ ! -e "/tmp/tmp-wordpress-tests-lib/$path" ]] && exit=1 152 | done 153 | if [[ 0 == "$exit" ]]; then 154 | return 0 155 | fi 156 | fi 157 | 158 | printf "${RED}WARNING${RESET} Could not download %s Test Suite. %s\n" "$2" "$CONNECTION" 159 | return 1 160 | } 161 | 162 | function packages_installed() { 163 | for file in wget curl svn rsync composer git; do 164 | bin=$(which $file) 165 | # Check if executable file. 166 | if ! [[ -f "$bin" && -x "$bin" ]]; then 167 | return 1 168 | fi 169 | done 170 | return 0 171 | } 172 | 173 | function clean_up_temp_files() { 174 | # Clean up files added by this script. 175 | [[ -d "/tmp/tmp-wordpress/" ]] && rm -rf "/tmp/tmp-wordpress/" 176 | [[ -d "/tmp/tmp-wordpress-tests-lib/" ]] && rm -rf "/tmp/tmp-wordpress-tests-lib/" 177 | [[ -f "/tmp/my.cnf" ]] && rm -f "/tmp/my.cnf" 178 | } 179 | 180 | function exit_script() { 181 | clean_up_temp_files 182 | exit 1 183 | } 184 | 185 | # Get arguments. 186 | for arg in "$@"; do 187 | if [[ "$arg" =~ ^- ]]; then 188 | # Argument start with a dash. 189 | case "$arg" in 190 | --phpunit-version=*) PHPUNIT_VERSION=${arg#"--phpunit-version="} ;; 191 | --wp-version=*) WP_VERSION=${arg#"--wp-version="} ;; 192 | --wp-ts-version=*) WP_TS_VERSION=${arg#"--wp-ts-version="} ;; 193 | --update-packages*) UPDATE_PACKAGES=true ;; 194 | -? | --help) 195 | printf "Install PHPUnit in the Local by Flywheel Mac app\n\n" 196 | printf "Usage:\n" 197 | printf "\tbash setup-phpunit.sh [option...]\n\n" 198 | printf "Example:\n" 199 | printf "\tbash setup-phpunit.sh --phpunit-version=6 --wp-version=trunk\n\n" 200 | printf "Options:\n" 201 | printf -- "\t--phpunit-version PHPUnit version to install\n" 202 | printf -- "\t--wp-version WordPress version to install\n" 203 | printf -- "\t Accepts a version number, 'latest', 'trunk' or 'nightly'. Default 'latest'\n" 204 | printf -- "\t--wp-ts-version WordPress Test Suite version to install\n" 205 | printf -- "\t Accepts a version number, 'latest', 'trunk' or 'nightly'. Default --wp-version option\n" 206 | printf -- "\t--update-packages Update all packages installed by this script\n" 207 | printf -- "\t Updates curl wget, rsync, git, subversion and composer\n" 208 | printf -- "\t-?|--help Display information about this script\n\n" 209 | exit 0 210 | ;; 211 | *) 212 | printf "Unknown option: %s.\nUse \"bash setup-phpunit.sh --help\" to see all options\n%s\n" "$arg" "$QUIT_MSG" 213 | exit_script 214 | ;; 215 | esac 216 | else 217 | # Argument doesn't start with a dash. 218 | printf "Unknown option: %s.\nUse \"bash setup-phpunit.sh --help\" to see all options\n%s\n" "$arg" "$QUIT_MSG" 219 | exit_script 220 | fi 221 | done 222 | 223 | # Can use $(uname) to determine OS type. 224 | if [[ 'Darwin' == $(uname) ]]; then 225 | OS_TYPE="MacOS" 226 | BREW_PATH=$(brew --prefix) 227 | else 228 | OS_TYPE="Linux/WSL" 229 | fi 230 | echo $OS_TYPE 231 | INSTALL_PACKAGES=false 232 | if ! packages_installed; then INSTALL_PACKAGES=true; fi 233 | [[ -z "$UPDATE_PACKAGES" ]] && UPDATE_PACKAGES=false 234 | 235 | if [[ "$INSTALL_PACKAGES" == true || "$UPDATE_PACKAGES" == true ]]; then 236 | 237 | [[ "$INSTALL_PACKAGES" == true ]] && printf "Installing packages...\n" || printf "Updating packages...\n" 238 | 239 | if [[ "MacOS" == $OS_TYPE && "$INSTALL_PACKAGES" == true ]]; then 240 | xcode-select --install 241 | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" 242 | brew install wget 243 | brew install svn 244 | # Composer installed in Local's Site Shell. 245 | # brew install composer 246 | fi 247 | if ! [[ "MacOS" == $OS_TYPE ]]; then 248 | # Re-synchronize the package index files from their sources. 249 | apt-get update -y 250 | 251 | # Install packages. 252 | apt-get install -y wget subversion curl git rsync 253 | 254 | # Install composer. 255 | if [[ -f "/usr/bin/curl" && ! -f "/usr/local/bin/composer" ]]; then 256 | curl -sS https://getcomposer.org/installer | php 257 | mv composer.phar /usr/local/bin/composer || exit 258 | if [[ -f "$HOME/.bashrc" ]]; then 259 | printf "Adding .composer/vendor/bin to the PATH\n" 260 | echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >>"$HOME/.bashrc" 261 | fi 262 | else 263 | if [[ -f "/usr/local/bin/composer" ]]; then 264 | printf "Updating composer...\n" 265 | composer self-update || printf "${RED}WARNING${RESET} Could not update composer. %s\n" "$CONNECTION" 266 | fi 267 | fi 268 | fi 269 | fi 270 | 271 | # Re-check if all packages are installed. 272 | if ! packages_installed; then 273 | printf "${RED}ERROR${RESET} Missing packages. %s\n%s\n" "$CONNECTION" "$QUIT" 274 | exit_script 275 | fi 276 | 277 | # Get the current PHP version. 278 | PHP_VERSION=$(php -r "echo PHP_VERSION;") 279 | 280 | # Get first three characters from version 281 | readonly PHP_VERSION="${PHP_VERSION:0:3}" 282 | 283 | # https://phpunit.de/supported-versions.html 284 | # Currently WordPress only supports up to PHPUnit 7.x 285 | # https://make.wordpress.org/core/handbook/testing/automated-testing/phpunit/#setup 286 | # Set the PHPUnit version if needed. 287 | if [[ -z "$PHPUNIT_VERSION" ]]; then 288 | case "$PHP_VERSION" in 289 | 7.4 | 7.3 | 7.2) 290 | PHPUNIT_VERSION=7 291 | ;; 292 | 7.1 | 7.0) 293 | PHPUNIT_VERSION=6 294 | ;; 295 | 5.6) 296 | PHPUNIT_VERSION=4 297 | ;; 298 | *) 299 | PHPUNIT_VERSION=7 300 | ;; 301 | esac 302 | fi 303 | 304 | readonly PHPUNIT_VERSION="$PHPUNIT_VERSION" 305 | 306 | # Install PHPUnit. 307 | printf "Installing PHPUnit %s... \n" "$PHPUNIT_VERSION" 308 | if download "https://phar.phpunit.de/phpunit-$PHPUNIT_VERSION.phar" "phpunit-$PHPUNIT_VERSION.phar"; then 309 | chmod +x "phpunit-$PHPUNIT_VERSION.phar" 310 | mv -fv "phpunit-$PHPUNIT_VERSION.phar" /usr/local/bin/phpunit 311 | else 312 | printf "%s\n" "$QUIT" 313 | exit_script 314 | fi 315 | 316 | # Make .bashrc if not present. 317 | if [[ ! -f "$HOME/.bashrc" ]]; then 318 | echo "Creating ~/.bashrc" 319 | touch "$HOME/.bashrc" 320 | fi 321 | 322 | # Set WordPress environment variables. 323 | if [[ -f "$HOME/.bashrc" ]]; then 324 | 325 | # Get the variables. 326 | source "$HOME/.bashrc" 327 | 328 | if [[ -z "${WP_TESTS_DIR}" ]]; then 329 | printf "Setting WP_TESTS_DIR environment variable\n" 330 | echo 'export WP_TESTS_DIR=/tmp/wordpress-tests-lib' >>"$HOME/.bashrc" 331 | fi 332 | if [[ -z "${WP_CORE_DIR}" ]]; then 333 | printf "Setting WP_CORE_DIR environment variable\n" 334 | echo 'export WP_CORE_DIR=/tmp/wordpress' >>"$HOME/.bashrc" 335 | fi 336 | 337 | # Get the new variables. 338 | source "$HOME/.bashrc" 339 | fi 340 | 341 | if [[ -z "$WP_CORE_DIR" || -z "$WP_TESTS_DIR" ]]; then 342 | printf "${RED}ERROR${RESET} The WordPress directories for PHPUnit are not set\n%s\n" "$QUIT" 343 | exit_script 344 | fi 345 | 346 | # Delete tmp files (if they exist). 347 | clean_up_temp_files 348 | 349 | # Create tmp directories. 350 | mkdir "/tmp/tmp-wordpress/" || exit 351 | mkdir "/tmp/tmp-wordpress-tests-lib/" || exit 352 | 353 | # Create core and tests directories (if needed). 354 | [[ -d "$WP_CORE_DIR" ]] || mkdir "$WP_CORE_DIR" || exit 355 | [[ -d "$WP_TESTS_DIR" ]] || mkdir "$WP_TESTS_DIR" || exit 356 | 357 | cd "$WP_CORE_DIR" || exit 358 | 359 | # Set default WordPress version. 360 | [[ -z "$WP_VERSION" ]] && WP_VERSION='latest' 361 | 362 | # Get the latest WordPress version from API. 363 | #readonly WP_LATEST=$(wget -q -O - "https://api.wordpress.org/core/version-check/1.5/" | head -n 4 | tail -n 1) 364 | 365 | # http serves a single offer, whereas https serves multiple. we only want one. 366 | readonly WP_LATEST=$(wget -q -O - "http://api.wordpress.org/core/version-check/1.7/" | grep -o '"version":"[^"]*"' | head -1 | tr -d '"' | awk -F: '{print $2}') 367 | 368 | if [[ 'latest' == "$WP_VERSION" ]]; then 369 | WP_VERSION="$WP_LATEST" 370 | if [[ -z "$WP_LATEST" ]]; then 371 | printf "${RED}ERROR${RESET} Could not get latest WordPress version from api.wordpress.org. %s\n%s\n" "$CONNECTION" "$QUIT" 372 | exit_script 373 | fi 374 | fi 375 | 376 | # Set WordPress version. 377 | readonly WP_VERSION="$WP_VERSION" 378 | 379 | # Set default test suite version. 380 | [[ -z "$WP_TS_VERSION" ]] && WP_TS_VERSION="$WP_VERSION" 381 | 382 | # Install WordPress. 383 | if [[ 'trunk' == "$WP_VERSION" ]]; then 384 | printf "Installing WordPress trunk... \n" 385 | svn export --quiet --force "https://develop.svn.wordpress.org/trunk/src/" "/tmp/tmp-wordpress/" 386 | rsync -a --delete "/tmp/tmp-wordpress/" "$WP_CORE_DIR" 387 | elif [[ 'nightly' == "$WP_VERSION" ]]; then 388 | printf "Installing WordPress nightly... \n" 389 | if download "https://wordpress.org/nightly-builds/wordpress-latest.zip" "/tmp/wordpress-latest.zip"; then 390 | unzip -o -q "/tmp/wordpress-latest.zip" -d "/tmp/tmp-wordpress/" 391 | rsync -a --delete "/tmp/tmp-wordpress/wordpress/" "$WP_CORE_DIR" 392 | fi 393 | else 394 | printf "Installing WordPress %s... \n" "$WP_VERSION" 395 | if download "https://wordpress.org/wordpress-$WP_VERSION.tar.gz" "/tmp/wordpress.tar.gz"; then 396 | tar --strip-components=1 -zxmf "/tmp/wordpress.tar.gz" -C "/tmp/tmp-wordpress/" 397 | rsync -a --delete "/tmp/tmp-wordpress/" "$WP_CORE_DIR" 398 | fi 399 | fi 400 | 401 | if [[ 'trunk' == "$WP_TS_VERSION" || 'nightly' == "$WP_TS_VERSION" ]]; then 402 | TS_ARCHIVE="trunk" 403 | elif [[ $WP_TS_VERSION == 'latest' ]]; then 404 | TS_ARCHIVE="tags/$WP_LATEST" 405 | WP_TS_VERSION="$WP_LATEST" 406 | else 407 | TS_ARCHIVE="tags/$WP_TS_VERSION" 408 | fi 409 | 410 | # Install WP test suite. 411 | printf "Installing WordPress %s Test Suite...\n" "$WP_TS_VERSION" 412 | if download_test_suite "$TS_ARCHIVE" "$WP_TS_VERSION"; then 413 | rsync -a --delete "/tmp/tmp-wordpress-tests-lib/" "$WP_TESTS_DIR" 414 | else 415 | if [[ 'trunk' == "$TS_ARCHIVE" ]]; then 416 | printf "%s\n" "$QUIT" 417 | exit_script 418 | fi 419 | 420 | printf "Installing Test Suite from trunk...\n" 421 | if download_test_suite "trunk" "trunk"; then 422 | rsync -a --delete "/tmp/tmp-wordpress-tests-lib/" "$WP_TESTS_DIR" 423 | else 424 | printf "%s\n" "$QUIT" 425 | exit_script 426 | fi 427 | fi 428 | 429 | # Update credentials in the wp-tests-config.php file. 430 | if [[ -f "$WP_TESTS_DIR/wp-tests-config.php" ]]; then 431 | printf "Updating wp-tests-config.php...\n" 432 | if [[ $(uname -s) == 'Darwin' ]]; then 433 | ioption='-i .bak' 434 | else 435 | ioption='-i' 436 | fi 437 | 438 | sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR/wp-tests-config.php" 439 | sed $ioption "s/youremptytestdbnamehere/wordpress_test/" "$WP_TESTS_DIR/wp-tests-config.php" 440 | sed $ioption "s/yourusernamehere/root/" "$WP_TESTS_DIR/wp-tests-config.php" 441 | sed $ioption "s/yourpasswordhere/root/" "$WP_TESTS_DIR/wp-tests-config.php" 442 | fi 443 | 444 | # VVV has the tests config outside the $WP_TESTS_DIR dir. 445 | if [[ -f "$WP_TESTS_DIR/wp-tests-config.php" ]]; then 446 | cp -v "$WP_TESTS_DIR/wp-tests-config.php" "/tmp/wp-tests-config.php" 447 | fi 448 | 449 | # Make tests config for develop.git.wordpress.org. 450 | if [[ -f "$LOCAL_PUBLIC/wp-tests-config-sample.php" ]]; then 451 | printf "Create credentials for wp-tests-config.php...\n" 452 | sed -e 's/yourusernamehere/root/g' -e 's/yourpasswordhere/root/g' -e 's/youremptytestdbnamehere/wordpress_test/g' $LOCAL_PUBLIC/wp-tests-config-sample.php >$LOCAL_PUBLIC/wp-tests-config.php 453 | fi 454 | 455 | # If tests config not present copy to Local's WP root. 456 | if [[ ! -f "$LOCAL_PUBLIC/wp-tests-config.php" ]]; then 457 | cp -v "$WP_TESTS_DIR/wp-tests-config.php" "$LOCAL_PUBLIC" 458 | fi 459 | 460 | # Install database if it doesn't exist. 461 | printf "Checking if database wordpress_test exists\n" 462 | touch /tmp/my.cnf 463 | 464 | # Suppress password warnings. It silly I know :-) 465 | printf "[client]\npassword=root\nuser=root" >"/tmp/my.cnf" 466 | 467 | # Check if database exists. 468 | database="" 469 | if ! [[ "mysqlshow --version" ]]; then 470 | database=$(mysqlshow --defaults-file="/tmp/my.cnf" wordpress_test | grep -v Wildcard | grep -o wordpress_test) 471 | elif $(mysql -e 'use wordpress_test'); then 472 | database="wordpress_test" 473 | fi 474 | 475 | if ! [[ "wordpress_test" == "$database" ]]; then 476 | printf "Creating database wordpress_test\n" 477 | SOCKET=$(mysqld --verbose --help | grep ^socket | awk '{print $2, $3, $4}') 478 | PORT=$(mysqld --verbose --help | grep ^port | head -1 | awk '{print $2}') 479 | #mysqladmin --defaults-file="/tmp/my.cnf" create "wordpress_test" --host="localhost" --port="$PORT" --socket="$SOCKET" 480 | mysqladmin create "wordpress_test" 481 | else 482 | printf "Database wordpress_test already exists\n" 483 | fi 484 | 485 | # Cleanup files. 486 | clean_up_temp_files 487 | 488 | printf "\nFinished setting up packages\n\n" 489 | --------------------------------------------------------------------------------