f25f4ade6f78aa9768691a1fef2300d0ad761950
[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($p, &$errors, &$results) {
66 if (method_exists($p, "done")) {
67 $p->then(function($result) use(&$results) {
68 if (isset($result)) {
69 $results[] = $result;
70 }
71 }, function($error) use (&$errors) {
72 if (isset($error)) {
73 $errors[] = $error;
74 }
75 });
76 } else {
77 $p->onResolve(function($error, $result) use(&$errors, &$results) {
78 if (isset($error)) {
79 $errors[] = $error;
80 }
81 if (isset($result)) {
82 $results[] = $result;
83 }
84 });
85 }
86 }
87 }
88
89 trait AssertSuccess
90 {
91 function assertAllSuccess(array $apis, ...$args) {
92 foreach ($apis as $api) {
93 $this->consumePromise($api->get(...$args), $errors, $results);
94 }
95 $api->send();
96 $this->assertEmpty($errors, "errors");
97 $this->assertNotEmpty($results, "results");
98 return $results;
99 }
100
101 function assertSuccess(seekat\API $api, ...$args) {
102 $this->consumePromise($api->get(...$args), $errors, $results);
103 $api->send();
104 $this->assertEmpty($errors, "errors");
105 $this->assertNotEmpty($results, "results");
106 return $results[0];
107 }
108 }
109
110 trait AssertCancelled
111 {
112 function assertCancelled($promise) {
113 $this->consumePromise($promise, $errors, $results);
114
115 $this->assertEmpty($results);
116 $this->assertStringMatchesFormat("%SCancelled%S", \seekat\Exception\message($errors[0]));
117 }
118 }
119
120 trait AssertFailure
121 {
122 function assertFailure(seekat\API $api, ...$args) {
123 $this->consumePromise($api->get(...$args), $errors, $results);
124 $api->send();
125 $this->assertNotEmpty($errors, "errors");
126 $this->assertEmpty($results, "results");
127 return $errors[0];
128 }
129 }
130
131 class CombinedTestdoxPrinter extends PHPUnit_TextUI_ResultPrinter
132 {
133 function isTestClass(PHPUnit_Framework_TestSuite $suite) {
134 $suiteName = $suite->getName();
135 return false === strpos($suiteName, "::")
136 && substr($suiteName, -4) === "Test";
137 }
138
139 function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
140 if ($this->isTestClass($suite)) {
141 $this->column = 0;
142 }
143
144 return parent::startTestSuite($suite);
145 }
146
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;
153
154 --$this->numTestsRun;
155 $this->writeProgress(str_repeat(" ", $colWidth));
156 } else {
157 $this->writeNewLine();
158 }
159 }
160
161 parent::endTestSuite($suite);
162 }
163 }
164
165 class TestdoxListener extends PHPUnit_Util_TestDox_ResultPrinter_Text
166 {
167 private $groups;
168
169 function __construct() {
170 parent::__construct("php://stdout", ["testdox"]);
171 $this->groups = new ReflectionProperty("PHPUnit_Util_TestDox_ResultPrinter", "groups");
172 $this->groups->setAccessible(true);
173 }
174
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"]);
180 }
181 }
182
183 parent::startTest($test);
184 $this->groups->setValue($this, ["testdox"]);
185
186 }
187 }
188
189 class DebugLogListener extends PHPUnit\Framework\BaseTestListener
190 {
191 private $printLog = false;
192
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();
199 } else {
200 $handler->clear();
201 }
202 }
203
204 function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
205 $this->printLog = true;
206 }
207
208 function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
209 $this->printLog = true;
210 }
211
212 }