name: Tests
on:
    pull_request: null
    push:
        branches:
            - main
jobs:
    integration:
        runs-on: ubuntu-latest
        services:
            mysql:
                image: mysql:latest
                env:
                    MYSQL_ALLOW_EMPTY_PASSWORD: false
                    MYSQL_ROOT_PASSWORD: kimai
                    MYSQL_DATABASE: kimai
                ports:
                    - 3306/tcp
                options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
        strategy:
            matrix:
                php: ['8.1', '8.2', '8.3', '8.4']

        name: Integration (${{ matrix.php }})
        steps:

            -   name: Clone Kimai
                uses: actions/checkout@v4
                with:
                    persist-credentials: false

            -   name: Setup PHP
                uses: shivammathur/setup-php@v2
                with:
                    php-version: ${{ matrix.php }}
                    coverage: pcov
                    extensions: ctype, gd, iconv, intl, ldap, mbstring, mysql, xml, zip
                    tools: cs2pr, symfony-cli
                env:
                    fail-fast: true

            -   name: Determine software versions
                run: mysql --version && php --version

            -   name: Determine composer cache directory
                id: composer-cache
                run: echo "composer_cache_directory=$(composer config cache-dir)" >> $GITHUB_ENV

            -   name: Cache Composer dependencies
                uses: actions/cache@v4
                with:
                    path: "${{ env.composer_cache_directory }}"
                    key: ${{ runner.os }}-${{ matrix.php }}-${{ hashFiles('**/composer.lock') }}

            -   name: Install dependencies
                run: composer install

            -   name: Validate Composer
                run: composer validate --strict --no-check-all

            -   name: Warmup cache
                run: APP_ENV=dev bin/console kimai:reload -n

            -   name: Check codestyles
                run: vendor/bin/php-cs-fixer fix --dry-run --verbose --config=.php-cs-fixer.dist.php --using-cache=no --show-progress=none --format=checkstyle | cs2pr

            -   name: Run PHPStan for application
                run: vendor/bin/phpstan analyse -c phpstan.neon --no-progress --error-format=checkstyle | cs2pr

            -   name: Run PHPStan for tests
                run: vendor/bin/phpstan analyse -c tests/phpstan.neon --no-progress --error-format=checkstyle | cs2pr

            -   name: Lint codebase
                run: composer linting

            -   name: Install LDAP package (for tests)
                run: composer require laminas/laminas-ldap

            -   name: Setup problem matchers (for PHPUnit)
                run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

            -   name: Run quick unit-tests
                run: composer tests-unit
                env:
                    DATABASE_URL: mysql://root:kimai@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/kimai?charset=utf8mb4&serverVersion=8.0.35
                    APP_ENV: test
                    MAILER_URL: null://localhost

            -   name: Full test-suite
                if: matrix.php != '8.2'
                run: vendor/bin/phpunit tests/
                env:
                    DATABASE_URL: mysql://root:kimai@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/kimai?charset=utf8mb4&serverVersion=8.0.35
                    APP_ENV: test
                    MAILER_URL: null://localhost

            -   name: Full test-suite with coverage
                if: matrix.php == '8.2'
                run: vendor/bin/phpunit tests/ --coverage-clover=coverage.xml
                env:
                    DATABASE_URL: mysql://root:kimai@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/kimai?charset=utf8mb4&serverVersion=8.0.35
                    APP_ENV: dev
                    MAILER_URL: null://localhost

            -   name: Upload code coverage
                if: matrix.php == '8.2'
                uses: codecov/codecov-action@v5
                with:
                    token: ${{ secrets.CODECOV_TOKEN }}
                    files: ./coverage.xml
                    fail_ci_if_error: true

            -   name: Run migrations on MySQL
                run: |
                    bin/console doctrine:database:drop --if-exists --force -n
                    bin/console doctrine:database:create --if-not-exists -n
                    bin/console doctrine:migrations:migrate -n
                    bin/console doctrine:migrations:migrate first -n
                env:
                    DATABASE_URL: mysql://root:kimai@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/kimai?charset=utf8mb4&serverVersion=8.0.35
                    APP_ENV: dev
                    MAILER_URL: null://localhost

            -   name: Check for security issues in packages
                run: symfony security:check
