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
46 function provideAPI() {
47 $auth = \seekat\API\auth
("token", getenv("GITHUB_TOKEN"));
54 "using ReactPHP" => [new \seekat\
API(\seekat\API\Future\react
(), $headers, $url, $client, $logger)],
55 "using AmPHP" => [new \seekat\
API(\seekat\API\Future\amp
(), $headers, $url, $client, $logger)],
62 function consumePromise($p, &$errors, &$results) {
63 if (method_exists($p, "done")) {
64 $p->then(function($result) use(&$results) {
68 }, function($error) use (&$errors) {
74 $p->onResolve(function($error, $result) use(&$errors, &$results) {
88 function assertAllSuccess(array $apis, ...$args) {
89 foreach ($apis as $api) {
90 $this->consumePromise($api->get(...$args), $errors, $results);
93 $this->assertEmpty($errors, "errors");
94 $this->assertNotEmpty($results, "results");
98 function assertSuccess(seekat\API
$api, ...$args) {
99 $this->consumePromise($api->get(...$args), $errors, $results);
101 $this->assertEmpty($errors, "errors");
102 $this->assertNotEmpty($results, "results");
107 trait AssertCancelled
109 function assertCancelled($promise) {
110 $this->consumePromise($promise, $errors, $results);
112 $this->assertEmpty($results);
113 $this->assertStringMatchesFormat("%SCancelled%S", \seekat\Exception\
message($errors[0]));
119 function assertFailure(seekat\API
$api, ...$args) {
120 $this->consumePromise($api->get(...$args), $errors, $results);
122 $this->assertNotEmpty($errors, "errors");
123 $this->assertEmpty($results, "results");
128 class CombinedTestdoxPrinter
extends PHPUnit\TextUI\DefaultResultPrinter
130 function isTestClass(PHPUnit\Framework\TestSuite
$suite) {
131 $suiteName = $suite->getName();
132 return false === strpos($suiteName, "::")
133 && substr($suiteName, -4) === "Test";
136 function startTestSuite(PHPUnit\Framework\TestSuite
$suite) : void
{
137 if ($this->isTestClass($suite)) {
141 parent
::startTestSuite($suite);
144 function endTestSuite(PHPUnit\Framework\TestSuite
$suite) : void
{
145 /* print % progress */
146 if ($this->isTestClass($suite)) {
147 if ($this->numTestsRun
!= $this->numTests
) {
148 $colWidth = $this->maxColumn
- $this->column
;
149 $this->column
= $this->maxColumn
- 1;
151 --$this->numTestsRun
;
152 $this->writeProgress(str_repeat(" ", $colWidth));
154 $this->writeNewLine();
158 parent
::endTestSuite($suite);
162 class TestdoxListener
implements PHPUnit\Framework\TestListener
167 function __construct() {
168 $this->testdox
= new PHPUnit\Util\TestDox\
TextResultPrinter("php://stdout", ["testdox"]);
169 $this->groups
= new ReflectionProperty("\PHPUnit\Util\TestDox\ResultPrinter", "groups");
170 $this->groups
->setAccessible(true);
173 function startTest(PHPUnit\Framework\Test
$test) : void
{
174 /* always show test class, even if no testdox test */
175 if ($test instanceof \PHPUnit\Framework\TestCase
) {
176 if ($test->getGroups() == ["default"]) {
177 $this->groups
->setValue($this->testdox
, ["default"]);
181 $this->testdox
->startTest($test);
182 $this->groups
->setValue($this->testdox
, ["testdox"]);
185 public function addError(\PHPUnit\Framework\Test
$test, Throwable
$t, float $time): void
187 $this->testdox
->addError($test, $t, $time);
190 public function addWarning(\PHPUnit\Framework\Test
$test, \PHPUnit\Framework\Warning
$e, float $time): void
192 $this->testdox
->addWarning($test, $e, $time);
195 public function addFailure(\PHPUnit\Framework\Test
$test, \PHPUnit\Framework\AssertionFailedError
$e, float $time): void
197 $this->testdox
->addFailure($test, $e, $time);
200 public function addIncompleteTest(\PHPUnit\Framework\Test
$test, Throwable
$t, float $time): void
202 $this->testdox
->addIncompleteTest($test, $t, $time);
205 public function addRiskyTest(\PHPUnit\Framework\Test
$test, Throwable
$t, float $time): void
207 $this->testdox
->addRiskyTest($test, $t, $time);
210 public function addSkippedTest(\PHPUnit\Framework\Test
$test, Throwable
$t, float $time): void
212 $this->testdox
->addSkippedTest($test, $t, $time);
215 public function startTestSuite(\PHPUnit\Framework\TestSuite
$suite): void
217 $this->testdox
->startTestSuite($suite);
220 public function endTestSuite(\PHPUnit\Framework\TestSuite
$suite): void
222 $this->testdox
->endTestSuite($suite);
225 public function endTest(\PHPUnit\Framework\Test
$test, float $time): void
227 $this->testdox
->endTest($test, $time);
231 class DebugLogListener
implements \PHPUnit\Framework\TestListener
233 use \PHPUnit\Framework\TestListenerDefaultImplementation
;
235 private $printLog = false;
237 function endTest(PHPUnit\Framework\Test
$test, float $time) : void
{
238 /* @var $handler \Monolog\Handler\FingersCrossedHandler */
239 $handler = logger()->getHandlers()[0];
240 if ($this->printLog
) {
241 $this->printLog
= false;
242 $handler->activate();
248 function addError(PHPUnit\Framework\Test
$test, \Throwable
$e, float $time) : void
{
249 $this->printLog
= true;
252 function addFailure(PHPUnit\Framework\Test
$test, PHPUnit\Framework\AssertionFailedError
$e, float $time) : void
{
253 $this->printLog
= true;