name: Tests

on: [ push, pull_request ]

jobs:
  run:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        php: [ '8.1', '8.2', '8.3', '8.4' ]
        imagemagick: [ '6.9.12-55', '7.1.1-32' ]
        imagick: [ '3.7.0' ]

    name: PHP ${{ matrix.php }} - ImageMagick ${{ matrix.imagemagick }}

    steps:
      - name: Checkout project
        uses: actions/checkout@v4

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: mbstring, gd
          coverage: none

      - name: Prepare environment for Imagemagick
        run: |
          sudo apt-get -y remove imagemagick imagemagick-6-common libmagic-dev
          sudo apt-get update
          sudo apt-get install -y libjpeg-dev libgif-dev libtiff-dev libpng-dev libwebp-dev libavif-dev libheif-dev
          sudo apt-get install -y libmagickwand-dev

      - name: Cache ImageMagick
        uses: actions/cache@v4
        id: cache-imagemagick
        with:
          path: /home/runner/im/imagemagick-${{ matrix.imagemagick }}
          key: ${{ runner.os }}-ImageMagick-${{ matrix.imagemagick }}-${{ hashFiles('**/composer.json') }}
          restore-keys: ${{ runner.os }}-ImageMagick-${{ matrix.imagemagick }}-

      - name: Check ImageMagick cache exists
        uses: andstor/file-existence-action@v3
        id: cache-imagemagick-exists
        with:
          files: /home/runner/im/imagemagick-${{ matrix.imagemagick }}

      - name: Install ImageMagick
        if: ( steps.cache-imagemagick.outputs.cache-hit != 'true' || steps.cache-imagemagick-exists.outputs.files_exists != 'true' )
        run: |
          curl -o /tmp/ImageMagick.tar.xz -sL https://imagemagick.org/archive/releases/ImageMagick-${{ matrix.imagemagick }}.tar.xz
          (
            cd /tmp || exit 1
            tar xf ImageMagick.tar.xz
            cd ImageMagick-${{ matrix.imagemagick }}
            sudo ./configure --prefix=/home/runner/im/imagemagick-${{ matrix.imagemagick }}
            sudo make -j$(nproc)
            sudo make install
          )

      - name: Install Imagick PHP extension
        run: |
          curl -o /tmp/imagick.tgz -sL http://pecl.php.net/get/imagick-${{ matrix.imagick }}.tgz
          (
            cd /tmp || exit 1
            tar -xzf imagick.tgz
            cd imagick-${{ matrix.imagick }}
            phpize
            sudo ./configure --with-imagick=/home/runner/im/imagemagick-${{ matrix.imagemagick }}
            sudo make -j$(nproc)
            sudo make install
          )
          sudo bash -c 'echo "extension=imagick.so" >> /etc/php/${{ matrix.php }}/cli/php.ini'
          php --ri imagick;

      - name: Get composer cache directory
        id: composer-cache
        run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

      - name: Cache composer dependencies
        uses: actions/cache@v4
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-php-${{ matrix.php }}-${{ hashFiles('**/composer.json') }}
          restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-

      - name: Install dependencies
        run: composer update --prefer-stable --prefer-dist --no-interaction

      - name: GD Version
        run: php -r 'var_dump(gd_info());'

      - name: Imagick Version
        run: php -r 'var_dump(Imagick::getVersion());'

      - name: Supported Imagick Formats
        run: php -r 'var_dump(Imagick::queryFormats());'

      - name: Execute tests
        run: vendor/bin/phpunit --no-coverage

      - name: Run analyzer
        run: vendor/bin/phpstan

      - name: Validate coding standards
        run: vendor/bin/phpcs
