-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpunit.inc.bash
More file actions
executable file
·195 lines (173 loc) · 6.21 KB
/
phpunit.inc.bash
File metadata and controls
executable file
·195 lines (173 loc) · 6.21 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Note the phpUnitQuickTests=$phpUnitQuickTests
# this sets a config variable which you can then use
# to allow tests to run less thoroughly but more quickly
# @see https://github.com/edmondscommerce/phpqa#quick-tests
# Check for bootstrap file and create a placeholder if it doesn't exist
bootstrapFile="$testsDir/bootstrap.php"
if [[ ! -f "$bootstrapFile" ]]; then
echo "Creating placeholder bootstrap file at $bootstrapFile"
cat > "$bootstrapFile" << 'EOF'
<?php
declare(strict_types=1);
/**
* PHPUnit Bootstrap File
*
* This is a placeholder bootstrap file created by PHP-QA-CI.
*
* PURPOSE:
* The bootstrap file is executed before PHPUnit runs any tests.
* It's used to set up the testing environment, including:
* - Loading the autoloader
* - Setting environment variables
* - Initializing framework components
* - Configuring test databases
*
* SYMFONY PROJECTS:
* For Symfony projects, you should typically:
* 1. Load the autoloader
* 2. Use Dotenv to load .env.test
*
* Example for Symfony:
* ```php
* use Symfony\Component\Dotenv\Dotenv;
*
* require dirname(__DIR__).'/vendor/autoload.php';
*
* if (method_exists(Dotenv::class, 'bootEnv')) {
* (new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
* }
* ```
*
* REPLACE THIS FILE with your project-specific bootstrap logic.
*/
// Load composer autoloader
require dirname(__DIR__) . '/vendor/autoload.php';
// Uncomment and add your project-specific bootstrap logic here:
// (static function (): void {
// // e.g. set environment variables, initialise framework, configure test database
// })();
EOF
echo "Placeholder bootstrap file created. Please customize it for your project needs."
fi
phpCmd=phpNoXdebug
if [[ "1" == "$phpUnitCoverage" ]]
then
phpCmd="$phpBinPath"
fi
phpunitPath="$binDir"/phpunit
phpunitVersion="$("$phpCmd" -f "$phpunitPath" -- --version | grep -Po '\d+.\d+.\d+')"
phpunitVersionMajor="$(echo "$phpunitVersion" | cut -d . -f1)"
echo "PHPUnit Major Version: $phpunitVersionMajor"
paratestConfig=
echo "Checking for paratest"
if [[ -f "$binDir"/paratest ]]
then
echo "Found paratest, using this instead of standard $binDir/phpunit"
phpunitPath="$binDir"/paratest
paratestConfig=(--phpunit "$binDir"/phpunit)
fi
phpunitFailedOnlyFiltered=0
phpunitExitCode=99
phpunitLogFilePath="$varDir/phpunit_logs/phpunit.junit.xml"
phpunitLogDir="$varDir/phpunit_logs"
while (( phpunitExitCode > 0 ))
do
extraConfigs=(" ")
extraConfigs+=( --strict-global-state )
# Enabling testdox seems to prevent displaying of warnings
# extraConfigs+=( --testdox )
extraConfigs+=( --fail-on-risky )
extraConfigs+=( --fail-on-warning )
extraConfigs+=( --log-junit "$phpunitLogFilePath" )
if(( $phpunitVersionMajor >= 10 ))
then
extraConfigs+=( --colors=always )
extraConfigs+=( --display-incomplete )
extraConfigs+=( --display-skipped )
extraConfigs+=( --display-deprecations )
extraConfigs+=( --display-errors )
extraConfigs+=( --display-notices )
extraConfigs+=( --display-warnings )
fi
## MODES
if [[ "1" == "$phpUnitIterativeMode" ]]
then
# Uniterate mode - order by defects, stop on first error, no coverage and enforce time limits
echo
echo "Uniterate Mode - Iterative Testing with Fast Failure"
echo "----------------------------------------------------"
echo
extraConfigs+=( --order-by=depends,defects )
extraConfigs+=( --stop-on-failure --stop-on-error --stop-on-defect --stop-on-warning )
extraConfigs+=( --no-coverage )
extraConfigs+=( --enforce-time-limit )
elif [[ "1" != "$phpUnitCoverage" ]]
then
# No Coverage mode - do not generate coverage
export XDEBUG_MODE=off
extraConfigs+=( --no-coverage )
extraConfigs+=( --enforce-time-limit )
elif [[ "false" != "${CI:-'false'}" ]]
then
# When in CI and generating coverage - do not enforce time limits
# Note: Removed stop-on-failure flags to allow full test runs in CI
: # No-op to keep the block valid
else
# Default, do enforce timelimits
extraConfigs+=( --enforce-time-limit )
fi
set +e
set -x
# If specific paths are provided, append them after the config options
pathArgs=()
if [[ -n "$specifiedPath" ]]; then
pathArgs+=("${pathsToCheck[@]}")
echo "Running PHPUnit on specified paths: ${pathsToCheck[*]}"
fi
# Capture both JUnit XML (via --log-junit) and stdout (via tee)
phpUnitQuickTests="$phpUnitQuickTests" $phpCmd -f $phpunitPath \
-- \
${paratestConfig[@]} \
-c ${phpUnitConfigPath} \
${extraConfigs[@]} \
${pathArgs[@]} \
2>&1 | tee "$phpunitLogDir/phpunit.log"
phpunitExitCode=${PIPESTATUS[0]}
set +x
set -e
if [[ "" != "$(grep '<testsuites/>' $phpunitLogFilePath)" || ! -f $phpunitLogFilePath ]]
then
echo "
ERROR - no tests have been run!
Please ensure you have at least one valid test suite configured in your phpunit.xml file
"
phpunitExitCode=1
fi
# Archive both log files BEFORE tryAgainOrAbort so logs are saved even on failure (in CI mode)
# But AFTER PHPUnit has finished writing files
echo ""
echo "Log Archival"
echo "============"
# Archive JUnit XML for parse-junit-logs.py
archiveToolLog "PHPUnit JUnit XML" "$phpunitLogDir" "phpunit.junit.xml" "$specifiedPath" "${pathsToCheck[@]}"
echo ""
# Archive human-readable stdout log
archiveToolLog "PHPUnit stdout" "$phpunitLogDir" "phpunit.log" "$specifiedPath" "${pathsToCheck[@]}"
# Extract and display test result summary
if [[ -f "$phpunitLogDir/phpunit.log" ]]; then
testSummary=$(grep -E '^Tests:.*Assertions:' "$phpunitLogDir/phpunit.log" | tail -n1)
if [[ -n "$testSummary" ]]; then
echo "Result: $testSummary"
fi
fi
if (( $phpunitExitCode > 0 ))
then
if (( $phpunitExitCode > 2 ))
then
printf "\n\n\nPHPUnit Crashed\n\nRunning again with Debug mode...\n\n\n"
qaQuickTests="$phpUnitQuickTests" phpNoXdebug -f "$binDir"/phpunit -- "$testsDir" --debug
fi
tryAgainOrAbort "PHPUnit Tests"
fi
done
set -e