Added test for Catcher::unshiftHandler
This commit is contained in:
parent
7fe6607692
commit
2f08a7a800
2 changed files with 66 additions and 18 deletions
|
@ -82,17 +82,18 @@ class Catcher {
|
|||
foreach ($handlers as $v => $h) {
|
||||
if (in_array($h, $this->handlers, true)) {
|
||||
trigger_error("Handlers must be unique; skipping\n", \E_USER_WARNING);
|
||||
unset($handlers[$v]);
|
||||
$modified = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
unset($handlers[$v]);
|
||||
$modified = true;
|
||||
}
|
||||
if ($modified) {
|
||||
$handlers = array_values($handlers);
|
||||
}
|
||||
|
||||
$this->handlers = [ ...$handlers, ...$this->handlers ];
|
||||
if (count($handlers) > 0) {
|
||||
$this->handlers = [ ...$handlers, ...$this->handlers ];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -16,7 +16,6 @@ use MensBeam\Framework\Catcher\{
|
|||
|
||||
|
||||
class TestCatcher extends \PHPUnit\Framework\TestCase {
|
||||
|
||||
/**
|
||||
* @covers \MensBeam\Framework\Catcher::__construct()
|
||||
*
|
||||
|
@ -29,6 +28,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
|
|||
$c = new Catcher();
|
||||
$this->assertSame('MensBeam\Framework\Catcher', $c::class);
|
||||
$this->assertEquals(1, count($c->getHandlers()));
|
||||
$this->assertSame(PlainTextHandler::class, $c->getHandlers()[0]::class);
|
||||
$c->__destruct();
|
||||
|
||||
$c = new Catcher(
|
||||
|
@ -38,6 +38,10 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
|
|||
);
|
||||
$this->assertSame('MensBeam\Framework\Catcher', $c::class);
|
||||
$this->assertEquals(3, count($c->getHandlers()));
|
||||
$h = $c->getHandlers();
|
||||
$this->assertSame(PlainTextHandler::class, $h[0]::class);
|
||||
$this->assertSame(HTMLHandler::class, $h[1]::class);
|
||||
$this->assertSame(JSONHandler::class, $h[2]::class);
|
||||
$c->__destruct();
|
||||
}
|
||||
|
||||
|
@ -49,8 +53,9 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
|
|||
* @covers \MensBeam\Framework\Catcher\Handler::__construct()
|
||||
*/
|
||||
public function testMethod_pushHandler__warning(): void {
|
||||
set_error_handler(function($errno) {
|
||||
$this->assertEquals(\E_USER_WARNING, $errno);
|
||||
$e = null;
|
||||
set_error_handler(function($errno) use (&$e) {
|
||||
$e = $errno;
|
||||
});
|
||||
|
||||
$h = new PlainTextHandler();
|
||||
|
@ -58,6 +63,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
|
|||
$c->__destruct();
|
||||
|
||||
restore_error_handler();
|
||||
$this->assertEquals(\E_USER_WARNING, $e);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -65,6 +71,8 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
|
|||
*
|
||||
* @covers \MensBeam\Framework\Catcher::__construct()
|
||||
* @covers \MensBeam\Framework\Catcher::__destruct()
|
||||
* @covers \MensBeam\Framework\Catcher::getHandlers()
|
||||
* @covers \MensBeam\Framework\Catcher::removeHandler()
|
||||
* @covers \MensBeam\Framework\Catcher\Handler::__construct()
|
||||
*/
|
||||
public function testMethod_removeHandler(): void {
|
||||
|
@ -77,16 +85,8 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
|
|||
$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 {
|
||||
$e = null;
|
||||
try {
|
||||
$h = [
|
||||
new PlainTextHandler(),
|
||||
|
@ -94,10 +94,57 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
|
|||
];
|
||||
$c = new Catcher(...$h);
|
||||
$c->removeHandler(...$h);
|
||||
} catch (\Exception $e) {
|
||||
$this->assertSame(\Exception::class, $e::class);
|
||||
} catch (\Throwable $t) {
|
||||
$e = $t::class;
|
||||
} finally {
|
||||
$c->__destruct();
|
||||
$this->assertSame(\Exception::class, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \MensBeam\Framework\Catcher::setHandlers()
|
||||
*
|
||||
* @covers \MensBeam\Framework\Catcher::__construct()
|
||||
* @covers \MensBeam\Framework\Catcher::__destruct()
|
||||
* @covers \MensBeam\Framework\Catcher::getHandlers()
|
||||
* @covers \MensBeam\Framework\Catcher\Handler::__construct()
|
||||
*/
|
||||
public function testMethod_setHandlers(): void {
|
||||
$c = new Catcher();
|
||||
$c->setHandlers(new PlainTextHandler());
|
||||
$h = $c->getHandlers();
|
||||
$this->assertEquals(1, count($h));
|
||||
$this->assertSame(PlainTextHandler::class, $h[0]::class);
|
||||
$c->__destruct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \MensBeam\Framework\Catcher::unshiftHandler()
|
||||
*
|
||||
* @covers \MensBeam\Framework\Catcher::__construct()
|
||||
* @covers \MensBeam\Framework\Catcher::__destruct()
|
||||
* @covers \MensBeam\Framework\Catcher\Handler::__construct()
|
||||
*/
|
||||
public function testMethod_unshiftHandler(): void {
|
||||
$c = new Catcher(new PlainTextHandler());
|
||||
$c->unshiftHandler(new JSONHandler(), new HTMLHandler(), new PlainTextHandler());
|
||||
$h = $c->getHandlers();
|
||||
$this->assertEquals(4, count($h));
|
||||
$this->assertSame(JSONHandler::class, $h[0]::class);
|
||||
$this->assertSame(HTMLHandler::class, $h[1]::class);
|
||||
$this->assertSame(PlainTextHandler::class, $h[2]::class);
|
||||
$this->assertSame(PlainTextHandler::class, $h[3]::class);
|
||||
|
||||
$e = null;
|
||||
set_error_handler(function($errno) use (&$e) {
|
||||
$e = $errno;
|
||||
});
|
||||
|
||||
$c->unshiftHandler($h[0]);
|
||||
$c->__destruct();
|
||||
|
||||
restore_error_handler();
|
||||
$this->assertEquals(\E_USER_WARNING, $e);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue