name: PHP Templates Tests
on: [ push ]
#on:
#  workflow_run:
#    workflows: ["Dusk"]
#    types:
#      - completed
#  workflow_dispatch:
#    inputs:
#      templates:
#        description: 'Templates to test'
#        required: true
#        default: 'microweber-templates/big'


concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: false

jobs:
  main:
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 1
      fail-fast: false
      matrix:
        templates: [
          'microweber-templates/big',
          'microweber-templates/big-free',
          'microweber-templates/art',
          'microweber-templates/bootstrap5',
          'microweber-templates/business',
          'microweber-templates/burger',
          'microweber-templates/beauty',
          'microweber-templates/dream',
          'microweber-templates/events',
          'microweber-templates/freelancer',
          'microweber-templates/fitpower',
          'microweber-templates/guesthouse',
          'microweber-templates/hospital',
          'microweber-templates/human-resources',
          'microweber-templates/hostel',
          'microweber-templates/machine',
          'microweber-templates/mobile-app',
          'microweber-templates/networking-space',
          'microweber-templates/new-world',
          'microweber-templates/nomad',
          'microweber-templates/nova',
          'microweber-templates/online-learning',
          'microweber-templates/photon',
          'microweber-templates/photographer',
          'microweber-templates/portfolio',
          'microweber-templates/private-school',
          'microweber-templates/protein-store',
          'microweber-templates/services',
          'microweber-templates/shopmag',
          'microweber-templates/simple',
          'microweber-templates/theplace',
          'microweber-templates/tourism',
          'microweber-templates/travel',
          'microweber-templates/urban',
          'microweber-templates/web-designer',
          'microweber-templates/yoga-studio'
        ]
        php-versions: ['8.2']
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - uses: ./.github/actions/setup-php
      with:
        php_version: ${{ matrix.php-versions }}
#    - name: Setup Apache
#      uses: thunder/apache-shiva-php-action@v1
#      with:
#        php-version: '7.4'
#        site-directory: /home/runner/work/microweber/microweber/
#        # Defaults to 8888
#        http-port:
#          8000
    - name: Npm install
      uses: actions/setup-node@v3
      with:
        node-version: 16
        cache: 'npm'
        cache-dependency-path: ./package-lock.json
    - run: npm install
    - run: npm run build
    - name: Cleanup old theme files
      run: |
        rm -rf userfiles/templates/big
        rm -rf userfiles/templates/*
        rm -rf bootstrap/cache/*
    - name: Add auth composer
      id: create-json
      uses: jsdaniell/create-json@1.1.2
      with:
        name: "auth.json"
        json: '{"http-basic":{"packages.microweberapi.com":{"username":"${{ secrets.COMPOSER_USERNAME }}","password":"${{ secrets.COMPOSER_PASSWORD }}"}}}'
    - name: Install Composer dependencies
      run: |
        ls -la
        composer install
        chmod -R 0755 vendor/laravel/dusk/bin/
    - name: Clean opcache
      run: |
        php -r "if (function_exists('opcache_reset')) { var_dump(opcache_reset()); } else { echo 'opcache_reset function does not exist.'; }"
    - name: Install template
      run: composer require ${{ matrix.templates }} --no-cache -vvv
    - name: Remove screenshot layout test
      run: |
        rm -f userfiles/templates/big/src/tests/Browser/BigScreenshotLayoutsTest.php
    - name: Install Microweber with template ${{ matrix.templates }}
      run: php artisan microweber:install --email=admin@site.com --username=1 --password=1 --db-host=/home/runner/work/microweber/microweber/storage/database1.sqlite --db-name=microweber --db-password=nopass --db-driver=sqlite --db-prefix=site_ --template=${{ matrix.templates }} --default-content=1
    - name: Run Laravel Server
      run:  php -d variables_order=EGPCS -S 127.0.0.1:8000 > /home/runner/work/microweber/microweber/storage/logs/serve.log 2>&1 &
    - name: Run Dusk Browse test
      run:  php artisan dusk --filter=BrowsePagesForBrokenTagsTest
    - name: Upload Screenshots
      if: failure()
      uses: actions/upload-artifact@v4
      with:
        name: screenshots
        path: tests/Browser/screenshots
    - name: Upload Console Logs
      if: failure()
      uses: actions/upload-artifact@v4
      with:
        name: console
        path: tests/Browser/console
    - name: Upload Laravel Storage Logs
      if: failure()
      uses: actions/upload-artifact@v4
      with:
        name: storage-logs
        path: storage/logs
    - name: Upload Laravel config
      if: failure()
      uses: actions/upload-artifact@v4
      with:
        name: config
        path:
          config



