1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 12:47:39 +02:00

Move expectException before the line where exception is thrown

This commit is contained in:
Mario Blažek
2019-08-13 15:34:48 +02:00
parent 37900f9268
commit d317cb97d3
13 changed files with 62 additions and 45 deletions

View File

@@ -86,14 +86,14 @@ class GelfMessageFormatterTest extends TestCase
*/ */
public function testFormatInvalidFails() public function testFormatInvalidFails()
{ {
$this->expectException(\InvalidArgumentException::class);
$formatter = new GelfMessageFormatter(); $formatter = new GelfMessageFormatter();
$record = [ $record = [
'level' => Logger::ERROR, 'level' => Logger::ERROR,
'level_name' => 'ERROR', 'level_name' => 'ERROR',
]; ];
$this->expectException(\InvalidArgumentException::class);
$formatter->format($record); $formatter->format($record);
} }

View File

@@ -260,8 +260,6 @@ class NormalizerFormatterTest extends TestCase
public function testThrowsOnInvalidEncoding() public function testThrowsOnInvalidEncoding()
{ {
$this->expectException(\RuntimeException::class);
$formatter = new NormalizerFormatter(); $formatter = new NormalizerFormatter();
$reflMethod = new \ReflectionMethod($formatter, 'toJson'); $reflMethod = new \ReflectionMethod($formatter, 'toJson');
$reflMethod->setAccessible(true); $reflMethod->setAccessible(true);
@@ -269,6 +267,9 @@ class NormalizerFormatterTest extends TestCase
// send an invalid unicode sequence as a object that can't be cleaned // send an invalid unicode sequence as a object that can't be cleaned
$record = new \stdClass; $record = new \stdClass;
$record->message = "\xB1\x31"; $record->message = "\xB1\x31";
$this->expectException(\RuntimeException::class);
$reflMethod->invoke($formatter, $record); $reflMethod->invoke($formatter, $record);
} }

View File

@@ -96,8 +96,6 @@ class AbstractProcessingHandlerTest extends TestCase
*/ */
public function testPushPopProcessor() public function testPushPopProcessor()
{ {
$this->expectException(\LogicException::class);
$logger = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler'); $logger = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler');
$processor1 = new WebProcessor; $processor1 = new WebProcessor;
$processor2 = new WebProcessor; $processor2 = new WebProcessor;
@@ -107,6 +105,9 @@ class AbstractProcessingHandlerTest extends TestCase
$this->assertEquals($processor2, $logger->popProcessor()); $this->assertEquals($processor2, $logger->popProcessor());
$this->assertEquals($processor1, $logger->popProcessor()); $this->assertEquals($processor1, $logger->popProcessor());
$this->expectException(\LogicException::class);
$logger->popProcessor(); $logger->popProcessor();
} }
@@ -115,10 +116,10 @@ class AbstractProcessingHandlerTest extends TestCase
*/ */
public function testPushProcessorWithNonCallable() public function testPushProcessorWithNonCallable()
{ {
$this->expectException(\TypeError::class);
$handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler'); $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler');
$this->expectException(\TypeError::class);
$handler->pushProcessor(new \stdClass()); $handler->pushProcessor(new \stdClass());
} }

View File

@@ -100,11 +100,12 @@ class ElasticaHandlerTest extends TestCase
*/ */
public function testSetFormatterInvalid() public function testSetFormatterInvalid()
{ {
$handler = new ElasticaHandler($this->client);
$formatter = new NormalizerFormatter();
$this->expectException(\InvalidArgumentException::class); $this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('ElasticaHandler is only compatible with ElasticaFormatter'); $this->expectExceptionMessage('ElasticaHandler is only compatible with ElasticaFormatter');
$handler = new ElasticaHandler($this->client);
$formatter = new NormalizerFormatter();
$handler->setFormatter($formatter); $handler->setFormatter($formatter);
} }

View File

@@ -112,11 +112,12 @@ class ElasticsearchHandlerTest extends TestCase
*/ */
public function testSetFormatterInvalid() public function testSetFormatterInvalid()
{ {
$handler = new ElasticsearchHandler($this->client);
$formatter = new NormalizerFormatter();
$this->expectException(\InvalidArgumentException::class); $this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('ElasticsearchHandler is only compatible with ElasticsearchFormatter'); $this->expectExceptionMessage('ElasticsearchHandler is only compatible with ElasticsearchFormatter');
$handler = new ElasticsearchHandler($this->client);
$formatter = new NormalizerFormatter();
$handler->setFormatter($formatter); $handler->setFormatter($formatter);
} }

View File

@@ -162,13 +162,14 @@ class FilterHandlerTest extends TestCase
*/ */
public function testHandleWithBadCallbackThrowsException() public function testHandleWithBadCallbackThrowsException()
{ {
$this->expectException(\RuntimeException::class);
$handler = new FilterHandler( $handler = new FilterHandler(
function ($record, $handler) { function ($record, $handler) {
return 'foo'; return 'foo';
} }
); );
$this->expectException(\RuntimeException::class);
$handler->handle($this->getRecord(Logger::WARNING)); $handler->handle($this->getRecord(Logger::WARNING));
} }
} }

View File

@@ -132,11 +132,12 @@ class FingersCrossedHandlerTest extends TestCase
*/ */
public function testHandleWithBadCallbackThrowsException() public function testHandleWithBadCallbackThrowsException()
{ {
$this->expectException(\RuntimeException::class);
$handler = new FingersCrossedHandler(function ($record, $handler) { $handler = new FingersCrossedHandler(function ($record, $handler) {
return 'foo'; return 'foo';
}); });
$this->expectException(\RuntimeException::class);
$handler->handle($this->getRecord(Logger::WARNING)); $handler->handle($this->getRecord(Logger::WARNING));
} }

View File

@@ -30,9 +30,10 @@ class NewRelicHandlerTest extends TestCase
public function testThehandlerThrowsAnExceptionIfTheNRExtensionIsNotLoaded() public function testThehandlerThrowsAnExceptionIfTheNRExtensionIsNotLoaded()
{ {
$handler = new StubNewRelicHandlerWithoutExtension();
$this->expectException(MissingExtensionException::class); $this->expectException(MissingExtensionException::class);
$handler = new StubNewRelicHandlerWithoutExtension();
$handler->handle($this->getRecord(Logger::ERROR)); $handler->handle($this->getRecord(Logger::ERROR));
} }

View File

@@ -135,7 +135,7 @@ class ProcessHandlerTest extends TestCase
->method('selectErrorStream') ->method('selectErrorStream')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$this->expectException('\UnexpectedValueException'); $this->expectException(\UnexpectedValueException::class);
/** @var ProcessHandler $handler */ /** @var ProcessHandler $handler */
$handler->handle($this->getRecord(Logger::WARNING, 'stream failing, whoops')); $handler->handle($this->getRecord(Logger::WARNING, 'stream failing, whoops'));
} }
@@ -147,7 +147,9 @@ class ProcessHandlerTest extends TestCase
public function testStartupWithErrorsThrowsUnexpectedValueException() public function testStartupWithErrorsThrowsUnexpectedValueException()
{ {
$handler = new ProcessHandler('>&2 echo "some fake error message"'); $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')); $handler->handle($this->getRecord(Logger::WARNING, 'some warning in the house'));
} }
@@ -167,7 +169,7 @@ class ProcessHandlerTest extends TestCase
->method('readProcessErrors') ->method('readProcessErrors')
->willReturnOnConsecutiveCalls('', $this->returnValue('some fake error message here')); ->willReturnOnConsecutiveCalls('', $this->returnValue('some fake error message here'));
$this->expectException('\UnexpectedValueException'); $this->expectException(\UnexpectedValueException::class);
/** @var ProcessHandler $handler */ /** @var ProcessHandler $handler */
$handler->handle($this->getRecord(Logger::WARNING, 'some test stuff')); $handler->handle($this->getRecord(Logger::WARNING, 'some test stuff'));
} }

View File

@@ -90,54 +90,57 @@ class SocketHandlerTest extends TestCase
public function testExceptionIsThrownOnFsockopenError() public function testExceptionIsThrownOnFsockopenError()
{ {
$this->expectException(\UnexpectedValueException::class);
$this->setMockHandler(['fsockopen']); $this->setMockHandler(['fsockopen']);
$this->handler->expects($this->once()) $this->handler->expects($this->once())
->method('fsockopen') ->method('fsockopen')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$this->expectException(\UnexpectedValueException::class);
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }
public function testExceptionIsThrownOnPfsockopenError() public function testExceptionIsThrownOnPfsockopenError()
{ {
$this->expectException(\UnexpectedValueException::class);
$this->setMockHandler(['pfsockopen']); $this->setMockHandler(['pfsockopen']);
$this->handler->expects($this->once()) $this->handler->expects($this->once())
->method('pfsockopen') ->method('pfsockopen')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$this->handler->setPersistent(true); $this->handler->setPersistent(true);
$this->expectException(\UnexpectedValueException::class);
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }
public function testExceptionIsThrownIfCannotSetTimeout() public function testExceptionIsThrownIfCannotSetTimeout()
{ {
$this->expectException(\UnexpectedValueException::class);
$this->setMockHandler(['streamSetTimeout']); $this->setMockHandler(['streamSetTimeout']);
$this->handler->expects($this->once()) $this->handler->expects($this->once())
->method('streamSetTimeout') ->method('streamSetTimeout')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$this->expectException(\UnexpectedValueException::class);
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }
public function testExceptionIsThrownIfCannotSetChunkSize() public function testExceptionIsThrownIfCannotSetChunkSize()
{ {
$this->expectException(\UnexpectedValueException::class);
$this->setMockHandler(array('streamSetChunkSize')); $this->setMockHandler(array('streamSetChunkSize'));
$this->handler->setChunkSize(8192); $this->handler->setChunkSize(8192);
$this->handler->expects($this->once()) $this->handler->expects($this->once())
->method('streamSetChunkSize') ->method('streamSetChunkSize')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$this->expectException(\UnexpectedValueException::class);
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }
public function testWriteFailsOnIfFwriteReturnsFalse() public function testWriteFailsOnIfFwriteReturnsFalse()
{ {
$this->expectException(\RuntimeException::class);
$this->setMockHandler(['fwrite']); $this->setMockHandler(['fwrite']);
$callback = function ($arg) { $callback = function ($arg) {
@@ -153,13 +156,13 @@ class SocketHandlerTest extends TestCase
->method('fwrite') ->method('fwrite')
->will($this->returnCallback($callback)); ->will($this->returnCallback($callback));
$this->expectException(\RuntimeException::class);
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }
public function testWriteFailsIfStreamTimesOut() public function testWriteFailsIfStreamTimesOut()
{ {
$this->expectException(\RuntimeException::class);
$this->setMockHandler(['fwrite', 'streamGetMetadata']); $this->setMockHandler(['fwrite', 'streamGetMetadata']);
$callback = function ($arg) { $callback = function ($arg) {
@@ -178,13 +181,13 @@ class SocketHandlerTest extends TestCase
->method('streamGetMetadata') ->method('streamGetMetadata')
->will($this->returnValue(['timed_out' => true])); ->will($this->returnValue(['timed_out' => true]));
$this->expectException(\RuntimeException::class);
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }
public function testWriteFailsOnIncompleteWrite() public function testWriteFailsOnIncompleteWrite()
{ {
$this->expectException(\RuntimeException::class);
$this->setMockHandler(['fwrite', 'streamGetMetadata']); $this->setMockHandler(['fwrite', 'streamGetMetadata']);
$res = $this->res; $res = $this->res;
@@ -201,6 +204,8 @@ class SocketHandlerTest extends TestCase
->method('streamGetMetadata') ->method('streamGetMetadata')
->will($this->returnValue(['timed_out' => false])); ->will($this->returnValue(['timed_out' => false]));
$this->expectException(\RuntimeException::class);
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }
@@ -255,8 +260,6 @@ class SocketHandlerTest extends TestCase
public function testAvoidInfiniteLoopWhenNoDataIsWrittenForAWritingTimeoutSeconds() public function testAvoidInfiniteLoopWhenNoDataIsWrittenForAWritingTimeoutSeconds()
{ {
$this->expectException(\RuntimeException::class);
$this->setMockHandler(['fwrite', 'streamGetMetadata']); $this->setMockHandler(['fwrite', 'streamGetMetadata']);
$this->handler->expects($this->any()) $this->handler->expects($this->any())
@@ -269,6 +272,8 @@ class SocketHandlerTest extends TestCase
$this->handler->setWritingTimeout(1); $this->handler->setWritingTimeout(1);
$this->expectException(\RuntimeException::class);
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }

View File

@@ -38,7 +38,7 @@ class SyslogHandlerTest extends \PHPUnit\Framework\TestCase
*/ */
public function testConstructInvalidFacility() public function testConstructInvalidFacility()
{ {
$this->expectException('UnexpectedValueException'); $this->expectException(\UnexpectedValueException::class);
$handler = new SyslogHandler('test', 'unknown'); $handler = new SyslogHandler('test', 'unknown');
} }
} }

View File

@@ -142,8 +142,6 @@ class LoggerTest extends \PHPUnit\Framework\TestCase
*/ */
public function testPushPopHandler() public function testPushPopHandler()
{ {
$this->expectException(\LogicException::class);
$logger = new Logger(__METHOD__); $logger = new Logger(__METHOD__);
$handler1 = new TestHandler; $handler1 = new TestHandler;
$handler2 = new TestHandler; $handler2 = new TestHandler;
@@ -153,6 +151,9 @@ class LoggerTest extends \PHPUnit\Framework\TestCase
$this->assertEquals($handler2, $logger->popHandler()); $this->assertEquals($handler2, $logger->popHandler());
$this->assertEquals($handler1, $logger->popHandler()); $this->assertEquals($handler1, $logger->popHandler());
$this->expectException(\LogicException::class);
$logger->popHandler(); $logger->popHandler();
} }
@@ -186,8 +187,6 @@ class LoggerTest extends \PHPUnit\Framework\TestCase
*/ */
public function testPushPopProcessor() public function testPushPopProcessor()
{ {
$this->expectException(\LogicException::class);
$logger = new Logger(__METHOD__); $logger = new Logger(__METHOD__);
$processor1 = new WebProcessor; $processor1 = new WebProcessor;
$processor2 = new WebProcessor; $processor2 = new WebProcessor;
@@ -197,6 +196,9 @@ class LoggerTest extends \PHPUnit\Framework\TestCase
$this->assertEquals($processor2, $logger->popProcessor()); $this->assertEquals($processor2, $logger->popProcessor());
$this->assertEquals($processor1, $logger->popProcessor()); $this->assertEquals($processor1, $logger->popProcessor());
$this->expectException(\LogicException::class);
$logger->popProcessor(); $logger->popProcessor();
} }
@@ -598,8 +600,6 @@ class LoggerTest extends \PHPUnit\Framework\TestCase
*/ */
public function testDefaultHandleException() public function testDefaultHandleException()
{ {
$this->expectException(\Exception::class);
$logger = new Logger(__METHOD__); $logger = new Logger(__METHOD__);
$handler = $this->getMockBuilder('Monolog\Handler\HandlerInterface')->getMock(); $handler = $this->getMockBuilder('Monolog\Handler\HandlerInterface')->getMock();
$handler->expects($this->any()) $handler->expects($this->any())
@@ -610,6 +610,9 @@ class LoggerTest extends \PHPUnit\Framework\TestCase
->method('handle') ->method('handle')
->will($this->throwException(new \Exception('Some handler exception'))) ->will($this->throwException(new \Exception('Some handler exception')))
; ;
$this->expectException(\Exception::class);
$logger->pushHandler($handler); $logger->pushHandler($handler);
$logger->info('test'); $logger->info('test');
} }

View File

@@ -142,13 +142,13 @@ class RegistryTest extends \PHPUnit\Framework\TestCase
*/ */
public function testFailsOnUnspecifiedReplacement() public function testFailsOnUnspecifiedReplacement()
{ {
$this->expectException(\InvalidArgumentException::class);
$log1 = new Logger('test1'); $log1 = new Logger('test1');
$log2 = new Logger('test2'); $log2 = new Logger('test2');
Registry::addLogger($log1, 'log'); Registry::addLogger($log1, 'log');
$this->expectException(\InvalidArgumentException::class);
Registry::addLogger($log2, 'log'); Registry::addLogger($log2, 'log');
} }
} }