#!/usr/bin/env bash
#Thanks Leo for this.

function phpunit() {
  if [[ ! -e ./vendor/bin/phpunit ]]; then
    echo "The vendor/bin/phpunit file is missing. Did you install the dependencies?"
    exit 1
  fi

  vendor/bin/phpunit --testdox --colors >results.txt
  #cat ergebnis.txt | grep ' │'
  echo "Complete result in results.txt"
}

function phpunitshort() {
  if [[ ! -e ./vendor/bin/phpunit ]]; then
    echo "The vendor/bin/phpunit file is missing. Did you install the dependencies?"
    exit 1
  fi

  vendor/bin/phpunit -c phpunit.short.xml --testdox --colors >results.short.txt
  #cat ergebnis.txt | grep ' │'
  echo "Complete result in results.short.txt"
}

function php-cs-fixer() {
  if [[ ! -e ./vendor/bin/php-cs-fixer ]]; then
    echo "The vendor/bin/php-cs-fixer file is missing. Did you install the dependencies?"
    exit 1
  fi

  vendor/bin/php-cs-fixer fix -v
  vendor/bin/php-cs-fixer fix -v --config .php-cs-fixer.legacy.php
}


function usage() {
  cat <<HEREDOC
Usage: ./run [task]
  phpunit       Run PhpUnit in each bundle
  phpunitshort  Run PhpUnit in each bundle with max 100 tests for user agents and devices
  php-cs-fixer  Run the PHP-CS-Fixer in each bundle
HEREDOC
}

case "$1" in
  phpunit)
    phpunit
    exit 0
  ;;
  phpunitshort)
    phpunitshort
    exit 0
  ;;  
  php-cs-fixer)
    php-cs-fixer
    exit 0
  ;;
  *)
    usage
    exit 1
  ;;
esac
