#!/usr/bin/env php
<?php
/**
 * Copyright © OXID eSales AG. All rights reserved.
 * See LICENSE file for license details.
 */

use OxidEsales\TestingLibrary\Helper\TestResultsPrintingHelper;
use OxidEsales\TestingLibrary\TestConfig;

require_once __DIR__ . '/../base.php';

$testConfig = new TestConfig();
$resultsHelper = new TestResultsPrintingHelper();

$arguments = array();
array_shift($argv);
foreach ($argv as $argument) {
    $arguments[] = (strpos($argument, '-') === 0) ? $argument : escapeshellarg($argument);
}

$php = getenv('PHPBIN') ? getenv('PHPBIN') : 'php';

$phpUnit = $testConfig->getVendorDirectory() . "/bin/phpunit";
if (!file_exists($phpUnit)) {
    $phpUnit = "phpunit";
}
$phpUnit = "$php $phpUnit --bootstrap " . __DIR__ . "/../bootstrap.php";

$arguments = empty($arguments) ? array(escapeshellarg('AllTestsSelenium')) : $arguments;

$returnCode = 0;
/** Replace markers (e.g. /logs/phpunit_log_TIMESTAMP.xml) with unique timestamp strings */
$argumentString = $resultsHelper->insertReportTimestamps(implode(' ', $arguments));
if (end($arguments) == escapeshellarg('AllTestsSelenium')) {
    $testSuites = $testConfig->getTestSuites();
    foreach ($testSuites as $suite) {
        $suiteReturnCode = runSuite($suite, $phpUnit, $argumentString);
        $returnCode = $returnCode == 0 ? $suiteReturnCode : $returnCode;
    }
} else {
    $suite = $testConfig->getCurrentTestSuite();
    $returnCode = runSuite($suite, $phpUnit, $argumentString);
}

exit($returnCode);

function runSuite(string $suite, string $phpUnit, string $arguments)
{
    $return = 0;
    $configuration = '';
    if (file_exists($suite . "/phpunit.xml")) {
        $configuration = "-c " . escapeshellarg("$suite/phpunit.xml");
    }
    $suite = escapeshellarg($suite);
    passthru(
            "TEST_SUITE=$suite $phpUnit $configuration $arguments",
            $return
    );
    return $return;
}
