#!/bin/zsh

restoreAndExit () {
  mv composer.json.bak composer.json
  exit
}

declare -A testbench

testbench=(
["10.*"]="^8.15"
["11.*"]="^8.15"
)
cp composer.json composer.json.bak

for phpVersion in "8.2" "8.3"
do
  for laravelVersion in "10.*" # "11.*"
  do
    for stability in "prefer-lowest" "prefer-stable"
    do
      echo "PHP$phpVersion – L$laravelVersion – $stability - testbench:${testbench[$laravelVersion]}"

      herd use $phpVersion
      herd composer require "laravel/framework:$laravelVersion" "orchestra/testbench:${testbench[$laravelVersion]}" --no-interaction --no-update

      if herd composer update --$stability --prefer-dist --no-interaction && herd php vendor/bin/pest; then
      else
        echo "Process failed! Press any key to restore composer.json..."
        read -s -n 1
        restoreAndExit
      fi
    done
  done
done

restoreAndExit


