1if /i "%GITHUB_ACTIONS%" neq "True" (
2    echo for CI only
3    exit /b 3
4)
5
6set NO_INTERACTION=1
7set REPORT_EXIT_STATUS=1
8set SKIP_IO_CAPTURE_TESTS=1
9
10call %~dp0find-target-branch.bat
11if "%BRANCH%" neq "master" (
12	set STABILITY=stable
13) else (
14	set STABILITY=staging
15)
16set DEPS_DIR=%PHP_BUILD_CACHE_BASE_DIR%\deps-%BRANCH%-%PHP_SDK_VS%-%PHP_SDK_ARCH%
17if not exist "%DEPS_DIR%" (
18	echo "%DEPS_DIR%" doesn't exist
19	exit /b 3
20)
21
22rem setup MySQL related exts
23set MYSQL_PWD=Password12!
24set MYSQL_TEST_PASSWD=%MYSQL_PWD%
25set MYSQL_TEST_USER=root
26set MYSQL_TEST_HOST=127.0.0.1
27set MYSQL_TEST_PORT=3306
28set PDO_MYSQL_TEST_USER=%MYSQL_TEST_USER%
29set PDO_MYSQL_TEST_PASS=%MYSQL_PWD%
30set PDO_MYSQL_TEST_HOST=%MYSQL_TEST_HOST%
31set PDO_MYSQL_TEST_PORT=%MYSQL_TEST_PORT%
32set PDO_MYSQL_TEST_DSN=mysql:host=%PDO_MYSQL_TEST_HOST%;port=%PDO_MYSQL_TEST_PORT%;dbname=test
33mysql --host=%PDO_MYSQL_TEST_HOST% --port=%MYSQL_TEST_PORT% --user=%MYSQL_TEST_USER% --password=%MYSQL_TEST_PASSWD% -e "CREATE DATABASE IF NOT EXISTS test"
34if %errorlevel% neq 0 exit /b 3
35
36rem setup PostgreSQL related exts
37set PGUSER=postgres
38set PGPASSWORD=Password12!
39rem set PGSQL_TEST_CONNSTR=host=127.0.0.1 dbname=test port=5432 user=postgres password=Password12!
40echo ^<?php $conn_str = "host=127.0.0.1 dbname=test port=5432 user=%PGUSER% password=%PGPASSWORD%"; ?^> >> "./ext/pgsql/tests/config.inc"
41set PDO_PGSQL_TEST_DSN=pgsql:host=127.0.0.1 port=5432 dbname=test user=%PGUSER% password=%PGPASSWORD%
42set TMP_POSTGRESQL_BIN=%PGBIN%
43"%TMP_POSTGRESQL_BIN%\createdb.exe" test
44if %errorlevel% neq 0 exit /b 3
45
46rem setup ODBC related exts
47set ODBC_TEST_USER=sa
48set ODBC_TEST_PASS=Password12!
49set ODBC_TEST_DSN=Driver={ODBC Driver 17 for SQL Server};Server=^(local^)\SQLEXPRESS;Database=master;uid=%ODBC_TEST_USER%;pwd=%ODBC_TEST_PASS%
50set PDOTEST_DSN=odbc:%ODBC_TEST_DSN%
51
52rem setup Firebird related exts
53if "%PLATFORM%" == "x64" (
54	set PHP_FIREBIRD_DOWNLOAD_URL=https://github.com/FirebirdSQL/firebird/releases/download/v4.0.4/Firebird-4.0.4.3010-0-x64.zip
55) else (
56	set PHP_FIREBIRD_DOWNLOAD_URL=https://github.com/FirebirdSQL/firebird/releases/download/v4.0.4/Firebird-4.0.4.3010-0-Win32.zip
57)
58curl -sLo Firebird.zip %PHP_FIREBIRD_DOWNLOAD_URL%
597z x -oC:\Firebird Firebird.zip
60set PDO_FIREBIRD_TEST_DATABASE=C:\test.fdb
61set PDO_FIREBIRD_TEST_DSN=firebird:dbname=%PDO_FIREBIRD_TEST_DATABASE%
62set PDO_FIREBIRD_TEST_USER=SYSDBA
63set PDO_FIREBIRD_TEST_PASS=phpfi
64echo create database '%PDO_FIREBIRD_TEST_DATABASE%' user '%PDO_FIREBIRD_TEST_USER%' password '%PDO_FIREBIRD_TEST_PASS%';> C:\Firebird\setup.sql
65C:\Firebird\instsvc.exe install -n TestInstance
66C:\Firebird\isql -q -i C:\Firebird\setup.sql
67C:\Firebird\instsvc.exe start -n TestInstance
68if %errorlevel% neq 0 exit /b 3
69path C:\Firebird;%PATH%
70
71rem prepare for ext/openssl
72rmdir /s /q C:\OpenSSL-Win32 >NUL 2>NUL
73rmdir /s /q C:\OpenSSL-Win64 >NUL 2>NUL
74if "%PLATFORM%" == "x64" (
75	set OPENSSLDIR="C:\Program Files\Common Files\SSL"
76) else (
77	set OPENSSLDIR="C:\Program Files (x86)\Common Files\SSL"
78)
79if /i "%GITHUB_ACTIONS%" equ "True" (
80    rmdir /s /q %OPENSSLDIR% >nul 2>&1
81)
82mkdir %OPENSSLDIR%
83if %errorlevel% neq 0 exit /b 3
84copy %DEPS_DIR%\template\ssl\openssl.cnf %OPENSSLDIR%
85if %errorlevel% neq 0 exit /b 3
86rem set OPENSSL_CONF=%OPENSSLDIR%\openssl.cnf
87set OPENSSL_CONF=
88rem set SSLEAY_CONF=
89
90rem prepare for OPcache
91if "%OPCACHE%" equ "1" set OPCACHE_OPTS=-d opcache.enable=1 -d opcache.enable_cli=1 -d opcache.protect_memory=1 -d opcache.jit_buffer_size=64M -d opcache.jit=tracing
92rem work-around for failing to dl(mysqli) with OPcache (https://github.com/php/php-src/issues/8508)
93if "%OPCACHE%" equ "1" set OPCACHE_OPTS=%OPCACHE_OPTS% -d extension=mysqli
94
95rem prepare for enchant
96mkdir %~d0\usr\local\lib\enchant-2
97if %errorlevel% neq 0 exit /b 3
98copy %DEPS_DIR%\bin\libenchant2_hunspell.dll %~d0\usr\local\lib\enchant-2
99if %errorlevel% neq 0 exit /b 3
100mkdir %~d0\usr\local\share\enchant\hunspell
101if %errorlevel% neq 0 exit /b 3
102echo Fetching enchant dicts
103pushd %~d0\usr\local\share\enchant\hunspell
104powershell -Command wget https://downloads.php.net/~windows/qa/appveyor/ext/enchant/dict.zip -OutFile dict.zip
105unzip dict.zip
106del /q dict.zip
107popd
108
109rem prepare for snmp
110set MIBDIRS=%DEPS_DIR%\share\mibs
111sed -i "s/exec HexTest .*/exec HexTest cscript\.exe \/nologo %GITHUB_WORKSPACE:\=\/%\/ext\/snmp\/tests\/bigtest\.js/g" %GITHUB_WORKSPACE%\ext\snmp\tests\snmpd.conf
112start %DEPS_DIR%\bin\snmpd.exe -C -c %GITHUB_WORKSPACE%\ext\snmp\tests\snmpd.conf -Ln
113
114set PHP_BUILD_DIR=%PHP_BUILD_OBJ_DIR%\Release
115if "%THREAD_SAFE%" equ "1" set PHP_BUILD_DIR=%PHP_BUILD_DIR%_TS
116
117rem prepare for mail
118curl -sLo hMailServer.exe https://www.hmailserver.com/download_file/?downloadid=271
119hMailServer.exe /verysilent
120cd %APPVEYOR_BUILD_FOLDER%
121%PHP_BUILD_DIR%\php.exe -dextension_dir=%PHP_BUILD_DIR% -dextension=com_dotnet .github\setup_hmailserver.php
122
123mkdir %PHP_BUILD_DIR%\test_file_cache
124rem generate php.ini
125echo extension_dir=%PHP_BUILD_DIR% > %PHP_BUILD_DIR%\php.ini
126echo opcache.file_cache=%PHP_BUILD_DIR%\test_file_cache >> %PHP_BUILD_DIR%\php.ini
127if "%OPCACHE%" equ "1" echo zend_extension=php_opcache.dll >> %PHP_BUILD_DIR%\php.ini
128rem work-around for some spawned PHP processes requiring OpenSSL
129echo extension=php_openssl.dll >> %PHP_BUILD_DIR%\php.ini
130
131rem remove ext dlls for which tests are not supported
132for %%i in (ldap) do (
133	del %PHP_BUILD_DIR%\php_%%i.dll
134)
135
136set TEST_PHPDBG_EXECUTABLE=%PHP_BUILD_DIR%\phpdbg.exe
137
138mkdir c:\tests_tmp
139
140nmake test TESTS="%OPCACHE_OPTS% -g FAIL,BORK,LEAK,XLEAK --no-progress -q --offline --show-diff --show-slow 1000 --set-timeout 120 --temp-source c:\tests_tmp --temp-target c:\tests_tmp --bless %PARALLEL%"
141
142set EXIT_CODE=%errorlevel%
143
144taskkill /f /im snmpd.exe
145
146if %EXIT_CODE% GEQ 1 (
147	git checkout ext\pgsql\tests\config.inc
148	git diff > bless_tests.patch
149)
150
151exit /b %EXIT_CODE%
152