compatibility with 2.6.0 and 3.1.0
[m6w6/seekat] / peridot.php
1 <?php
2
3 use Evenement\EventEmitterInterface as EventEmitter;
4 use Monolog\Handler\AbstractProcessingHandler;
5 use Monolog\Logger;
6 use Peridot\Configuration;
7 use Peridot\Console\Application;
8 use Peridot\Console\Environment;
9 use Peridot\Core\Suite;
10 use Peridot\Core\Test;
11 use seekat\API;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Peridot\Reporter\CodeCoverage\AbstractCodeCoverageReporter;
15 use Peridot\Reporter\CodeCoverageReporters;
16 use Peridot\Reporter\ReporterFactory;
17 use Peridot\Reporter\AnonymousReporter;
18 use Peridot\Reporter\AbstractBaseReporter;
19
20 return function(EventEmitter $emitter) {
21 (new CodeCoverageReporters($emitter))->register();
22
23 $emitter->on('peridot.reporters', function(InputInterface $input, ReporterFactory $reporterFactory) {
24 $reporterFactory->register(
25 'seekat',
26 'Spec + Text Code coverage reporter',
27 function(AnonymousReporter $ar) use ($reporterFactory) {
28
29 return new class($reporterFactory, $ar->getConfiguration(), $ar->getOutput(), $ar->getEventEmitter()) extends AbstractBaseReporter {
30 private $reporters = [];
31
32 function __construct(ReporterFactory $factory, Configuration $configuration, OutputInterface $output, EventEmitter $eventEmitter) {
33 fprintf(STDERR, "Creating reporters\n");
34 $this->reporters[] = $factory->create("spec");
35 $this->reporters[] = $factory->create("text-code-coverage");
36 parent::__construct($configuration, $output, $eventEmitter);
37 }
38
39 function init() {
40 }
41 function __2call($method, array $args) {
42 fprintf(STDERR, "Calling %s\n", $method);
43 foreach ($this->reporters as $reporter) {
44 $output = $reporter->$method(...$args);
45 }
46 return $output;
47 }
48 };
49 }
50 );
51 });
52
53 $emitter->on('code-coverage.start', function (AbstractCodeCoverageReporter $reporter) {
54 $reporter->addDirectoryToWhitelist(__DIR__."/lib")
55 ->addDirectoryToWhitelist(__DIR__."/tests");
56 });
57
58 $emitter->on("peridot.start", function(Environment $env, Application $app) {
59 $app->setCatchExceptions(false);
60 $definition = $env->getDefinition();
61 $definition->getArgument("path")
62 ->setDefault(implode(" ", glob("tests/*")));
63 $definition->getOption("reporter")
64 ->setDefault("seekat");
65 });
66
67 $log = new class extends AbstractProcessingHandler {
68 private $records = [];
69 protected function write(array $record) {
70 $this->records[] = $record["formatted"];
71 }
72 function clean() {
73 $this->records = [];
74 }
75 function dump(OutputInterface $output) {
76 if ($this->records) {
77 $output->writeln(["\n", "Debug log:", "==========="]);
78 $output->write($this->records);
79 $this->clean();
80 }
81 }
82 };
83 $emitter->on("suite.start", function(Suite $suite) use($log) {
84 $headers = [];
85 if (($token = getenv("GITHUB_TOKEN"))) {
86 $headers["Authentication"] = "token $token";
87 } elseif (function_exists("posix_isatty") && defined("STDIN") && posix_isatty(STDIN)) {
88 fprintf(STDOUT, "GITHUB_TOKEN is not set in the environment, enter Y to continue without: ");
89 fflush(STDOUT);
90 if (strncasecmp(fgets(STDIN), "Y", 1)) {
91 exit;
92 }
93 } else {
94 throw new Exception("GITHUB_TOKEN is not set in the environment");
95 }
96 $suite->getScope()->api = new API($headers, null, null, new Logger("seekat", [$log]));
97 });
98
99 $emitter->on("test.failed", function(Test $test, \Throwable $e) {
100
101 });
102 $emitter->on("test.passed", function() use($log) {
103 $log->clean();
104 });
105 $emitter->on("peridot.end", function($exitCode, InputInterface $input, OutputInterface $output) use($log) {
106 $log->dump($output);
107 });
108 };