name: Continuous Integration

on:
  push:
    branches:
      - master
      - main
  pull_request:

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:8.0
        env:
          # The MySQL docker container requires these environment variables to be set
          # so we can create and migrate the test database.
          # See: https://hub.docker.com/_/mysql
          MYSQL_DATABASE: db_absensi_test
          MYSQL_ROOT_PASSWORD: root
        ports:
          # Opens port 3306 on service container and host
          # https://docs.github.com/en/actions/using-containerized-services/about-service-containers
          - 3306:3306
          # Before continuing, verify the mysql container is reachable from the ubuntu host
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

    steps:
      - uses: actions/checkout@v4

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.4'
          tools: composer

      - name: Validate composer.json and composer.lock
        run: composer validate --strict

      - name: Cache Composer packages
        id: composer-cache
        uses: actions/cache@v4
        with:
          path: vendor
          key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-php-

      - name: Install dependencies
        run: composer install --prefer-dist --no-progress

      # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
      # Docs: https://getcomposer.org/doc/articles/scripts.md

      - name: Run test suite
        run: composer run-script test
