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() {
51 "using ReactPHP" => [new \seekat\
API(\seekat\API\Future\react
(), $headers, logger
: $logger)],
52 "using AmPHP" => [new \seekat\
API(\seekat\API\Future\amp
(), $headers, logger
: $logger)],
59 function consumePromise($p, &$errors, &$results) {
60 if (method_exists($p, "done")) {
61 $p->then(function($result) use(&$results) {
65 }, function($error) use (&$errors) {
71 $p->onResolve(function($error, $result) use(&$errors, &$results) {
85 function assertAllSuccess(array $apis, ...$args) {
86 foreach ($apis as $api) {
87 $this->consumePromise($api->get(...$args), $errors, $results);
90 $this->assertEmpty($errors, "errors");
91 $this->assertNotEmpty($results, "results");
95 function assertSuccess(seekat\API
$api, ...$args) {
96 $this->consumePromise($api->get(...$args), $errors, $results);
98 $this->assertEmpty($errors, "errors");
99 $this->assertNotEmpty($results, "results");
104 trait AssertCancelled
106 function assertCancelled($promise) {
107 $this->consumePromise($promise, $errors, $results);
109 $this->assertEmpty($results);
110 $this->assertStringMatchesFormat("%SCancelled%S", \seekat\Exception\
message($errors[0]));
116 function assertFailure(seekat\API
$api, ...$args) {
117 $this->consumePromise($api->get(...$args), $errors, $results);
119 $this->assertNotEmpty($errors, "errors");
120 $this->assertEmpty($results, "results");
125 class CombinedTestdoxPrinter
extends PHPUnit\TextUI\DefaultResultPrinter
127 function isTestClass(PHPUnit\Framework\TestSuite
$suite) {
128 $suiteName = $suite->getName();
129 return false === strpos($suiteName, "::")
130 && substr($suiteName, -4) === "Test";
133 function startTestSuite(PHPUnit\Framework\TestSuite
$suite) : void
{
134 if ($this->isTestClass($suite)) {
138 parent
::startTestSuite($suite);
141 function endTestSuite(PHPUnit\Framework\TestSuite
$suite) : void
{
142 /* print % progress */
143 if ($this->isTestClass($suite)) {
144 if ($this->numTestsRun
!= $this->numTests
) {
145 $colWidth = $this->maxColumn
- $this->column
;
146 $this->column
= $this->maxColumn
- 1;
148 --$this->numTestsRun
;
149 $this->writeProgress(str_repeat(" ", $colWidth));
151 $this->writeNewLine();
155 parent
::endTestSuite($suite);
159 class TestdoxListener
implements PHPUnit\Framework\TestListener
164 function __construct() {
165 $this->testdox
= new PHPUnit\Util\TestDox\
TextResultPrinter("php://stdout", ["testdox"]);
166 $this->groups
= new ReflectionProperty("\PHPUnit\Util\TestDox\ResultPrinter", "groups");
167 $this->groups
->setAccessible(true);
170 function startTest(PHPUnit\Framework\Test
$test) : void
{
171 /* always show test class, even if no testdox test */
172 if ($test instanceof \PHPUnit\Framework\TestCase
) {
173 if ($test->getGroups() == ["default"]) {
174 $this->groups
->setValue($this->testdox
, ["default"]);
178 $this->testdox
->startTest($test);
179 $this->groups
->setValue($this->testdox
, ["testdox"]);
182 public function addError(\PHPUnit\Framework\Test
$test, Throwable
$t, float $time): void
184 $this->testdox
->addError($test, $t, $time);
187 public function addWarning(\PHPUnit\Framework\Test
$test, \PHPUnit\Framework\Warning
$e, float $time): void
189 $this->testdox
->addWarning($test, $e, $time);
192 public function addFailure(\PHPUnit\Framework\Test
$test, \PHPUnit\Framework\AssertionFailedError
$e, float $time): void
194 $this->testdox
->addFailure($test, $e, $time);
197 public function addIncompleteTest(\PHPUnit\Framework\Test
$test, Throwable
$t, float $time): void
199 $this->testdox
->addIncompleteTest($test, $t, $time);
202 public function addRiskyTest(\PHPUnit\Framework\Test
$test, Throwable
$t, float $time): void
204 $this->testdox
->addRiskyTest($test, $t, $time);
207 public function addSkippedTest(\PHPUnit\Framework\Test
$test, Throwable
$t, float $time): void
209 $this->testdox
->addSkippedTest($test, $t, $time);
212 public function startTestSuite(\PHPUnit\Framework\TestSuite
$suite): void
214 $this->testdox
->startTestSuite($suite);
217 public function endTestSuite(\PHPUnit\Framework\TestSuite
$suite): void
219 $this->testdox
->endTestSuite($suite);
222 public function endTest(\PHPUnit\Framework\Test
$test, float $time): void
224 $this->testdox
->endTest($test, $time);
228 class DebugLogListener
implements \PHPUnit\Framework\TestListener
230 use \PHPUnit\Framework\TestListenerDefaultImplementation
;
232 private $printLog = false;
234 function endTest(PHPUnit\Framework\Test
$test, float $time) : void
{
235 /* @var $handler \Monolog\Handler\FingersCrossedHandler */
236 $handler = logger()->getHandlers()[0];
237 if ($this->printLog
) {
238 $this->printLog
= false;
239 $handler->activate();
245 function addError(PHPUnit\Framework\Test
$test, \Throwable
$e, float $time) : void
{
246 $this->printLog
= true;
249 function addFailure(PHPUnit\Framework\Test
$test, PHPUnit\Framework\AssertionFailedError
$e, float $time) : void
{
250 $this->printLog
= true;