Dustin Wilson
2 years ago
7 changed files with 2165 additions and 102 deletions
File diff suppressed because it is too large
@ -0,0 +1,117 @@ |
|||
#!/usr/bin/env php |
|||
<?php |
|||
|
|||
$cwd = __DIR__; |
|||
$codeDir = "$cwd/lib"; |
|||
$testDir = "$cwd/tests"; |
|||
|
|||
function help($error = true): void { |
|||
$help = <<<USAGE |
|||
Usage: |
|||
run test [typical|quick|coverage|full] [additional_phpunit_options] |
|||
run --help |
|||
|
|||
USAGE; |
|||
|
|||
if ($error) { |
|||
fprintf(\STDERR, $help); |
|||
} else { |
|||
echo $help; |
|||
} |
|||
|
|||
exit((int)$error); |
|||
} |
|||
|
|||
function error(string $message): void { |
|||
fprintf(\STDERR, "ERROR: $message\n"); |
|||
exit(1); |
|||
} |
|||
|
|||
if (count($argv) === 1) { |
|||
help(); |
|||
} |
|||
|
|||
switch ($argv[1]) { |
|||
case 'test': |
|||
$opts = [ |
|||
'--colors', |
|||
'--coverage-html='.escapeshellarg("$testDir/coverage") |
|||
]; |
|||
|
|||
$argv[2] = $argv[2] ?? ''; |
|||
switch ($argv[2]) { |
|||
case '': |
|||
case 'typical': |
|||
$opts[] = '--exclude-group=optional'; |
|||
break; |
|||
case 'quick': |
|||
$opts[] = '--exclude-group=optional,slow'; |
|||
break; |
|||
case 'coverage': |
|||
$opts[] = '--exclude-group=optional,coverageOptional'; |
|||
break; |
|||
case 'full': |
|||
break; |
|||
default: |
|||
help(); |
|||
} |
|||
|
|||
if (isset($argv[3])) { |
|||
$opts = [ ...$opts, array_slice($argv, 3) ]; |
|||
} |
|||
|
|||
$opts = implode(' ', $opts); |
|||
break; |
|||
case '-h': |
|||
case '--help': |
|||
help(false); |
|||
break; |
|||
default: |
|||
help(); |
|||
} |
|||
|
|||
$phpunitPath = escapeshellarg("$cwd/vendor/bin/phpunit"); |
|||
$confPath = "$testDir/phpunit.dist.xml"; |
|||
if (!file_exists($confPath)) { |
|||
$confPath = "$testDir/phpunit.xml"; |
|||
if (!file_exists($confPath)) { |
|||
error('A phpunit configuration must be present at "tests/phpunit.dist.xml" or "tests/phpunit.xml"; aborting'); |
|||
} |
|||
} |
|||
$confPath = escapeshellarg($confPath); |
|||
|
|||
$cmd = [ |
|||
escapeshellarg(\PHP_BINARY), |
|||
'-d opcache.enable_cli=0', |
|||
'-d zend.assertions=1' |
|||
]; |
|||
if (!extension_loaded('xdebug')) { |
|||
if (file_exists("$extDir/xdebug.so")) { |
|||
$cmd[] = '-d extension=xdebug.so'; |
|||
} else { |
|||
error('Xdebug is not installed on your system; aborting'); |
|||
} |
|||
} |
|||
$cmd[] = '-d xdebug.mode=coverage,trace'; |
|||
$cmd = implode(' ', $cmd); |
|||
|
|||
$process = proc_open("$cmd $phpunitPath -c $confPath $opts", [ |
|||
1 => ['pipe', 'w'], |
|||
2 => ['pipe', 'w'] |
|||
], $pipes); |
|||
|
|||
if ($process === false) { |
|||
error('Failed to execute phpunit'); |
|||
} |
|||
|
|||
$stderr = trim(stream_get_contents($pipes[2])); |
|||
$output = trim(stream_get_contents($pipes[1])); |
|||
|
|||
fclose($pipes[1]); |
|||
fclose($pipes[2]); |
|||
proc_close($process); |
|||
|
|||
echo "$output\n"; |
|||
if ($stderr !== '') { |
|||
error($stderr); |
|||
} |
@ -0,0 +1,24 @@ |
|||
<?php |
|||
/** @license MIT |
|||
* Copyright 2017 , Dustin Wilson, J. King et al. |
|||
* See LICENSE and AUTHORS files for details */ |
|||
|
|||
declare(strict_types=1); |
|||
namespace MensBeam\Framework; |
|||
|
|||
ini_set('memory_limit', '-1'); |
|||
ini_set('zend.assertions', '1'); |
|||
ini_set('assert.exception', 'true'); |
|||
error_reporting(\E_ALL); |
|||
|
|||
$cwd = dirname(__DIR__); |
|||
$docRoot = "$cwd/tests/docroot"; |
|||
require_once "$cwd/vendor/autoload.php"; |
|||
|
|||
if (function_exists('xdebug_set_filter')) { |
|||
if (defined('XDEBUG_PATH_INCLUDE')) { |
|||
xdebug_set_filter(\XDEBUG_FILTER_CODE_COVERAGE, \XDEBUG_PATH_INCLUDE, [ "$cwd/lib/" ]); |
|||
} else { |
|||
xdebug_set_filter(\XDEBUG_FILTER_CODE_COVERAGE, \XDEBUG_PATH_WHITELIST, [ "$cwd/lib/" ]); |
|||
} |
|||
} |
@ -0,0 +1,103 @@ |
|||
<?php |
|||
/** |
|||
* @license MIT |
|||
* Copyright 2022 Dustin Wilson, et al. |
|||
* See LICENSE and AUTHORS files for details |
|||
*/ |
|||
|
|||
declare(strict_types=1); |
|||
namespace MensBeam\Framework\TestCase; |
|||
use MensBeam\Framework\Catcher; |
|||
use MensBeam\Framework\Catcher\{ |
|||
PlainTextHandler, |
|||
HTMLHandler, |
|||
JSONHandler |
|||
}; |
|||
|
|||
|
|||
class TestCatcher extends \PHPUnit\Framework\TestCase { |
|||
|
|||
/** |
|||
* @covers \MensBeam\Framework\Catcher::__construct() |
|||
* |
|||
* @covers \MensBeam\Framework\Catcher::getHandlers() |
|||
* @covers \MensBeam\Framework\Catcher::pushHandler() |
|||
* @covers \MensBeam\Framework\Catcher::__destruct() |
|||
* @covers \MensBeam\Framework\Catcher\Handler::__construct() |
|||
*/ |
|||
public function testMethod___construct(): void { |
|||
$c = new Catcher(); |
|||
$this->assertSame('MensBeam\Framework\Catcher', $c::class); |
|||
$this->assertEquals(1, count($c->getHandlers())); |
|||
$c->__destruct(); |
|||
|
|||
$c = new Catcher( |
|||
new PlainTextHandler(), |
|||
new HTMLHandler(), |
|||
new JSONHandler() |
|||
); |
|||
$this->assertSame('MensBeam\Framework\Catcher', $c::class); |
|||
$this->assertEquals(3, count($c->getHandlers())); |
|||
$c->__destruct(); |
|||
} |
|||
|
|||
/** |
|||
* @covers \MensBeam\Framework\Catcher::pushHandler() |
|||
* |
|||
* @covers \MensBeam\Framework\Catcher::__construct() |
|||
* @covers \MensBeam\Framework\Catcher::__destruct() |
|||
* @covers \MensBeam\Framework\Catcher\Handler::__construct() |
|||
*/ |
|||
public function testMethod_pushHandler__warning(): void { |
|||
set_error_handler(function($errno) { |
|||
$this->assertEquals(\E_USER_WARNING, $errno); |
|||
}); |
|||
|
|||
$h = new PlainTextHandler(); |
|||
$c = new Catcher($h, $h); |
|||
$c->__destruct(); |
|||
|
|||
restore_error_handler(); |
|||
} |
|||
|
|||
/** |
|||
* @covers \MensBeam\Framework\Catcher::removeHandler() |
|||
* |
|||
* @covers \MensBeam\Framework\Catcher::__construct() |
|||
* @covers \MensBeam\Framework\Catcher::__destruct() |
|||
* @covers \MensBeam\Framework\Catcher\Handler::__construct() |
|||
*/ |
|||
public function testMethod_removeHandler(): void { |
|||
$h = new HTMLHandler(); |
|||
$c = new Catcher( |
|||
new PlainTextHandler(), |
|||
$h |
|||
); |
|||
$this->assertEquals(2, count($c->getHandlers())); |
|||
$c->removeHandler($h); |
|||
$this->assertEquals(1, count($c->getHandlers())); |
|||
$c->__destruct(); |
|||
} |
|||
|
|||
/** |
|||
* @covers \MensBeam\Framework\Catcher::removeHandler() |
|||
* |
|||
* @covers \MensBeam\Framework\Catcher::__construct() |
|||
* @covers \MensBeam\Framework\Catcher::__destruct() |
|||
* @covers \MensBeam\Framework\Catcher\Handler::__construct() |
|||
*/ |
|||
public function testMethod_removeHandler__exception(): void { |
|||
try { |
|||
$h = [ |
|||
new PlainTextHandler(), |
|||
new HTMLHandler(), |
|||
]; |
|||
$c = new Catcher(...$h); |
|||
$c->removeHandler(...$h); |
|||
} catch (\Exception $e) { |
|||
$this->assertSame(\Exception::class, $e::class); |
|||
} finally { |
|||
$c->__destruct(); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,22 @@ |
|||
<?xml version="1.0"?> |
|||
<phpunit |
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" |
|||
colors="true" |
|||
bootstrap="bootstrap.php" |
|||
convertErrorsToExceptions="false" |
|||
convertNoticesToExceptions="false" |
|||
convertWarningsToExceptions="false" |
|||
beStrictAboutTestsThatDoNotTestAnything="true" |
|||
forceCoversAnnotation="true"> |
|||
<coverage processUncoveredFiles="true"> |
|||
<include> |
|||
<directory suffix=".php">../lib</directory> |
|||
</include> |
|||
</coverage> |
|||
<testsuites> |
|||
<testsuite name="Main"> |
|||
<file>cases/TestCatcher.php</file> |
|||
</testsuite> |
|||
</testsuites> |
|||
</phpunit> |
Loading…
Reference in new issue