version: '{branch}.{build}'

branches:
  except:
    - PHP-7.0-Linux
    - PHP5

environment:
    # MSSQL credentials from https://www.appveyor.com/docs/services-databases/
    TEST_PHP_SQL_PWD: Password12!
    TEST_PHP_SQL_UID: sa
    SQLSRV_DBNAME: msphpsql_sqlsrv
    PDOSQLSRV_DBNAME: msphpsql_pdosqlsrv
    PYTHON: c:\Python36
    # For details about Appveyor build worker images (VM template): https://www.appveyor.com/docs/build-environment/#build-worker-images
    matrix:
      - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
        BUILD_PLATFORM: x64
        TEST_PHP_SQL_SERVER: (local)\SQL2017
        SQL_INSTANCE: SQL2017
        PHP_VC: vc15
        PHP_MAJOR_VER: 7.4
        PHP_MINOR_VER: latest
        PHP_EXE_PATH: x64\Release_TS
        THREAD: ts
        platform: x64
      - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
        BUILD_PLATFORM: x86
        TEST_PHP_SQL_SERVER: (local)\SQL2019
        SQL_INSTANCE: SQL2019
        PHP_VC: vs16
        PHP_MAJOR_VER: 8.0
        PHP_MINOR_VER: latest
        PHP_EXE_PATH: Release
        THREAD: nts
        platform: x86

# PHP_MAJOR_VER is PHP major version to build (7.4, 7.3)
# PHP_MINOR_VER is PHP point release number (or latest for latest release)
# PHP_VC is the Visual C++ version
# PHP_EXE_PATH is the relative path from php src folder to php executable
# THREAD is either non-thread-safe (nts) or thread-safe (ts)

matrix:
  fast_finish: true

# clone directory (or %APPVEYOR_BUILD_FOLDER%)
clone_folder: c:\projects\sqlphp

build:
    parallel: true                  # enable MSBuild parallel builds

install:
    - echo start SQL Server
    # Based on http://www.appveyor.com/docs/services-databases
    - ps: >-
        [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null;
        [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | Out-Null;

        $instanceName = $env:SQL_INSTANCE;
        $uri = "ManagedComputer[@Name='$env:COMPUTERNAME']/ServerInstance[@Name='$instanceName']/ServerProtocol[@Name='Tcp']";
        $wmi = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer');
        $tcp = $wmi.GetSmoObject($uri);
        $tcp.IsEnabled = $true;
        $tcp.Alter();
        Start-Service "MSSQL`$$instanceName";

        Set-Service SQLBrowser -StartupType Manual;
        Start-Service SQLBrowser;
    - echo Downloading prerequisites
    - ps: | 
        $client = New-Object Net.WebClient;
        $client.Headers.Add("user-agent", "appveyor-ci-build2");
        $client.DownloadFile("http://windows.php.net/downloads/releases/sha256sum.txt", "c:\projects\sha256sum.txt");
        If ($env:PHP_MINOR_VER -Match "latest") {
            $env:PHP_VERSION=type c:\projects\sha256sum.txt | where { $_ -match "php-($env:PHP_MAJOR_VER\.\d+)-src" } | foreach { $matches[1] }  ;
        } Else {
            $env:PHP_VERSION=$env:PHP_MAJOR_VER + '.' + $env:PHP_MINOR_VER;
        }
    - echo Downloading MSODBCSQL 17
    # AppVeyor build works are x64 VMs and 32-bit ODBC driver cannot be installed on it
    - ps: (new-object net.webclient).DownloadFile('https://download.microsoft.com/download/a/e/b/aeb7d4ff-ca20-45db-86b8-8a8f774ce97b/en-US/17.8.1.1/x64/msodbcsql.msi', 'c:\projects\msodbcsql.msi')
    - cmd /c start /wait msiexec /i "c:\projects\msodbcsql.msi" /q IACCEPTMSODBCSQLLICENSETERMS=YES ADDLOCAL=ALL
    - echo Checking the version of MSODBCSQL
    - reg query "HKLM\SOFTWARE\ODBC\odbcinst.ini\ODBC Driver 17 for SQL Server"
    - dir %WINDIR%\System32\msodbcsql*.dll
    - cd c:\projects
    - echo update SQL connection string
    - ps: (Get-Content ${env:APPVEYOR_BUILD_FOLDER}\test\functional\pdo_sqlsrv\MsSetup.inc) | ForEach-Object { $_ -replace "TARGET_SERVER", ${env:TEST_PHP_SQL_SERVER} -replace "TARGET_DATABASE", ${env:PDOSQLSRV_DBNAME} -replace "TARGET_USERNAME",  ${env:TEST_PHP_SQL_UID} -replace "TARGET_PASSWORD",  ${env:TEST_PHP_SQL_PWD} } | Set-Content ${env:APPVEYOR_BUILD_FOLDER}\test\functional\pdo_sqlsrv\MsSetup.inc
    - ps: Select-String ${env:SQL_INSTANCE} ${env:APPVEYOR_BUILD_FOLDER}\test\functional\pdo_sqlsrv\MsSetup.inc
    - ps: Select-String ${env:PDOSQLSRV_DBNAME} ${env:APPVEYOR_BUILD_FOLDER}\test\functional\pdo_sqlsrv\MsSetup.inc
    - ps: (Get-Content ${env:APPVEYOR_BUILD_FOLDER}\test\functional\sqlsrv\MsSetup.inc) | ForEach-Object { $_ -replace "TARGET_SERVER", ${env:TEST_PHP_SQL_SERVER} -replace "TARGET_DATABASE", ${env:SQLSRV_DBNAME} -replace "TARGET_USERNAME",  ${env:TEST_PHP_SQL_UID} -replace "TARGET_PASSWORD",  ${env:TEST_PHP_SQL_PWD} } | Set-Content ${env:APPVEYOR_BUILD_FOLDER}\test\functional\sqlsrv\MsSetup.inc
    - ps: Select-String ${env:SQL_INSTANCE} ${env:APPVEYOR_BUILD_FOLDER}\test\functional\sqlsrv\MsSetup.inc
    - ps: Select-String ${env:SQLSRV_DBNAME} ${env:APPVEYOR_BUILD_FOLDER}\test\functional\sqlsrv\MsSetup.inc
    - echo install opencppcoverage
    - choco install opencppcoverage
    - set path=C:\Program Files\OpenCppCoverage;%PYTHON%;%PYTHON%\Scripts;%path%
    - copy %APPVEYOR_BUILD_FOLDER%\codecov.yml c:\projects

build_script:
    - copy %APPVEYOR_BUILD_FOLDER%\buildscripts\*.py c:\projects
    - cd c:\projects
    - python -V
    - python builddrivers.py --PHPVER=%PHP_VERSION% --ARCH=%BUILD_PLATFORM% --THREAD=%THREAD% --SOURCE=%APPVEYOR_BUILD_FOLDER%\source --TESTING --NO_RENAME
    - cd c:\projects\php-sdk\phpdev\%PHP_VC%\%BUILD_PLATFORM%\php-%PHP_VERSION%-src\
    - set PHP_SRC_DIR=%CD%\ext
    - cd %PHP_EXE_PATH%
    - set PHP_EXE_PATH=%CD%
    - echo Showing the last few lines of php.ini
    - ps: Get-Content ${env:PHP_EXE_PATH}\php.ini -Tail 5
    - php --ini
    - php -m

test_script:
    - Echo setup test database for SQLSRV tests  - %SQLSRV_DBNAME%
    - python %APPVEYOR_BUILD_FOLDER%\test\functional\setup\setup_dbs.py -dbname %SQLSRV_DBNAME%
    - Echo setup test database for PDO_SQLSRV tests - %PDOSQLSRV_DBNAME%
    - python %APPVEYOR_BUILD_FOLDER%\test\functional\setup\setup_dbs.py -dbname %PDOSQLSRV_DBNAME%
    # for code coverage - exclude the following files:
    #   core_init.cpp, which primarily consists module initialization and shutdown
    #   core_stream.cpp for pdo_sqlsrv because it is only used by sqlsrv driver
    - ps: >-
        If ($env:BUILD_PLATFORM -Match "x86") {
            Write-Host "Running phpt tests via OpenCppCoverage..."
            OpenCppCoverage.exe --sources ${env:PHP_SRC_DIR}\*sqlsrv --modules ${env:PHP_EXE_PATH}\php*sqlsrv.dll --excluded_sources core_init.cpp --excluded_sources ${env:PHP_SRC_DIR}\pdo_sqlsrv\shared\core_stream.cpp --export_type=cobertura:c:\projects\coverage.xml --quiet --cover_children --continue_after_cpp_exception --optimized_build -- .\php.exe run-tests.php -P --no-color ${env:APPVEYOR_BUILD_FOLDER}\test\functional\ | out-file -filePath ${env:APPVEYOR_BUILD_FOLDER}\test\functional\tests.log -encoding UTF8;
            Write-Host "Showing the last 25 lines of the log file..."
            Get-Content ${env:APPVEYOR_BUILD_FOLDER}\test\functional\tests.log -Tail 25;
            ls *.xml
        } Else {
            Write-Host "Running phpt tests the regular way..."
            .\php.exe run-tests.php -P ${env:APPVEYOR_BUILD_FOLDER}\test\functional\sqlsrv\*.phpt | out-file -filePath ${env:APPVEYOR_BUILD_FOLDER}\test\functional\sqlsrv.log -encoding UTF8;
            Write-Host "Showing the last 25 lines of the log file..."
            Get-Content ${env:APPVEYOR_BUILD_FOLDER}\test\functional\sqlsrv.log -Tail 25;
            .\php.exe run-tests.php -P ${env:APPVEYOR_BUILD_FOLDER}\test\functional\pdo_sqlsrv\*.phpt | out-file -filePath ${env:APPVEYOR_BUILD_FOLDER}\test\functional\pdo_sqlsrv.log -encoding UTF8;
            Write-Host "Showing the last 25 lines of the log file..."
            Get-Content ${env:APPVEYOR_BUILD_FOLDER}\test\functional\pdo_sqlsrv.log -Tail 25;
        }
    - python %APPVEYOR_BUILD_FOLDER%\test\functional\setup\cleanup_dbs.py -dbname %SQLSRV_DBNAME%
    - python %APPVEYOR_BUILD_FOLDER%\test\functional\setup\cleanup_dbs.py -dbname %PDOSQLSRV_DBNAME%
    - cd %PHP_EXE_PATH%
    - ps: $fileExists = Test-Path "c:\projects\coverage.xml"
    - ps: >-
        If ($fileExists -eq $true) {
            cd c:\projects
            Write-Host "Running coverage analysis...";
            Write-Host "Showing the packages...";
            Select-String package .\coverage.xml;
            Invoke-WebRequest -Uri 'https://codecov.io/bash' -OutFile codecov.sh
            bash codecov.sh -f "coverage.xml"
            cd ${env:PHP_EXE_PATH}
        }

after_test:
    - cd %APPVEYOR_BUILD_FOLDER%\test\functional\
    - ps: ls *.log
    - python output.py
    - ps: $difffiles = Get-ChildItem sqlsrv\*.diff
    - ps: $outfiles = Get-ChildItem sqlsrv\*.out
    - ps: foreach($file in $difffiles){ls $file; more $file}
    - ps: foreach($file in $outfiles){ls $file; more $file}
    - ps: $diff2files = Get-ChildItem pdo_sqlsrv\*.diff
    - ps: $out2files = Get-ChildItem pdo_sqlsrv\*.out
    - ps: foreach($file in $diff2files){ls $file; more $file}
    - ps: foreach($file in $out2files){ls $file; more $file}
    - cd %APPVEYOR_BUILD_FOLDER%\test\functional\
    - ps: $xmlfiles = Get-ChildItem *.xml
    - ps: foreach($file in $xmlfiles){(new-object net.webclient).UploadFile("https://ci.appveyor.com/api/testresults/junit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path $file))}
    - ps: >-
        If ($difffiles -ne $null -Or $diff2files -ne $null) {
            $host.SetShouldExit(1);
            Write-Host "Forcing build failure due to phpt unit test failure(s)";
        }

