#!/usr/bin/env php
<?php

use Swoole\Event;
use Swoole\Runtime;
use Yurun\Swoole\CoPool\CoPool;
use Yurun\Swoole\CoPool\Interfaces\ICoTask;
use Yurun\Swoole\CoPool\Interfaces\ITaskParam;

require dirname(__DIR__) . '/vendor/autoload.php';

function test()
{
    $descriptorspec = [
        ['pipe', 'r'],  // 标准输入，子进程从此管道中读取数据
        ['pipe', 'w'],  // 标准输出，子进程向此管道中写入数据
    ];
    $cmd = __DIR__ . '/phpunit -c ' . __DIR__ . '/phpunit.xml';
    $pipes = null;
    $processHndler = proc_open($cmd, $descriptorspec, $pipes);
    $records2 = [];
    while(!feof($pipes[1]))
    {
        $content = fgets($pipes[1]);
        if(false !== $content)
        {
            if(2 === count($records2))
            {
                array_shift($records2);
            }
            $records2[] = $content;
            echo $content;
        }
    }
    
    do {
        $status = proc_get_status($processHndler);
    } while($status['running'] ?? false);
    foreach($pipes as $pipe)
    {
        fclose($pipe);
    }
    proc_close($processHndler);
    
    if(version_compare(SWOOLE_VERSION, '4.4', '<') && 255 === ($status['exitcode'] ?? 0) && 'OK' === substr($records2[1] ?? '', 0, 2))
    {
        exit(0);
    }
    else
    {
        exit($status['exitcode'] ?? 0);
    }
}

test();
