#!/usr/bin/env php parser = $parser; } public function afterTraverse(array $nodes) { $main = array_pop($nodes); /** @var Node\Stmt\Expression $main */ $main = $main->expr; $main = $main->expr; $require = array_pop($nodes); $nodes[] = $this->getPrependSetter()[0]; $nodes[] = $require; $nodes[] = new Node\Stmt\Expression(new Node\Expr\Assign( new Node\Expr\Variable('code'), new Node\Scalar\LNumber(0) )); $nodes[] = $this->getCoroutineSetter()[0]; $nodes[] = new Node\Stmt\Expression( new Node\Expr\FuncCall( new Node\Name('Swoole\Coroutine\run'), [ new Node\Arg(new Node\Expr\Closure([ 'stmts' => [ new Node\Stmt\Expression(new Node\Expr\Assign( new Node\Expr\Variable('code'), $main )), $this->getTimerClearAll()[0], $this->getCoordinatorResume()[0], ], 'uses' => [ new Node\Expr\ClosureUse(new Node\Expr\Variable('code'), true), ], ])), ] ) ); $nodes[] = new Node\Stmt\Expression(new Node\Expr\Exit_(new Node\Expr\Variable('code'))); return $nodes; } protected function getPrependSetter(): array { return $this->parser->parse(' $argv) { // --prepend /path/to/file if ($argv === "--prepend") { unset($_SERVER["argv"][$index]); if (isset($_SERVER["argv"][$index + 1])) { $prepend = $_SERVER["argv"][$index + 1]; unset($_SERVER["argv"][$index + 1]); } break; } // --prepend=/path/to/file if (strpos($argv, "--prepend=") === 0) { $prepend = substr($argv, 10); unset($_SERVER["argv"][$index]); break; } } if ($prepend !== null && file_exists($prepend)) { require $prepend; } })(); '); } protected function getCoroutineSetter(): array { return $this->parser->parse(" SWOOLE_HOOK_ALL, 'exit_condition' => function() { return Swoole\\Coroutine::stats()['coroutine_num'] === 0; } ]);"); } protected function getTimerClearAll(): array { return $this->parser->parse('parser->parse('resume();'); } } $parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7); $printer = new Standard(); $traverser = new NodeTraverser(); $code = file_get_contents(__DIR__ . '/../vendor/phpunit/phpunit/phpunit'); $stmts = $parser->parse($code); $traverser->addVisitor(new RunInCoroutineVisitor($parser)); $stmts = $traverser->traverse($stmts); $code = $printer->prettyPrint($stmts); // TODO: Unknown reason $code = ltrim($code, '?>' . PHP_EOL); file_put_contents(__DIR__ . '/co-phpunit', $code); file_put_contents(__DIR__ . '/../src/testing/co-phpunit', $code);