|
| 1 | +name: Reusable PHPUnit Test |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + job-name: |
| 7 | + description: Name of the job to appear in GitHub UI |
| 8 | + type: string |
| 9 | + required: true |
| 10 | + php-version: |
| 11 | + description: The PHP version the workflow should run |
| 12 | + type: string |
| 13 | + required: true |
| 14 | + job-id: |
| 15 | + description: Job ID to be used as part of cache key and artifact name |
| 16 | + type: string |
| 17 | + required: false |
| 18 | + db-platform: |
| 19 | + description: The database platform to be tested |
| 20 | + type: string |
| 21 | + required: false |
| 22 | + mysql-version: |
| 23 | + description: Version of the mysql Docker image |
| 24 | + type: string |
| 25 | + required: false |
| 26 | + group-name: |
| 27 | + description: The @group to test |
| 28 | + type: string |
| 29 | + required: false |
| 30 | + enable-artifact-upload: |
| 31 | + description: Whether artifact uploading of coverage results should be enabled |
| 32 | + type: boolean |
| 33 | + required: false |
| 34 | + enable-coverage: |
| 35 | + description: Whether coverage should be enabled |
| 36 | + type: boolean |
| 37 | + required: false |
| 38 | + enable-profiling: |
| 39 | + description: Whether slow tests should be profiled |
| 40 | + type: boolean |
| 41 | + required: false |
| 42 | + extra-extensions: |
| 43 | + description: Additional PHP extensions that are needed to be enabled |
| 44 | + type: string |
| 45 | + required: false |
| 46 | + extra-composer-options: |
| 47 | + description: Additional Composer options that should be appended to the `composer update` call |
| 48 | + type: string |
| 49 | + required: false |
| 50 | + extra-phpunit-options: |
| 51 | + description: Additional PHPUnit options that should be appended to the `vendor/bin/phpunit` call |
| 52 | + type: string |
| 53 | + required: false |
| 54 | + |
| 55 | +env: |
| 56 | + NLS_LANG: 'AMERICAN_AMERICA.UTF8' |
| 57 | + NLS_DATE_FORMAT: 'YYYY-MM-DD HH24:MI:SS' |
| 58 | + NLS_TIMESTAMP_FORMAT: 'YYYY-MM-DD HH24:MI:SS' |
| 59 | + NLS_TIMESTAMP_TZ_FORMAT: 'YYYY-MM-DD HH24:MI:SS' |
| 60 | + |
| 61 | +jobs: |
| 62 | + tests: |
| 63 | + name: ${{ inputs.job-name }} |
| 64 | + runs-on: ubuntu-22.04 |
| 65 | + |
| 66 | + # Service containers cannot be extracted to caller workflows yet |
| 67 | + services: |
| 68 | + mysql: |
| 69 | + image: mysql:${{ inputs.mysql-version || '8.0' }} |
| 70 | + env: |
| 71 | + MYSQL_ALLOW_EMPTY_PASSWORD: yes |
| 72 | + MYSQL_DATABASE: test |
| 73 | + ports: |
| 74 | + - 3306:3306 |
| 75 | + options: >- |
| 76 | + --health-cmd="mysqladmin ping" |
| 77 | + --health-interval=10s |
| 78 | + --health-timeout=5s |
| 79 | + --health-retries=3 |
| 80 | +
|
| 81 | + postgres: |
| 82 | + image: postgres |
| 83 | + env: |
| 84 | + POSTGRES_USER: postgres |
| 85 | + POSTGRES_PASSWORD: postgres |
| 86 | + POSTGRES_DB: test |
| 87 | + ports: |
| 88 | + - 5432:5432 |
| 89 | + options: >- |
| 90 | + --health-cmd=pg_isready |
| 91 | + --health-interval=10s |
| 92 | + --health-timeout=5s |
| 93 | + --health-retries=3 |
| 94 | +
|
| 95 | + mssql: |
| 96 | + image: mcr.microsoft.com/mssql/server:2022-latest |
| 97 | + env: |
| 98 | + SA_PASSWORD: 1Secure*Password1 |
| 99 | + ACCEPT_EULA: Y |
| 100 | + MSSQL_PID: Developer |
| 101 | + ports: |
| 102 | + - 1433:1433 |
| 103 | + options: >- |
| 104 | + --health-cmd="/opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -U sa -P 1Secure*Password1 -Q 'SELECT @@VERSION'" |
| 105 | + --health-interval=10s |
| 106 | + --health-timeout=5s |
| 107 | + --health-retries=3 |
| 108 | +
|
| 109 | + oracle: |
| 110 | + image: gvenzl/oracle-xe:21 |
| 111 | + env: |
| 112 | + ORACLE_RANDOM_PASSWORD: true |
| 113 | + APP_USER: ORACLE |
| 114 | + APP_USER_PASSWORD: ORACLE |
| 115 | + ports: |
| 116 | + - 1521:1521 |
| 117 | + options: >- |
| 118 | + --health-cmd healthcheck.sh |
| 119 | + --health-interval 20s |
| 120 | + --health-timeout 10s |
| 121 | + --health-retries 10 |
| 122 | +
|
| 123 | + redis: |
| 124 | + image: redis |
| 125 | + ports: |
| 126 | + - 6379:6379 |
| 127 | + options: >- |
| 128 | + --health-cmd "redis-cli ping" |
| 129 | + --health-interval=10s |
| 130 | + --health-timeout=5s |
| 131 | + --health-retries=3 |
| 132 | +
|
| 133 | + memcached: |
| 134 | + image: memcached:1.6-alpine |
| 135 | + ports: |
| 136 | + - 11211:11211 |
| 137 | + |
| 138 | + steps: |
| 139 | + - name: Create database for MSSQL Server |
| 140 | + if: ${{ inputs.db-platform == 'SQLSRV' }} |
| 141 | + run: sqlcmd -S 127.0.0.1 -U sa -P 1Secure*Password1 -Q "CREATE DATABASE test" |
| 142 | + |
| 143 | + - name: Install latest ImageMagick |
| 144 | + if: ${{ contains(inputs.extra-extensions, 'imagick') }} |
| 145 | + run: | |
| 146 | + sudo apt-get update |
| 147 | + sudo apt-get install --reinstall libgs9-common fonts-noto-mono libgs9:amd64 libijs-0.35:amd64 fonts-urw-base35 ghostscript poppler-data libjbig2dec0:amd64 gsfonts libopenjp2-7:amd64 fonts-droid-fallback fonts-dejavu-core |
| 148 | + sudo apt-get install -y imagemagick |
| 149 | + sudo apt-get install --fix-broken |
| 150 | +
|
| 151 | + - name: Checkout |
| 152 | + uses: actions/checkout@v3 |
| 153 | + |
| 154 | + - name: Setup PHP |
| 155 | + uses: shivammathur/setup-php@v2 |
| 156 | + with: |
| 157 | + php-version: ${{ inputs.php-version }} |
| 158 | + tools: composer |
| 159 | + extensions: gd, ${{ inputs.extra-extensions }} |
| 160 | + coverage: ${{ env.COVERAGE_DRIVER }} |
| 161 | + env: |
| 162 | + COVERAGE_DRIVER: ${{ inputs.enable-coverage && 'xdebug' || 'none' }} |
| 163 | + |
| 164 | + - name: Setup global environment variables |
| 165 | + run: | |
| 166 | + echo "COMPOSER_CACHE_FILES_DIR=$(composer config cache-files-dir)" >> $GITHUB_ENV |
| 167 | + echo "ARTIFACT_NAME=${{ inputs.job-id || github.job }}-php-${{ inputs.php-version }}-db-${{ inputs.db-platform || 'none' }}" >> $GITHUB_ENV |
| 168 | +
|
| 169 | + - name: Cache dependencies |
| 170 | + uses: actions/cache@v3 |
| 171 | + with: |
| 172 | + path: ${{ env.COMPOSER_CACHE_FILES_DIR }} |
| 173 | + key: ${{ inputs.job-id || github.job }}-php-${{ inputs.php-version }}-db-${{ inputs.db-platform || 'none' }}-${{ hashFiles('**/composer.*') }} |
| 174 | + restore-keys: | |
| 175 | + ${{ inputs.job-id || github.job }}-php-${{ inputs.php-version }}-db-${{ inputs.db-platform || 'none' }}- |
| 176 | + ${{ inputs.job-id || github.job }}-php-${{ inputs.php-version }}- |
| 177 | + ${{ inputs.job-id || github.job }}- |
| 178 | +
|
| 179 | + - name: Cache PHPUnit's static analysis cache |
| 180 | + if: ${{ inputs.enable-artifact-upload }} |
| 181 | + uses: actions/cache@v3 |
| 182 | + with: |
| 183 | + path: build/.phpunit.cache/code-coverage |
| 184 | + key: phpunit-code-coverage-${{ hashFiles('**/phpunit.*') }} |
| 185 | + restore-keys: | |
| 186 | + phpunit-code-coverage- |
| 187 | +
|
| 188 | + - name: Install dependencies |
| 189 | + run: | |
| 190 | + composer config --global github-oauth.github.com ${{ secrets.GITHUB_TOKEN }} |
| 191 | + composer update --ansi ${{ inputs.extra-composer-options }} |
| 192 | +
|
| 193 | + - name: Compute additional PHPUnit options |
| 194 | + run: | |
| 195 | + echo "EXTRA_PHPUNIT_OPTIONS=${{ format('{0} {1} {2}', env.GROUP_OPTION, env.COVERAGE_OPTION, inputs.extra-phpunit-options) }}" >> $GITHUB_ENV |
| 196 | + env: |
| 197 | + COVERAGE_OPTION: ${{ inputs.enable-coverage && format('--coverage-php build/cov/coverage-{0}.cov', env.ARTIFACT_NAME) || '--no-coverage' }} |
| 198 | + GROUP_OPTION: ${{ inputs.group-name && format('--group {0}', inputs.group-name) || '' }} |
| 199 | + |
| 200 | + - name: Run tests |
| 201 | + run: script -e -c "vendor/bin/phpunit --color=always ${{ env.EXTRA_PHPUNIT_OPTIONS }}" |
| 202 | + env: |
| 203 | + DB: ${{ inputs.db-platform }} |
| 204 | + TACHYCARDIA_MONITOR_GA: ${{ inputs.enable-profiling && 'enabled' || '' }} |
| 205 | + TERM: xterm-256color |
| 206 | + |
| 207 | + - name: Upload coverage results as artifact |
| 208 | + if: ${{ inputs.enable-artifact-upload }} |
| 209 | + uses: actions/upload-artifact@v3 |
| 210 | + with: |
| 211 | + name: ${{ env.ARTIFACT_NAME }} |
| 212 | + path: build/cov/coverage-${{ env.ARTIFACT_NAME }}.cov |
| 213 | + if-no-files-found: error |
| 214 | + retention-days: 1 |
0 commit comments