flush
[m6w6/seekat] / tests / bootstrap.php
1 <?php
2
3 require_once __DIR__."/../vendor/autoload.php";
4
5 function headers() : array {
6 static $headers;
7
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: ");
13 fflush(STDOUT);
14 if (strncasecmp(fgets(STDIN), "Y", 1)) {
15 exit;
16 }
17 $headers = [];
18 } else {
19 throw new Exception("GITHUB_TOKEN is not set in the environment");
20 }
21 }
22
23 return $headers;
24 }
25
26 function logger() : \Monolog\Logger {
27 static $logger;
28
29 if (!isset($logger)) {
30 $logger = new \Monolog\Logger(
31 "test",
32 [
33 new \Monolog\Handler\FingersCrossedHandler(
34 new \Monolog\Handler\StreamHandler(STDERR),
35 \Monolog\Logger::EMERGENCY
36 )
37 ]
38 );
39 }
40
41 return $logger;
42 }
43
44 class BaseTest extends \PHPUnit\Framework\TestCase
45 {
46 /**
47 * @return Generator
48 */
49 function provideAPI() {
50 $auth = \seekat\API\auth("token", getenv("GITHUB_TOKEN"));
51 $headers = headers();
52 $url = null;
53 $client = null;
54 $logger = logger();
55
56 return [
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)],
59 ];
60 }
61 }
62
63 trait ConsumePromise
64 {
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;
69 });
70 }
71 }
72
73 trait AssertSuccess
74 {
75 function assertAllSuccess(array $apis, ...$args) {
76 foreach ($apis as $api) {
77 $this->consumePromise($api->get(...$args), $errors, $results);
78 }
79 $api->send();
80 $this->assertEmpty($errors, "errors");
81 $this->assertNotEmpty($results, "results");
82 return $results;
83 }
84
85 function assertSuccess(seekat\API $api, ...$args) {
86 $this->consumePromise($api->get(...$args), $errors, $results);
87 $api->send();
88 $this->assertEmpty($errors, "errors");
89 $this->assertNotEmpty($results, "results");
90 return $results[0];
91 }
92 }
93
94 trait AssertCancelled
95 {
96 function assertCancelled(\AsyncInterop\Promise $promise) {
97 $this->consumePromise($promise, $errors, $results);
98
99 $this->assertEmpty($results);
100 $this->assertStringMatchesFormat("%SCancelled%S", $errors[0]->getMessage());
101 }
102 }
103
104 trait AssertFailure
105 {
106 function assertFailure(seekat\API $api, ...$args) {
107 $this->consumePromise($api->get(...$args), $errors, $results);
108 $api->send();
109 $this->assertNotEmpty($errors, "errors");
110 $this->assertEmpty($results, "results");
111 return $errors[0];
112 }
113 }
114
115 class CombinedTestdoxPrinter extends PHPUnit_TextUI_ResultPrinter
116 {
117 function isTestClass(PHPUnit_Framework_TestSuite $suite) {
118 $suiteName = $suite->getName();
119 return false === strpos($suiteName, "::")
120 && substr($suiteName, -4) === "Test";
121 }
122
123 function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
124 if ($this->isTestClass($suite)) {
125 $this->column = 0;
126 }
127
128 return parent::startTestSuite($suite);
129 }
130
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;
137
138 --$this->numTestsRun;
139 $this->writeProgress(str_repeat(" ", $colWidth));
140 } else {
141 $this->writeNewLine();
142 }
143 }
144
145 parent::endTestSuite($suite);
146 }
147 }
148
149 class TestdoxListener extends PHPUnit_Util_TestDox_ResultPrinter_Text
150 {
151 private $groups;
152
153 function __construct() {
154 parent::__construct("php://stdout", ["testdox"]);
155 $this->groups = new ReflectionProperty("PHPUnit_Util_TestDox_ResultPrinter", "groups");
156 $this->groups->setAccessible(true);
157 }
158
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"]);
164 }
165 }
166
167 parent::startTest($test);
168 $this->groups->setValue($this, ["testdox"]);
169
170 }
171 }
172
173 class DebugLogListener extends PHPUnit\Framework\BaseTestListener
174 {
175 private $printLog = false;
176
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();
183 } else {
184 $handler->clear();
185 }
186 }
187
188 function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
189 $this->printLog = true;
190 }
191
192 function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
193 $this->printLog = true;
194 }
195
196 }