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

$path = dirname($argv[0]) . '/';
$root = substr($path, 0, -11);
$root = realpath($root);
$phpunit = './vendor/bin/phpunit -v --strict -c phpunit.xml.dist';

if (TRUE === extension_loaded('Xdebug')) {
	$phpunit .= ' --coverage-clover=build/logs/clover.xml';
	$logDir = $root . '/build/logs/';
	if (FALSE === is_dir($logDir)) {
		mkdir($logDir, 0755, TRUE);
	}
}

$descriptors = array(
	array("pipe", "r"),
	array("pipe", "w"),
	array("pipe", "r")
);
$pipes = array();

$process = proc_open($phpunit, $descriptors, $pipes, $root);
fclose($pipes[0]);

while ($char = fread($pipes[1], 8)) {
	echo $char;
}

$errors = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);

$exit = proc_close($process);
if (0 < $exit) {
	echo $errors;
	echo PHP_EOL;
}
exit($exit);
