|
| 1 | +#!/bin/bash |
| 2 | +# This file updates tests according to the phpunit library used for current php version, or php version in 1st argument. |
| 3 | +# Usage: |
| 4 | +# update-tests - to update tests according to the phpunit library used for current php version. |
| 5 | +# update-tests x.x - to update tests according to the phpunit library used for specific php version x.x, where x.x = 5.6|7.0|7.1|7.2|7.3|7.4|8.0. |
| 6 | + |
| 7 | +# Directory with phpunit tests. |
| 8 | +TEST_DIR="tests" |
| 9 | + |
| 10 | +if grep -q Microsoft /proc/version; then |
| 11 | + DEV_MODE=$(cmd.exe /c echo %COMPOSER_DEV_MODE% | sed -nr 's/\r//p') |
| 12 | +else |
| 13 | + DEV_MODE=$COMPOSER_DEV_MODE |
| 14 | +fi |
| 15 | + |
| 16 | +if [[ $1 == '' && $DEV_MODE != '1' ]]; then |
| 17 | + echo "Script works with composer in dev mode only." |
| 18 | + exit 0 |
| 19 | +fi |
| 20 | + |
| 21 | +if [[ $1 == '' ]]; then |
| 22 | + PHP_VERSION=$(php -v | sed -nr "s/PHP ([^.]*?\.[^.]*?)\..*/\1/p") |
| 23 | +else |
| 24 | + if [[ $1 == 'revert' ]]; then |
| 25 | + # Restore test files to the current branch version. |
| 26 | + git checkout -- $TEST_DIR |
| 27 | + echo "Tests reverted to the initial state." |
| 28 | + exit 0 |
| 29 | + fi |
| 30 | + PHP_VERSION=$1 |
| 31 | +fi |
| 32 | + |
| 33 | +echo "PHP_VERSION: $PHP_VERSION" |
| 34 | + |
| 35 | +VERSION_FILE="vendor/phpunit/phpunit/src/Runner/Version.php" |
| 36 | +CURRENT_PHP_UNIT='' |
| 37 | + |
| 38 | +RESULT=$(test -f $VERSION_FILE && sed -nr "s/.*new Version.+'(.+\..+)\..*'.*/\1/p" $VERSION_FILE) |
| 39 | + |
| 40 | +if [[ $? == 0 ]]; then |
| 41 | + CURRENT_PHP_UNIT=$RESULT |
| 42 | + echo "CURRENT_PHP_UNIT: $CURRENT_PHP_UNIT" |
| 43 | +else |
| 44 | + echo "CURRENT_PHP_UNIT: Not found." |
| 45 | +fi |
| 46 | + |
| 47 | +if [[ $PHP_VERSION == '5.6' ]]; then |
| 48 | + PHP_UNIT='5.7' |
| 49 | +elif [[ $PHP_VERSION == '7.0' ]]; then |
| 50 | + PHP_UNIT='6.5' |
| 51 | +elif [[ $PHP_VERSION == '7.1' ]]; then |
| 52 | + PHP_UNIT='7.5' |
| 53 | +elif [[ $PHP_VERSION == '7.2' ]]; then |
| 54 | + PHP_UNIT='8.5' |
| 55 | +elif [[ $PHP_VERSION == '7.3' || $PHP_VERSION == '7.4' || $PHP_VERSION == '8.0' ]]; then |
| 56 | + PHP_UNIT='9.5' |
| 57 | +fi |
| 58 | + |
| 59 | +if [[ $PHP_UNIT == '' ]]; then |
| 60 | + echo "Wrong PHP version: $PHP_VERSION" |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +if [[ $1 == '' && $CURRENT_PHP_UNIT == "$PHP_UNIT" ]]; then |
| 65 | + # Do nothing if current version of phpunit is the same as required. Important on CI. |
| 66 | + # Anytime force update available specifying the first argument like 'update-phpunit 7.0' |
| 67 | + echo "Nothing to do with phpunit." |
| 68 | + exit 0 |
| 69 | +fi |
| 70 | + |
| 71 | +# Restore test files to the current branch version. |
| 72 | +git checkout -- $TEST_DIR |
| 73 | + |
| 74 | +if [[ $PHP_UNIT == '5.7' || $PHP_UNIT == '6.5' || $PHP_UNIT == '7.5' ]]; then |
| 75 | + echo "Preparing tests for phpunit-$PHP_UNIT" |
| 76 | + find $TEST_DIR -type f -exec sed -i "s/: void//g" {} \; |
| 77 | +fi |
| 78 | + |
| 79 | +exit 0 |
0 commit comments