Browse Source

Added test for Catcher::unshiftHandler

2.1.0
Dustin Wilson 2 years ago
parent
commit
2f08a7a800
  1. 9
      lib/Catcher.php
  2. 75
      tests/cases/TestCatcher.php

9
lib/Catcher.php

@ -82,17 +82,18 @@ class Catcher {
foreach ($handlers as $v => $h) { foreach ($handlers as $v => $h) {
if (in_array($h, $this->handlers, true)) { if (in_array($h, $this->handlers, true)) {
trigger_error("Handlers must be unique; skipping\n", \E_USER_WARNING); trigger_error("Handlers must be unique; skipping\n", \E_USER_WARNING);
unset($handlers[$v]);
$modified = true;
continue; continue;
} }
unset($handlers[$v]);
$modified = true;
} }
if ($modified) { if ($modified) {
$handlers = array_values($handlers); $handlers = array_values($handlers);
} }
$this->handlers = [ ...$handlers, ...$this->handlers ]; if (count($handlers) > 0) {
$this->handlers = [ ...$handlers, ...$this->handlers ];
}
} }

75
tests/cases/TestCatcher.php

@ -16,7 +16,6 @@ use MensBeam\Framework\Catcher\{
class TestCatcher extends \PHPUnit\Framework\TestCase { class TestCatcher extends \PHPUnit\Framework\TestCase {
/** /**
* @covers \MensBeam\Framework\Catcher::__construct() * @covers \MensBeam\Framework\Catcher::__construct()
* *
@ -29,6 +28,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
$c = new Catcher(); $c = new Catcher();
$this->assertSame('MensBeam\Framework\Catcher', $c::class); $this->assertSame('MensBeam\Framework\Catcher', $c::class);
$this->assertEquals(1, count($c->getHandlers())); $this->assertEquals(1, count($c->getHandlers()));
$this->assertSame(PlainTextHandler::class, $c->getHandlers()[0]::class);
$c->__destruct(); $c->__destruct();
$c = new Catcher( $c = new Catcher(
@ -38,6 +38,10 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
); );
$this->assertSame('MensBeam\Framework\Catcher', $c::class); $this->assertSame('MensBeam\Framework\Catcher', $c::class);
$this->assertEquals(3, count($c->getHandlers())); $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(); $c->__destruct();
} }
@ -49,8 +53,9 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
* @covers \MensBeam\Framework\Catcher\Handler::__construct() * @covers \MensBeam\Framework\Catcher\Handler::__construct()
*/ */
public function testMethod_pushHandler__warning(): void { public function testMethod_pushHandler__warning(): void {
set_error_handler(function($errno) { $e = null;
$this->assertEquals(\E_USER_WARNING, $errno); set_error_handler(function($errno) use (&$e) {
$e = $errno;
}); });
$h = new PlainTextHandler(); $h = new PlainTextHandler();
@ -58,6 +63,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
$c->__destruct(); $c->__destruct();
restore_error_handler(); 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::__construct()
* @covers \MensBeam\Framework\Catcher::__destruct() * @covers \MensBeam\Framework\Catcher::__destruct()
* @covers \MensBeam\Framework\Catcher::getHandlers()
* @covers \MensBeam\Framework\Catcher::removeHandler()
* @covers \MensBeam\Framework\Catcher\Handler::__construct() * @covers \MensBeam\Framework\Catcher\Handler::__construct()
*/ */
public function testMethod_removeHandler(): void { public function testMethod_removeHandler(): void {
@ -77,16 +85,8 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
$c->removeHandler($h); $c->removeHandler($h);
$this->assertEquals(1, count($c->getHandlers())); $this->assertEquals(1, count($c->getHandlers()));
$c->__destruct(); $c->__destruct();
}
/** $e = null;
* @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 { try {
$h = [ $h = [
new PlainTextHandler(), new PlainTextHandler(),
@ -94,10 +94,57 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
]; ];
$c = new Catcher(...$h); $c = new Catcher(...$h);
$c->removeHandler(...$h); $c->removeHandler(...$h);
} catch (\Exception $e) { } catch (\Throwable $t) {
$this->assertSame(\Exception::class, $e::class); $e = $t::class;
} finally { } finally {
$c->__destruct(); $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…
Cancel
Save