name: Linux

on: [push]

jobs:
  ci:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        php:
          - '8.4-alpine'
          - '8.4-zts-alpine'
          - '8.3-alpine'
          - '8.3-zts-alpine'
          - '8.2-alpine'
          - '8.2-zts-alpine'
          - '8.1-alpine'
          - '8.1-zts-alpine'
        library:
          - ''
          - 'system'
        apcu:
          - ''
          - 'apcu'
        image: [php]
        include:
          - php: '8.0-alpine'
            image: ghcr.io/kjdev/php
          - php: '7.4-alpine'
            image: ghcr.io/kjdev/php
          - php: '7.3-alpine'
            image: ghcr.io/kjdev/php
          - php: '7.2-alpine'
            image: ghcr.io/kjdev/php
          - php: '7.1-alpine'
            image: ghcr.io/kjdev/php
          - php: '7.0-alpine'
            image: ghcr.io/kjdev/php
          - php: '5.6-alpine'
            image: ghcr.io/kjdev/php

    container:
      image: ${{ matrix.image }}:${{ matrix.php }}

    steps:
      - name: Prepare required command
        run: |
          apk upgrade --no-cache
          apk add --no-cache autoconf gcc git libtool make musl-dev

      - name: adding github workspace as safe directory
        run: git config --global --add safe.directory $GITHUB_WORKSPACE
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Checkout submodules
        run: |
          git submodule update --init --recursive
        if: ${{ ! matrix.library }}
      - name: Install dependency library
        run: |
          apk add --no-cache lz4-dev
        if: ${{ matrix.library }}

      - name: Install PHP APCu extension
        run: |
          printf "\n" | pecl install apcu
          echo 'extension=apcu.so' > $(php --ini | grep 'Scan for' | sed 's|.* \(/.*\)$|\1|')/apcu.ini
        if: ${{ matrix.apcu }}

      - name: Build
        run: |
          phpize
          ./configure
          make
        if: ${{ ! matrix.library }}
      - name: Build with library
        run: |
          phpize
          ./configure --with-lz4-includedir
          make
        if: ${{ matrix.library }}

      - name: Test Preparation APCu
        run: |
          cp $(php-config --extension-dir)/apcu.so modules/
          sed -i 's/\$(PHP_TEST_SHARED_EXTENSIONS)/-d extension=apcu \$(PHP_TEST_SHARED_EXTENSIONS)/' Makefile
        if: ${{ matrix.apcu }}
      - name: Test
        run: |
          make test TESTS="--show-diff" | tee test-output.txt
          grep 'TEST SUMMARY$' test-output.txt > /dev/null && exit 1 || exit 0
        env:
          REPORT_EXIT_STATUS: 1
          NO_INTERACTION: 1
      - name: Failure Test
        run: |
          for FILE in $(find ./tests -name '*.diff'); do
            echo echo $FILE
            cat $FILE
            echo
          done
        if: ${{ failure() }}
