flush
[m6w6/seekat] / peridot.php
1 <?php
2
3 use Monolog\Handler\AbstractProcessingHandler;
4 use Monolog\Logger;
5 use Peridot\Cli\Environment;
6 use Peridot\Cli\Application;
7 use Peridot\Configuration;
8 use Peridot\Core\Suite;
9 use Peridot\Core\Test;
10 use Peridot\Plugin\Scenarios;
11 use seekat\API;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14
15 return function(\Peridot\EventEmitterInterface $emitter) {
16 Scenarios\Plugin::register($emitter);
17
18 $emitter->on("peridot.start", function(Environment $env, Application $app) {
19 $app->setCatchExceptions(false);
20 $definition = $env->getDefinition();
21 $definition->getArgument("path")
22 ->setDefault(implode(" ", glob("tests/*")));
23 });
24
25 $log = new class extends AbstractProcessingHandler {
26 private $records = [];
27 protected function write(array $record) {
28 $this->records[] = $record["formatted"];
29 }
30 function clean() {
31 $this->records = [];
32 }
33 function dump(OutputInterface $output) {
34 if ($this->records) {
35 $output->writeln(["\n", "Debug log:", "==========="]);
36 $output->write($this->records);
37 $this->clean();
38 }
39 }
40 };
41 $emitter->on("suite.start", function(Suite $suite) use(&$headers, $log) {
42 $headers = [];
43 if (($token = getenv("GITHUB_TOKEN"))) {
44 $headers["Authorization"] = "token $token";
45 } elseif (function_exists("posix_isatty") && defined("STDIN") && posix_isatty(STDIN)) {
46 fprintf(STDOUT, "GITHUB_TOKEN is not set in the environment, enter Y to continue without: ");
47 fflush(STDOUT);
48 if (strncasecmp(fgets(STDIN), "Y", 1)) {
49 exit;
50 }
51 } else {
52 throw new Exception("GITHUB_TOKEN is not set in the environment");
53 }
54 $suite->getScope()->amp = new API(API\Future\amp(),
55 $headers, null, null, new Logger("amp", [$log]));
56 $suite->getScope()->react = new API(API\Future\react(),
57 $headers, null, null, new Logger("react", [$log]));
58 });
59
60 $emitter->on("test.failed", function(Test $test, \Throwable $e) {
61
62 });
63 $emitter->on("test.passed", function() use($log) {
64 $log->clean();
65 });
66 $emitter->on("peridot.end", function($exitCode, InputInterface $input, OutputInterface $output) use($log) {
67 $log->dump($output);
68 });
69 };