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($p, &$errors, &$results) {
66 if (method_exists($p, "done")) {
67 $p->then(function($result) use(&$results) {
71 }, function($error) use (&$errors) {
77 $p->onResolve(function($error, $result) use(&$errors, &$results) {
91 function assertAllSuccess(array $apis, ...$args) {
92 foreach ($apis as $api) {
93 $this->consumePromise($api->get(...$args), $errors, $results);
96 $this->assertEmpty($errors, "errors");
97 $this->assertNotEmpty($results, "results");
101 function assertSuccess(seekat\API
$api, ...$args) {
102 $this->consumePromise($api->get(...$args), $errors, $results);
104 $this->assertEmpty($errors, "errors");
105 $this->assertNotEmpty($results, "results");
110 trait AssertCancelled
112 function assertCancelled($promise) {
113 $this->consumePromise($promise, $errors, $results);
115 $this->assertEmpty($results);
116 $this->assertStringMatchesFormat("%SCancelled%S", \seekat\Exception\
message($errors[0]));
122 function assertFailure(seekat\API
$api, ...$args) {
123 $this->consumePromise($api->get(...$args), $errors, $results);
125 $this->assertNotEmpty($errors, "errors");
126 $this->assertEmpty($results, "results");
131 class CombinedTestdoxPrinter
extends PHPUnit_TextUI_ResultPrinter
133 function isTestClass(PHPUnit_Framework_TestSuite
$suite) {
134 $suiteName = $suite->getName();
135 return false === strpos($suiteName, "::")
136 && substr($suiteName, -4) === "Test";
139 function startTestSuite(PHPUnit_Framework_TestSuite
$suite) {
140 if ($this->isTestClass($suite)) {
144 return parent
::startTestSuite($suite);
147 function endTestSuite(PHPUnit_Framework_TestSuite
$suite) {
148 /* print % progress */
149 if ($this->isTestClass($suite)) {
150 if ($this->numTestsRun
!= $this->numTests
) {
151 $colWidth = $this->maxColumn
- $this->column
;
152 $this->column
= $this->maxColumn
- 1;
154 --$this->numTestsRun
;
155 $this->writeProgress(str_repeat(" ", $colWidth));
157 $this->writeNewLine();
161 parent
::endTestSuite($suite);
165 class TestdoxListener
extends PHPUnit_Util_TestDox_ResultPrinter_Text
169 function __construct() {
170 parent
::__construct("php://stdout", ["testdox"]);
171 $this->groups
= new ReflectionProperty("PHPUnit_Util_TestDox_ResultPrinter", "groups");
172 $this->groups
->setAccessible(true);
175 function startTest(PHPUnit_Framework_Test
$test) {
176 /* always show test class, even if no testdox test */
177 if ($test instanceof \PHPUnit\Framework\TestCase
) {
178 if ($test->getGroups() == ["default"]) {
179 $this->groups
->setValue($this, ["default"]);
183 parent
::startTest($test);
184 $this->groups
->setValue($this, ["testdox"]);
189 class DebugLogListener
extends PHPUnit\Framework\BaseTestListener
191 private $printLog = false;
193 function endTest(PHPUnit_Framework_Test
$test, $time) {
194 /* @var $handler \Monolog\Handler\FingersCrossedHandler */
195 $handler = logger()->getHandlers()[0];
196 if ($this->printLog
) {
197 $this->printLog
= false;
198 $handler->activate();
204 function addError(PHPUnit_Framework_Test
$test, Exception
$e, $time) {
205 $this->printLog
= true;
208 function addFailure(PHPUnit_Framework_Test
$test, PHPUnit_Framework_AssertionFailedError
$e, $time) {
209 $this->printLog
= true;