3 require_once __DIR__
."/../vendor/autoload.php";
5 function headers() : array {
8 if (!isset($headers)) {
9 if (($token = getenv("GITHUB_TOKEN"))) {
10 $headers["Authorization"] = "token $token";
11 } elseif (function_exists("posix_isatty") && defined("STDIN") && posix_isatty(STDIN
)) {
12 fprintf(STDOUT
, "GITHUB_TOKEN is not set in the environment, enter Y to continue without: ");
14 if (strncasecmp(fgets(STDIN
), "Y", 1)) {
19 throw new Exception("GITHUB_TOKEN is not set in the environment");
26 function logger() : \Monolog\Logger
{
29 if (!isset($logger)) {
30 $logger = new \Monolog\
Logger(
33 new \Monolog\Handler\
FingersCrossedHandler(
34 new \Monolog\Handler\
StreamHandler(STDERR
),
35 \Monolog\Logger
::EMERGENCY
44 class BaseTest
extends \PHPUnit\Framework\TestCase
49 function provideAPI() {
50 $auth = \seekat\API\auth
("token", getenv("GITHUB_TOKEN"));
57 "with ReactPHP" => [new \seekat\
API(\seekat\API\Future\react
(), $headers, $url, $client, $logger)],
58 "with AmPHP" => [new \seekat\
API(\seekat\API\Future\amp
(), $headers, $url, $client, $logger)],
65 function consumePromise(\AsyncInterop\Promise
$p, &$errors, &$results) {
66 $p->when(function($error, $result) use(&$errors, &$results) {
67 if ($error) $errors[] = $error;
68 if ($result) $results[] = $result;
75 function assertAllSuccess(array $apis, ...$args) {
76 foreach ($apis as $api) {
77 $this->consumePromise($api->get(...$args), $errors, $results);
80 $this->assertEmpty($errors, "errors");
81 $this->assertNotEmpty($results, "results");
85 function assertSuccess(seekat\API
$api, ...$args) {
86 $this->consumePromise($api->get(...$args), $errors, $results);
88 $this->assertEmpty($errors, "errors");
89 $this->assertNotEmpty($results, "results");
96 function assertCancelled(\AsyncInterop\Promise
$promise) {
97 $this->consumePromise($promise, $errors, $results);
99 $this->assertEmpty($results);
100 $this->assertStringMatchesFormat("%SCancelled%S", $errors[0]->getMessage());
106 function assertFailure(seekat\API
$api, ...$args) {
107 $this->consumePromise($api->get(...$args), $errors, $results);
109 $this->assertNotEmpty($errors, "errors");
110 $this->assertEmpty($results, "results");
115 class CombinedTestdoxPrinter
extends PHPUnit_TextUI_ResultPrinter
117 function isTestClass(PHPUnit_Framework_TestSuite
$suite) {
118 $suiteName = $suite->getName();
119 return false === strpos($suiteName, "::")
120 && substr($suiteName, -4) === "Test";
123 function startTestSuite(PHPUnit_Framework_TestSuite
$suite) {
124 if ($this->isTestClass($suite)) {
128 return parent
::startTestSuite($suite);
131 function endTestSuite(PHPUnit_Framework_TestSuite
$suite) {
132 /* print % progress */
133 if ($this->isTestClass($suite)) {
134 if ($this->numTestsRun
!= $this->numTests
) {
135 $colWidth = $this->maxColumn
- $this->column
;
136 $this->column
= $this->maxColumn
- 1;
138 --$this->numTestsRun
;
139 $this->writeProgress(str_repeat(" ", $colWidth));
141 $this->writeNewLine();
145 parent
::endTestSuite($suite);
149 class TestdoxListener
extends PHPUnit_Util_TestDox_ResultPrinter_Text
153 function __construct() {
154 parent
::__construct("php://stdout", ["testdox"]);
155 $this->groups
= new ReflectionProperty("PHPUnit_Util_TestDox_ResultPrinter", "groups");
156 $this->groups
->setAccessible(true);
159 function startTest(PHPUnit_Framework_Test
$test) {
160 /* always show test class, even if no testdox test */
161 if ($test instanceof \PHPUnit\Framework\TestCase
) {
162 if ($test->getGroups() == ["default"]) {
163 $this->groups
->setValue($this, ["default"]);
167 parent
::startTest($test);
168 $this->groups
->setValue($this, ["testdox"]);
173 class DebugLogListener
extends PHPUnit\Framework\BaseTestListener
175 private $printLog = false;
177 function endTest(PHPUnit_Framework_Test
$test, $time) {
178 /* @var $handler \Monolog\Handler\FingersCrossedHandler */
179 $handler = logger()->getHandlers()[0];
180 if ($this->printLog
) {
181 $this->printLog
= false;
182 $handler->activate();
188 function addError(PHPUnit_Framework_Test
$test, Exception
$e, $time) {
189 $this->printLog
= true;
192 function addFailure(PHPUnit_Framework_Test
$test, PHPUnit_Framework_AssertionFailedError
$e, $time) {
193 $this->printLog
= true;