diff --git a/tests/Monolog/Formatter/GelfMessageFormatterTest.php b/tests/Monolog/Formatter/GelfMessageFormatterTest.php index 9be4b3e6..3d58f58b 100644 --- a/tests/Monolog/Formatter/GelfMessageFormatterTest.php +++ b/tests/Monolog/Formatter/GelfMessageFormatterTest.php @@ -86,14 +86,14 @@ class GelfMessageFormatterTest extends TestCase */ public function testFormatInvalidFails() { - $this->expectException(\InvalidArgumentException::class); - $formatter = new GelfMessageFormatter(); $record = [ 'level' => Logger::ERROR, 'level_name' => 'ERROR', ]; + $this->expectException(\InvalidArgumentException::class); + $formatter->format($record); } diff --git a/tests/Monolog/Formatter/NormalizerFormatterTest.php b/tests/Monolog/Formatter/NormalizerFormatterTest.php index bc918543..1725cd7e 100644 --- a/tests/Monolog/Formatter/NormalizerFormatterTest.php +++ b/tests/Monolog/Formatter/NormalizerFormatterTest.php @@ -260,8 +260,6 @@ class NormalizerFormatterTest extends TestCase public function testThrowsOnInvalidEncoding() { - $this->expectException(\RuntimeException::class); - $formatter = new NormalizerFormatter(); $reflMethod = new \ReflectionMethod($formatter, 'toJson'); $reflMethod->setAccessible(true); @@ -269,6 +267,9 @@ class NormalizerFormatterTest extends TestCase // send an invalid unicode sequence as a object that can't be cleaned $record = new \stdClass; $record->message = "\xB1\x31"; + + $this->expectException(\RuntimeException::class); + $reflMethod->invoke($formatter, $record); } diff --git a/tests/Monolog/Handler/AbstractProcessingHandlerTest.php b/tests/Monolog/Handler/AbstractProcessingHandlerTest.php index 0d3736d0..7d732b39 100644 --- a/tests/Monolog/Handler/AbstractProcessingHandlerTest.php +++ b/tests/Monolog/Handler/AbstractProcessingHandlerTest.php @@ -96,8 +96,6 @@ class AbstractProcessingHandlerTest extends TestCase */ public function testPushPopProcessor() { - $this->expectException(\LogicException::class); - $logger = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler'); $processor1 = new WebProcessor; $processor2 = new WebProcessor; @@ -107,6 +105,9 @@ class AbstractProcessingHandlerTest extends TestCase $this->assertEquals($processor2, $logger->popProcessor()); $this->assertEquals($processor1, $logger->popProcessor()); + + $this->expectException(\LogicException::class); + $logger->popProcessor(); } @@ -115,10 +116,10 @@ class AbstractProcessingHandlerTest extends TestCase */ public function testPushProcessorWithNonCallable() { - $this->expectException(\TypeError::class); - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler'); + $this->expectException(\TypeError::class); + $handler->pushProcessor(new \stdClass()); } diff --git a/tests/Monolog/Handler/ElasticaHandlerTest.php b/tests/Monolog/Handler/ElasticaHandlerTest.php index c456ec13..453752f2 100644 --- a/tests/Monolog/Handler/ElasticaHandlerTest.php +++ b/tests/Monolog/Handler/ElasticaHandlerTest.php @@ -100,11 +100,12 @@ class ElasticaHandlerTest extends TestCase */ public function testSetFormatterInvalid() { + $handler = new ElasticaHandler($this->client); + $formatter = new NormalizerFormatter(); + $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('ElasticaHandler is only compatible with ElasticaFormatter'); - $handler = new ElasticaHandler($this->client); - $formatter = new NormalizerFormatter(); $handler->setFormatter($formatter); } diff --git a/tests/Monolog/Handler/ElasticsearchHandlerTest.php b/tests/Monolog/Handler/ElasticsearchHandlerTest.php index 06599e45..2cc982b3 100644 --- a/tests/Monolog/Handler/ElasticsearchHandlerTest.php +++ b/tests/Monolog/Handler/ElasticsearchHandlerTest.php @@ -112,11 +112,12 @@ class ElasticsearchHandlerTest extends TestCase */ public function testSetFormatterInvalid() { + $handler = new ElasticsearchHandler($this->client); + $formatter = new NormalizerFormatter(); + $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('ElasticsearchHandler is only compatible with ElasticsearchFormatter'); - $handler = new ElasticsearchHandler($this->client); - $formatter = new NormalizerFormatter(); $handler->setFormatter($formatter); } diff --git a/tests/Monolog/Handler/FilterHandlerTest.php b/tests/Monolog/Handler/FilterHandlerTest.php index db715719..cacbd33a 100644 --- a/tests/Monolog/Handler/FilterHandlerTest.php +++ b/tests/Monolog/Handler/FilterHandlerTest.php @@ -162,13 +162,14 @@ class FilterHandlerTest extends TestCase */ public function testHandleWithBadCallbackThrowsException() { - $this->expectException(\RuntimeException::class); - $handler = new FilterHandler( function ($record, $handler) { return 'foo'; } ); + + $this->expectException(\RuntimeException::class); + $handler->handle($this->getRecord(Logger::WARNING)); } } diff --git a/tests/Monolog/Handler/FingersCrossedHandlerTest.php b/tests/Monolog/Handler/FingersCrossedHandlerTest.php index 75dc49af..c649f00d 100644 --- a/tests/Monolog/Handler/FingersCrossedHandlerTest.php +++ b/tests/Monolog/Handler/FingersCrossedHandlerTest.php @@ -132,11 +132,12 @@ class FingersCrossedHandlerTest extends TestCase */ public function testHandleWithBadCallbackThrowsException() { - $this->expectException(\RuntimeException::class); - $handler = new FingersCrossedHandler(function ($record, $handler) { return 'foo'; }); + + $this->expectException(\RuntimeException::class); + $handler->handle($this->getRecord(Logger::WARNING)); } diff --git a/tests/Monolog/Handler/NewRelicHandlerTest.php b/tests/Monolog/Handler/NewRelicHandlerTest.php index e4e4d70b..a02a6efe 100644 --- a/tests/Monolog/Handler/NewRelicHandlerTest.php +++ b/tests/Monolog/Handler/NewRelicHandlerTest.php @@ -30,9 +30,10 @@ class NewRelicHandlerTest extends TestCase public function testThehandlerThrowsAnExceptionIfTheNRExtensionIsNotLoaded() { + $handler = new StubNewRelicHandlerWithoutExtension(); + $this->expectException(MissingExtensionException::class); - $handler = new StubNewRelicHandlerWithoutExtension(); $handler->handle($this->getRecord(Logger::ERROR)); } diff --git a/tests/Monolog/Handler/ProcessHandlerTest.php b/tests/Monolog/Handler/ProcessHandlerTest.php index c78d5590..fc1aff47 100644 --- a/tests/Monolog/Handler/ProcessHandlerTest.php +++ b/tests/Monolog/Handler/ProcessHandlerTest.php @@ -135,7 +135,7 @@ class ProcessHandlerTest extends TestCase ->method('selectErrorStream') ->will($this->returnValue(false)); - $this->expectException('\UnexpectedValueException'); + $this->expectException(\UnexpectedValueException::class); /** @var ProcessHandler $handler */ $handler->handle($this->getRecord(Logger::WARNING, 'stream failing, whoops')); } @@ -147,7 +147,9 @@ class ProcessHandlerTest extends TestCase public function testStartupWithErrorsThrowsUnexpectedValueException() { $handler = new ProcessHandler('>&2 echo "some fake error message"'); - $this->expectException('\UnexpectedValueException'); + + $this->expectException(\UnexpectedValueException::class); + $handler->handle($this->getRecord(Logger::WARNING, 'some warning in the house')); } @@ -167,7 +169,7 @@ class ProcessHandlerTest extends TestCase ->method('readProcessErrors') ->willReturnOnConsecutiveCalls('', $this->returnValue('some fake error message here')); - $this->expectException('\UnexpectedValueException'); + $this->expectException(\UnexpectedValueException::class); /** @var ProcessHandler $handler */ $handler->handle($this->getRecord(Logger::WARNING, 'some test stuff')); } diff --git a/tests/Monolog/Handler/SocketHandlerTest.php b/tests/Monolog/Handler/SocketHandlerTest.php index 48a2642a..f1377f07 100644 --- a/tests/Monolog/Handler/SocketHandlerTest.php +++ b/tests/Monolog/Handler/SocketHandlerTest.php @@ -90,54 +90,57 @@ class SocketHandlerTest extends TestCase public function testExceptionIsThrownOnFsockopenError() { - $this->expectException(\UnexpectedValueException::class); - $this->setMockHandler(['fsockopen']); $this->handler->expects($this->once()) ->method('fsockopen') ->will($this->returnValue(false)); + + $this->expectException(\UnexpectedValueException::class); + $this->writeRecord('Hello world'); } public function testExceptionIsThrownOnPfsockopenError() { - $this->expectException(\UnexpectedValueException::class); - $this->setMockHandler(['pfsockopen']); $this->handler->expects($this->once()) ->method('pfsockopen') ->will($this->returnValue(false)); + $this->handler->setPersistent(true); + + $this->expectException(\UnexpectedValueException::class); + $this->writeRecord('Hello world'); } public function testExceptionIsThrownIfCannotSetTimeout() { - $this->expectException(\UnexpectedValueException::class); - $this->setMockHandler(['streamSetTimeout']); $this->handler->expects($this->once()) ->method('streamSetTimeout') ->will($this->returnValue(false)); + + $this->expectException(\UnexpectedValueException::class); + $this->writeRecord('Hello world'); } public function testExceptionIsThrownIfCannotSetChunkSize() { - $this->expectException(\UnexpectedValueException::class); - $this->setMockHandler(array('streamSetChunkSize')); $this->handler->setChunkSize(8192); $this->handler->expects($this->once()) ->method('streamSetChunkSize') ->will($this->returnValue(false)); + + $this->expectException(\UnexpectedValueException::class); + $this->writeRecord('Hello world'); } public function testWriteFailsOnIfFwriteReturnsFalse() { - $this->expectException(\RuntimeException::class); - $this->setMockHandler(['fwrite']); $callback = function ($arg) { @@ -153,13 +156,13 @@ class SocketHandlerTest extends TestCase ->method('fwrite') ->will($this->returnCallback($callback)); + $this->expectException(\RuntimeException::class); + $this->writeRecord('Hello world'); } public function testWriteFailsIfStreamTimesOut() { - $this->expectException(\RuntimeException::class); - $this->setMockHandler(['fwrite', 'streamGetMetadata']); $callback = function ($arg) { @@ -178,13 +181,13 @@ class SocketHandlerTest extends TestCase ->method('streamGetMetadata') ->will($this->returnValue(['timed_out' => true])); + $this->expectException(\RuntimeException::class); + $this->writeRecord('Hello world'); } public function testWriteFailsOnIncompleteWrite() { - $this->expectException(\RuntimeException::class); - $this->setMockHandler(['fwrite', 'streamGetMetadata']); $res = $this->res; @@ -201,6 +204,8 @@ class SocketHandlerTest extends TestCase ->method('streamGetMetadata') ->will($this->returnValue(['timed_out' => false])); + $this->expectException(\RuntimeException::class); + $this->writeRecord('Hello world'); } @@ -255,8 +260,6 @@ class SocketHandlerTest extends TestCase public function testAvoidInfiniteLoopWhenNoDataIsWrittenForAWritingTimeoutSeconds() { - $this->expectException(\RuntimeException::class); - $this->setMockHandler(['fwrite', 'streamGetMetadata']); $this->handler->expects($this->any()) @@ -269,6 +272,8 @@ class SocketHandlerTest extends TestCase $this->handler->setWritingTimeout(1); + $this->expectException(\RuntimeException::class); + $this->writeRecord('Hello world'); } diff --git a/tests/Monolog/Handler/SyslogHandlerTest.php b/tests/Monolog/Handler/SyslogHandlerTest.php index 550e2105..5479d547 100644 --- a/tests/Monolog/Handler/SyslogHandlerTest.php +++ b/tests/Monolog/Handler/SyslogHandlerTest.php @@ -38,7 +38,7 @@ class SyslogHandlerTest extends \PHPUnit\Framework\TestCase */ public function testConstructInvalidFacility() { - $this->expectException('UnexpectedValueException'); + $this->expectException(\UnexpectedValueException::class); $handler = new SyslogHandler('test', 'unknown'); } } diff --git a/tests/Monolog/LoggerTest.php b/tests/Monolog/LoggerTest.php index deaa2c33..0163fafa 100644 --- a/tests/Monolog/LoggerTest.php +++ b/tests/Monolog/LoggerTest.php @@ -142,8 +142,6 @@ class LoggerTest extends \PHPUnit\Framework\TestCase */ public function testPushPopHandler() { - $this->expectException(\LogicException::class); - $logger = new Logger(__METHOD__); $handler1 = new TestHandler; $handler2 = new TestHandler; @@ -153,6 +151,9 @@ class LoggerTest extends \PHPUnit\Framework\TestCase $this->assertEquals($handler2, $logger->popHandler()); $this->assertEquals($handler1, $logger->popHandler()); + + $this->expectException(\LogicException::class); + $logger->popHandler(); } @@ -186,8 +187,6 @@ class LoggerTest extends \PHPUnit\Framework\TestCase */ public function testPushPopProcessor() { - $this->expectException(\LogicException::class); - $logger = new Logger(__METHOD__); $processor1 = new WebProcessor; $processor2 = new WebProcessor; @@ -197,6 +196,9 @@ class LoggerTest extends \PHPUnit\Framework\TestCase $this->assertEquals($processor2, $logger->popProcessor()); $this->assertEquals($processor1, $logger->popProcessor()); + + $this->expectException(\LogicException::class); + $logger->popProcessor(); } @@ -598,8 +600,6 @@ class LoggerTest extends \PHPUnit\Framework\TestCase */ public function testDefaultHandleException() { - $this->expectException(\Exception::class); - $logger = new Logger(__METHOD__); $handler = $this->getMockBuilder('Monolog\Handler\HandlerInterface')->getMock(); $handler->expects($this->any()) @@ -610,6 +610,9 @@ class LoggerTest extends \PHPUnit\Framework\TestCase ->method('handle') ->will($this->throwException(new \Exception('Some handler exception'))) ; + + $this->expectException(\Exception::class); + $logger->pushHandler($handler); $logger->info('test'); } diff --git a/tests/Monolog/RegistryTest.php b/tests/Monolog/RegistryTest.php index b917de86..0ada5ee6 100644 --- a/tests/Monolog/RegistryTest.php +++ b/tests/Monolog/RegistryTest.php @@ -142,13 +142,13 @@ class RegistryTest extends \PHPUnit\Framework\TestCase */ public function testFailsOnUnspecifiedReplacement() { - $this->expectException(\InvalidArgumentException::class); - $log1 = new Logger('test1'); $log2 = new Logger('test2'); Registry::addLogger($log1, 'log'); + $this->expectException(\InvalidArgumentException::class); + Registry::addLogger($log2, 'log'); } }