Intl/RoboFile.php

152 lines
5.6 KiB
PHP
Raw Normal View History

2018-04-23 14:21:25 -04:00
<?php
2018-09-18 12:10:09 -04:00
declare(strict_types=1);
2018-04-23 14:21:25 -04:00
use Robo\Result;
2019-12-13 09:22:12 -05:00
const BASE = __DIR__.\DIRECTORY_SEPARATOR;
const BASE_TEST = BASE."tests".\DIRECTORY_SEPARATOR;
define("IS_WIN", defined("PHP_WINDOWS_VERSION_MAJOR"));
define("IS_MAC", php_uname("s") === "Darwin");
2018-04-23 14:21:25 -04:00
2019-12-13 09:22:12 -05:00
function norm(string $path): string {
$out = realpath($path);
if (!$out) {
$out = str_replace(["/", "\\"], \DIRECTORY_SEPARATOR, $path);
}
return $out;
}
class RoboFile extends \Robo\Tasks {
/** Runs the typical test suite
2018-04-23 14:21:25 -04:00
*
* Arguments passed to the task are passed on to PHPUnit. Thus one may, for
* example, run the following command and get the expected results:
*
* ./robo test --testsuite TTRSS --exclude-group slow --testdox
*
* Please see the PHPUnit documentation for available options.
*/
2018-04-23 14:21:25 -04:00
public function test(array $args): Result {
2019-12-13 09:22:12 -05:00
return $this->runTests(escapeshellarg(\PHP_BINARY), "typical", $args);
2018-04-23 14:21:25 -04:00
}
2019-12-13 09:22:12 -05:00
/** Runs the full test suite
2018-04-23 14:21:25 -04:00
*
2018-04-25 15:00:05 -04:00
* This includes pedantic tests which may help to identify problems.
2018-04-24 14:54:50 -04:00
* See help for the "test" task for more details.
*/
2018-04-23 14:21:25 -04:00
public function testFull(array $args): Result {
2019-12-13 09:22:12 -05:00
return $this->runTests(escapeshellarg(\PHP_BINARY), "full", $args);
2018-04-23 14:21:25 -04:00
}
/**
* Runs a quick subset of the test suite
*
* See help for the "test" task for more details.
*/
2018-04-23 14:21:25 -04:00
public function testQuick(array $args): Result {
2019-12-13 09:22:12 -05:00
return $this->runTests(escapeshellarg(\PHP_BINARY), "quick", $args);
2018-04-23 14:21:25 -04:00
}
/** Produces a code coverage report
*
* By default this task produces an HTML-format coverage report in
* tests/coverage/. Additional reports may be produced by passing
2018-04-23 14:21:25 -04:00
* arguments to this task as one would to PHPUnit.
*
* Robo first tries to use phpdbg and will fall back to Xdebug if available.
* Because Xdebug slows down non-coverage tasks, however, phpdbg is highly
* recommended if debugging facilities are not otherwise needed.
*/
2018-04-23 14:21:25 -04:00
public function coverage(array $args): Result {
// run tests with code coverage reporting enabled
$exec = $this->findCoverageEngine();
2019-12-13 09:22:12 -05:00
return $this->runTests($exec, "coverage", array_merge(["--coverage-html", BASE_TEST."coverage"], $args));
2018-04-23 14:21:25 -04:00
}
2018-04-25 00:42:29 -04:00
2019-12-13 09:22:12 -05:00
/** Produces a code coverage report, with redundant tests
2018-04-25 15:00:05 -04:00
*
2019-12-13 09:22:12 -05:00
* Depending on the environment, some tests that normally provide
* coverage may be skipped, while working alternatives are normally
* suppressed for reasons of time. This coverage report will try to
* run all tests which may cover code.
*
* See also help for the "coverage" task for more details.
*/
2019-12-13 09:22:12 -05:00
public function coverageFull(array $args): Result {
// run tests with code coverage reporting enabled
$exec = $this->findCoverageEngine();
return $this->runTests($exec, "typical", array_merge(["--coverage-html", BASE_TEST."coverage"], $args));
2018-04-25 00:42:29 -04:00
}
2018-04-23 14:21:25 -04:00
2018-04-25 15:00:05 -04:00
/** Runs the coding standards fixer */
public function clean($opts = ['demo|d' => false]): Result {
2018-09-18 12:10:09 -04:00
$t = $this->taskExec(realpath(BASE."vendor/bin/php-cs-fixer"));
$t->arg("fix")->arg("--allow-risky=yes");
2018-04-25 15:00:05 -04:00
if ($opts['demo']) {
$t->args("--dry-run", "--diff")->option("--diff-format", "udiff");
}
return $t->run();
}
2019-12-13 09:22:12 -05:00
/** Runs a performance evaluation.
*
* The performance of the library's basic functionality is tested against
* the IntlCodePointBreakIterator class
*/
2019-12-13 09:22:12 -05:00
public function perf(array $args): Result {
$execpath = realpath(norm(BASE."perf/perf.php"));
return $this->taskExec("php")->arg($execpath)->args($args)->run();
}
2018-04-23 14:21:25 -04:00
protected function findCoverageEngine(): string {
2020-10-01 10:09:16 -04:00
$dir = rtrim(ini_get("extension_dir"), "/").\DIRECTORY_SEPARATOR;
$ext = IS_WIN ? "dll" : (IS_MAC ? "dylib" : "so");
$php = escapeshellarg(\PHP_BINARY);
$code = escapeshellarg(BASE."lib");
if (extension_loaded("pcov")) {
return "$php -d pcov.enabled=1 -d pcov.directory=$code";
} elseif (extension_loaded("xdebug")) {
return $php;
} elseif (file_exists($dir."pcov.$ext")) {
return "$php -d extension=pcov.$ext -d pcov.enabled=1 -d pcov.directory=$code";
} elseif (file_exists($dir."pcov.$ext")) {
return "$php -d zend_extension=xdebug.$ext";
2018-04-23 14:21:25 -04:00
} else {
2020-10-01 10:09:16 -04:00
if (IS_WIN) {
$dbg = dirname(\PHP_BINARY)."\\phpdbg.exe";
$dbg = file_exists($dbg) ? $dbg : "";
} else {
$dbg = trim(`which phpdbg 2>/dev/null`);
}
if ($dbg) {
return escapeshellarg($dbg)." -qrr";
} else {
return $php;
}
2018-04-23 14:21:25 -04:00
}
}
2018-04-24 14:54:50 -04:00
protected function runTests(string $executor, string $set, array $args): Result {
2018-04-24 14:54:50 -04:00
switch ($set) {
case "typical":
$set = ["--exclude-group", "optional"];
break;
case "quick":
$set = ["--exclude-group", "optional,slow"];
break;
2019-12-13 09:22:12 -05:00
case "coverage":
$set = ["--exclude-group", "optional,coverageOptional"];
break;
2018-04-24 14:54:50 -04:00
case "full":
$set = [];
break;
default:
throw new \Exception;
}
2019-12-13 09:22:12 -05:00
$execpath = norm(BASE."vendor-bin/phpunit/vendor/phpunit/phpunit/phpunit");
$confpath = realpath(BASE_TEST."phpunit.dist.xml") ?: norm(BASE_TEST."phpunit.xml");
return $this->taskExec($executor)->option("-d", "zend.assertions=1")->arg($execpath)->option("-c", $confpath)->args(array_merge($set, $args))->run();
2018-04-24 14:54:50 -04:00
}
2018-04-23 14:21:25 -04:00
}