parameters:
  configurationName: ''
  phpVersion: ''

# A reference for testing PECL extensions using azure can be seen at https://github.com/microsoft/msphpsql/blob/master/azure-pipelines.yml
# (that extension also runs unit tests on Windows/Macs)
jobs:
  - job: ${{ parameters.configurationName }}
    # NOTE: This currently does not use containers. Doing so may be useful for testing on php zts/32-bit.
    # Containers need to provide sudo apt-get in order to work, the php:x-cli images don't.
    # Containers are slower to start up than the default vm images
    pool:
      vmImage: ${{ parameters.vmImage }}
    steps:
    - script: |
        VER=${{ parameters.phpVersion }}
        # Refresh the cache for the PPA repository, it can be out of date if it's updated recently
        if [[ ! -f /usr/bin/phpize$VER ]]; then
          sudo add-apt-repository -u ppa:ondrej/php
        fi
        # Silently try to install the php version if it's available.
        # ondrej/php is a minimal install for php 8.0.
        sudo apt-get install -y php$VER php$VER-dev php$VER-xml php$VER-mbstring
        sudo update-alternatives --set php /usr/bin/php$VER
        # Fail the build early if the php version isn't installed on this image
        sudo update-alternatives --set phpize /usr/bin/phpize$VER || exit 1
        sudo update-alternatives --set pecl /usr/bin/pecl$VER
        sudo update-alternatives --set phar /usr/bin/phar$VER
        sudo update-alternatives --set phpdbg /usr/bin/phpdbg$VER
        sudo update-alternatives --set php-cgi /usr/bin/php-cgi$VER
        sudo update-alternatives --set phar.phar /usr/bin/phar.phar$VER
        sudo update-alternatives --set php-config /usr/bin/php-config$VER
      displayName: Use PHP version ${{ parameters.phpVersion }}
    - script: |
        VER=${{ parameters.phpVersion }}
        CONF_DIR=/etc/php/$VER/cli/conf.d

        sudo pecl install ast-1.0.14
        php --version
        sudo rm -f $CONF_DIR/*xdebug.ini
        echo 'extension=ast.so' | sudo tee $CONF_DIR/20-ast.ini
        php -m
        php --ini

        composer validate
        composer --prefer-dist --classmap-authoritative install
        pushd internal/paratest; composer --prefer-dist --classmap-authoritative install; popd
      displayName: 'Install dependencies'
    - script: |
        tests/run_all_tests || exit 1
        php -d phar.readonly=0 internal/package.php || exit 1
      displayName: 'Test phan'
