-
Notifications
You must be signed in to change notification settings - Fork 0
150 lines (139 loc) · 5.52 KB
/
php-unit.yml
File metadata and controls
150 lines (139 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# This workflow runs PHP unit tests on the specified PHP version with optional PHP extensions.
# It sets up PHP, installs code dependencies, and runs PHPUnit as a testing tool.
# Additional dependencies/environment variables can be cloned/set as specified in the inputs.
# The phpunit command uses a bootstrap file if the PHPUNIT_BOOTSTRAP environment variable is set.
# PHPUnit problem matchers are added to improve error reporting in the workflow logs.
on:
workflow_call:
inputs:
php-version:
type: string
required: true
description: 'PHP versions to test against.'
php-extensions:
type: string
description: 'PHP extensions to install, comma separated list of strings.'
dependencies:
type: string
description: 'Dependencies to install, JSON encoded map of path => repository url.'
default: '{}'
env:
type: string
description: 'Environment variables to set, JSON encoded map of name => value.'
default: '{}'
databases:
type: boolean
description: 'Enable database support for unit tests.'
phpunit-version:
type: string
description: 'PHPUnit version to use.'
default: 'latest'
setup-commands:
description: 'Custom setup commands (one per line)'
type: string
working-directory:
type: string
default: .
jobs:
test:
name: PHPUnit ${{ inputs.php-version }}
runs-on: ubuntu-latest
steps:
- uses: Icinga/github-actions/.github/actions/php-and-dependencies@main
name: Setup PHP and dependencies
env: &phpunit-env
ICINGAWEB_TEST_MYSQL_PORT: ${{ job.services.mysql.ports['3306'] }}
ICINGAWEB_TEST_PGSQL_PORT: ${{ job.services.pgsql.ports['5432'] }}
MYSQL_TESTDB_HOST: 127.0.0.1
MYSQL_TESTDB_PORT: ${{ job.services.mysql.ports['3306'] }}
MYSQL_TESTDB: icinga_unittest
MYSQL_TESTDB_USER: icinga_unittest
MYSQL_TESTDB_PASSWORD: icinga_unittest
PGSQL_TESTDB_HOST: 127.0.0.1
PGSQL_TESTDB_PORT: ${{ job.services.pgsql.ports['5432'] }}
PGSQL_TESTDB: icinga_unittest
PGSQL_TESTDB_USER: icinga_unittest
PGSQL_TESTDB_PASSWORD: icinga_unittest
with:
php-version: ${{ inputs.php-version }}
php-extensions: ${{ inputs.php-extensions }}
dependencies: ${{ inputs.dependencies }}
env: ${{ inputs.env }}
php-tools: phpunit:${{ inputs.phpunit-version }}
setup-commands: ${{ inputs.setup-commands }}
working-directory: ${{ inputs.working-directory }}
# Register the GitHub Actions problem matcher installed by shivammathur/setup-php.
# https://github.com/shivammathur/setup-php/blob/2.36.0/src/configs/pm/phpunit.json
- name: Setup problem matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Run PHPUnit
env: *phpunit-env
# Unlike linting and static analysis, which report errors via the GitHub Actions API and
# therefore (have to) use relative paths, we can use working-directory here because
# PHPUnit provides absolute paths and GitHub Actions problem matchers work with absolute paths.
working-directory: ${{ inputs.working-directory }}
run: |
echo "::group::PHPUnit"
BLUE_BOLD='\033[1;34m'
NC='\033[0m' # No Color
log_cmd() {
printf "${BLUE_BOLD}"
printf '%s ' "${@}"
printf "${NC}\n"
}
run_cmd() {
log_cmd "$@"
"$@"
}
args=()
has_bootstrap=false
if [ -f phpunit.xml ]; then
# Detect if phpunit.xml defines a bootstrap attribute
if grep -q 'bootstrap=' phpunit.xml 2>/dev/null; then
has_bootstrap=true
fi
else
test_dirs=$(find . -mindepth 1 -maxdepth 1 -type d -path './test*')
if [ -z "${test_dirs}" ]; then
echo "No test directories found, skipping PHPUnit"
exit 0
fi
args=(--colors=auto --display-deprecations --display-notices --display-warnings "${test_dirs[@]}")
fi
# Only apply PHPUNIT_BOOTSTRAP if defined and no bootstrap in phpunit.xml
if [ -n "${PHPUNIT_BOOTSTRAP}" ] && [ "${has_bootstrap}" = false ]; then
run_cmd phpunit --bootstrap "${PHPUNIT_BOOTSTRAP}" "${args[@]}"
else
run_cmd phpunit "${args[@]}"
fi
echo "::endgroup::"
services:
mysql:
# Service will not be started if image is empty.
image: ${{ inputs.databases && 'mariadb' || '' }}
env:
MARIADB_DATABASE: icinga_unittest
MARIADB_USER: icinga_unittest
MARIADB_PASSWORD: icinga_unittest
MARIADB_ROOT_PASSWORD: icinga_unittest
options: >-
--health-cmd "mariadb-admin ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 3306/tcp
pgsql:
# Service will not be started if image is empty.
image: ${{ inputs.databases && 'postgres' || '' }}
env:
POSTGRES_DB: icinga_unittest
POSTGRES_USER: icinga_unittest
POSTGRES_PASSWORD: icinga_unittest
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432/tcp