/vendor/monolog/monolog/tests/Monolog/Formatter/ChromePHPFormatterTest.php |
@@ -0,0 +1,158 @@ |
<?php |
|
/* |
* This file is part of the Monolog package. |
* |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
*/ |
|
namespace Monolog\Formatter; |
|
use Monolog\Logger; |
|
class ChromePHPFormatterTest extends \PHPUnit_Framework_TestCase |
{ |
/** |
* @covers Monolog\Formatter\ChromePHPFormatter::format |
*/ |
public function testDefaultFormat() |
{ |
$formatter = new ChromePHPFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('ip' => '127.0.0.1'), |
'message' => 'log', |
); |
|
$message = $formatter->format($record); |
|
$this->assertEquals( |
array( |
'meh', |
array( |
'message' => 'log', |
'context' => array('from' => 'logger'), |
'extra' => array('ip' => '127.0.0.1'), |
), |
'unknown', |
'error', |
), |
$message |
); |
} |
|
/** |
* @covers Monolog\Formatter\ChromePHPFormatter::format |
*/ |
public function testFormatWithFileAndLine() |
{ |
$formatter = new ChromePHPFormatter(); |
$record = array( |
'level' => Logger::CRITICAL, |
'level_name' => 'CRITICAL', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('ip' => '127.0.0.1', 'file' => 'test', 'line' => 14), |
'message' => 'log', |
); |
|
$message = $formatter->format($record); |
|
$this->assertEquals( |
array( |
'meh', |
array( |
'message' => 'log', |
'context' => array('from' => 'logger'), |
'extra' => array('ip' => '127.0.0.1'), |
), |
'test : 14', |
'error', |
), |
$message |
); |
} |
|
/** |
* @covers Monolog\Formatter\ChromePHPFormatter::format |
*/ |
public function testFormatWithoutContext() |
{ |
$formatter = new ChromePHPFormatter(); |
$record = array( |
'level' => Logger::DEBUG, |
'level_name' => 'DEBUG', |
'channel' => 'meh', |
'context' => array(), |
'datetime' => new \DateTime("@0"), |
'extra' => array(), |
'message' => 'log', |
); |
|
$message = $formatter->format($record); |
|
$this->assertEquals( |
array( |
'meh', |
'log', |
'unknown', |
'log', |
), |
$message |
); |
} |
|
/** |
* @covers Monolog\Formatter\ChromePHPFormatter::formatBatch |
*/ |
public function testBatchFormatThrowException() |
{ |
$formatter = new ChromePHPFormatter(); |
$records = array( |
array( |
'level' => Logger::INFO, |
'level_name' => 'INFO', |
'channel' => 'meh', |
'context' => array(), |
'datetime' => new \DateTime("@0"), |
'extra' => array(), |
'message' => 'log', |
), |
array( |
'level' => Logger::WARNING, |
'level_name' => 'WARNING', |
'channel' => 'foo', |
'context' => array(), |
'datetime' => new \DateTime("@0"), |
'extra' => array(), |
'message' => 'log2', |
), |
); |
|
$this->assertEquals( |
array( |
array( |
'meh', |
'log', |
'unknown', |
'info', |
), |
array( |
'foo', |
'log2', |
'unknown', |
'warn', |
), |
), |
$formatter->formatBatch($records) |
); |
} |
} |
/vendor/monolog/monolog/tests/Monolog/Formatter/GelfMessageFormatterTest.php |
@@ -0,0 +1,258 @@ |
<?php |
|
/* |
* This file is part of the Monolog package. |
* |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
*/ |
|
namespace Monolog\Formatter; |
|
use Monolog\Logger; |
|
class GelfMessageFormatterTest extends \PHPUnit_Framework_TestCase |
{ |
public function setUp() |
{ |
if (!class_exists('\Gelf\Message')) { |
$this->markTestSkipped("graylog2/gelf-php or mlehner/gelf-php is not installed"); |
} |
} |
|
/** |
* @covers Monolog\Formatter\GelfMessageFormatter::format |
*/ |
public function testDefaultFormatter() |
{ |
$formatter = new GelfMessageFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array(), |
'datetime' => new \DateTime("@0"), |
'extra' => array(), |
'message' => 'log', |
); |
|
$message = $formatter->format($record); |
|
$this->assertInstanceOf('Gelf\Message', $message); |
$this->assertEquals(0, $message->getTimestamp()); |
$this->assertEquals('log', $message->getShortMessage()); |
$this->assertEquals('meh', $message->getFacility()); |
$this->assertEquals(null, $message->getLine()); |
$this->assertEquals(null, $message->getFile()); |
$this->assertEquals($this->isLegacy() ? 3 : 'error', $message->getLevel()); |
$this->assertNotEmpty($message->getHost()); |
|
$formatter = new GelfMessageFormatter('mysystem'); |
|
$message = $formatter->format($record); |
|
$this->assertInstanceOf('Gelf\Message', $message); |
$this->assertEquals('mysystem', $message->getHost()); |
} |
|
/** |
* @covers Monolog\Formatter\GelfMessageFormatter::format |
*/ |
public function testFormatWithFileAndLine() |
{ |
$formatter = new GelfMessageFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('file' => 'test', 'line' => 14), |
'message' => 'log', |
); |
|
$message = $formatter->format($record); |
|
$this->assertInstanceOf('Gelf\Message', $message); |
$this->assertEquals('test', $message->getFile()); |
$this->assertEquals(14, $message->getLine()); |
} |
|
/** |
* @covers Monolog\Formatter\GelfMessageFormatter::format |
* @expectedException InvalidArgumentException |
*/ |
public function testFormatInvalidFails() |
{ |
$formatter = new GelfMessageFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
); |
|
$formatter->format($record); |
} |
|
/** |
* @covers Monolog\Formatter\GelfMessageFormatter::format |
*/ |
public function testFormatWithContext() |
{ |
$formatter = new GelfMessageFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('key' => 'pair'), |
'message' => 'log', |
); |
|
$message = $formatter->format($record); |
|
$this->assertInstanceOf('Gelf\Message', $message); |
|
$message_array = $message->toArray(); |
|
$this->assertArrayHasKey('_ctxt_from', $message_array); |
$this->assertEquals('logger', $message_array['_ctxt_from']); |
|
// Test with extraPrefix |
$formatter = new GelfMessageFormatter(null, null, 'CTX'); |
$message = $formatter->format($record); |
|
$this->assertInstanceOf('Gelf\Message', $message); |
|
$message_array = $message->toArray(); |
|
$this->assertArrayHasKey('_CTXfrom', $message_array); |
$this->assertEquals('logger', $message_array['_CTXfrom']); |
} |
|
/** |
* @covers Monolog\Formatter\GelfMessageFormatter::format |
*/ |
public function testFormatWithContextContainingException() |
{ |
$formatter = new GelfMessageFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger', 'exception' => array( |
'class' => '\Exception', |
'file' => '/some/file/in/dir.php:56', |
'trace' => array('/some/file/1.php:23', '/some/file/2.php:3'), |
)), |
'datetime' => new \DateTime("@0"), |
'extra' => array(), |
'message' => 'log', |
); |
|
$message = $formatter->format($record); |
|
$this->assertInstanceOf('Gelf\Message', $message); |
|
$this->assertEquals("/some/file/in/dir.php", $message->getFile()); |
$this->assertEquals("56", $message->getLine()); |
} |
|
/** |
* @covers Monolog\Formatter\GelfMessageFormatter::format |
*/ |
public function testFormatWithExtra() |
{ |
$formatter = new GelfMessageFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('key' => 'pair'), |
'message' => 'log', |
); |
|
$message = $formatter->format($record); |
|
$this->assertInstanceOf('Gelf\Message', $message); |
|
$message_array = $message->toArray(); |
|
$this->assertArrayHasKey('_key', $message_array); |
$this->assertEquals('pair', $message_array['_key']); |
|
// Test with extraPrefix |
$formatter = new GelfMessageFormatter(null, 'EXT'); |
$message = $formatter->format($record); |
|
$this->assertInstanceOf('Gelf\Message', $message); |
|
$message_array = $message->toArray(); |
|
$this->assertArrayHasKey('_EXTkey', $message_array); |
$this->assertEquals('pair', $message_array['_EXTkey']); |
} |
|
public function testFormatWithLargeData() |
{ |
$formatter = new GelfMessageFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('exception' => str_repeat(' ', 32767)), |
'datetime' => new \DateTime("@0"), |
'extra' => array('key' => str_repeat(' ', 32767)), |
'message' => 'log' |
); |
$message = $formatter->format($record); |
$messageArray = $message->toArray(); |
|
// 200 for padding + metadata |
$length = 200; |
|
foreach ($messageArray as $key => $value) { |
if (!in_array($key, array('level', 'timestamp'))) { |
$length += strlen($value); |
} |
} |
|
$this->assertLessThanOrEqual(65792, $length, 'The message length is no longer than the maximum allowed length'); |
} |
|
public function testFormatWithUnlimitedLength() |
{ |
$formatter = new GelfMessageFormatter('LONG_SYSTEM_NAME', null, 'ctxt_', PHP_INT_MAX); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('exception' => str_repeat(' ', 32767 * 2)), |
'datetime' => new \DateTime("@0"), |
'extra' => array('key' => str_repeat(' ', 32767 * 2)), |
'message' => 'log' |
); |
$message = $formatter->format($record); |
$messageArray = $message->toArray(); |
|
// 200 for padding + metadata |
$length = 200; |
|
foreach ($messageArray as $key => $value) { |
if (!in_array($key, array('level', 'timestamp'))) { |
$length += strlen($value); |
} |
} |
|
$this->assertGreaterThanOrEqual(131289, $length, 'The message should not be truncated'); |
} |
|
private function isLegacy() |
{ |
return interface_exists('\Gelf\IMessagePublisher'); |
} |
} |
/vendor/monolog/monolog/tests/Monolog/Formatter/JsonFormatterTest.php |
@@ -0,0 +1,183 @@ |
<?php |
|
/* |
* This file is part of the Monolog package. |
* |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
*/ |
|
namespace Monolog\Formatter; |
|
use Monolog\Logger; |
use Monolog\TestCase; |
|
class JsonFormatterTest extends TestCase |
{ |
/** |
* @covers Monolog\Formatter\JsonFormatter::__construct |
* @covers Monolog\Formatter\JsonFormatter::getBatchMode |
* @covers Monolog\Formatter\JsonFormatter::isAppendingNewlines |
*/ |
public function testConstruct() |
{ |
$formatter = new JsonFormatter(); |
$this->assertEquals(JsonFormatter::BATCH_MODE_JSON, $formatter->getBatchMode()); |
$this->assertEquals(true, $formatter->isAppendingNewlines()); |
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES, false); |
$this->assertEquals(JsonFormatter::BATCH_MODE_NEWLINES, $formatter->getBatchMode()); |
$this->assertEquals(false, $formatter->isAppendingNewlines()); |
} |
|
/** |
* @covers Monolog\Formatter\JsonFormatter::format |
*/ |
public function testFormat() |
{ |
$formatter = new JsonFormatter(); |
$record = $this->getRecord(); |
$this->assertEquals(json_encode($record)."\n", $formatter->format($record)); |
|
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false); |
$record = $this->getRecord(); |
$this->assertEquals(json_encode($record), $formatter->format($record)); |
} |
|
/** |
* @covers Monolog\Formatter\JsonFormatter::formatBatch |
* @covers Monolog\Formatter\JsonFormatter::formatBatchJson |
*/ |
public function testFormatBatch() |
{ |
$formatter = new JsonFormatter(); |
$records = array( |
$this->getRecord(Logger::WARNING), |
$this->getRecord(Logger::DEBUG), |
); |
$this->assertEquals(json_encode($records), $formatter->formatBatch($records)); |
} |
|
/** |
* @covers Monolog\Formatter\JsonFormatter::formatBatch |
* @covers Monolog\Formatter\JsonFormatter::formatBatchNewlines |
*/ |
public function testFormatBatchNewlines() |
{ |
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES); |
$records = $expected = array( |
$this->getRecord(Logger::WARNING), |
$this->getRecord(Logger::DEBUG), |
); |
array_walk($expected, function (&$value, $key) { |
$value = json_encode($value); |
}); |
$this->assertEquals(implode("\n", $expected), $formatter->formatBatch($records)); |
} |
|
public function testDefFormatWithException() |
{ |
$formatter = new JsonFormatter(); |
$exception = new \RuntimeException('Foo'); |
$formattedException = $this->formatException($exception); |
|
$message = $this->formatRecordWithExceptionInContext($formatter, $exception); |
|
$this->assertContextContainsFormattedException($formattedException, $message); |
} |
|
public function testDefFormatWithPreviousException() |
{ |
$formatter = new JsonFormatter(); |
$exception = new \RuntimeException('Foo', 0, new \LogicException('Wut?')); |
$formattedPrevException = $this->formatException($exception->getPrevious()); |
$formattedException = $this->formatException($exception, $formattedPrevException); |
|
$message = $this->formatRecordWithExceptionInContext($formatter, $exception); |
|
$this->assertContextContainsFormattedException($formattedException, $message); |
} |
|
public function testDefFormatWithThrowable() |
{ |
if (!class_exists('Error') || !is_subclass_of('Error', 'Throwable')) { |
$this->markTestSkipped('Requires PHP >=7'); |
} |
|
$formatter = new JsonFormatter(); |
$throwable = new \Error('Foo'); |
$formattedThrowable = $this->formatException($throwable); |
|
$message = $this->formatRecordWithExceptionInContext($formatter, $throwable); |
|
$this->assertContextContainsFormattedException($formattedThrowable, $message); |
} |
|
/** |
* @param string $expected |
* @param string $actual |
* |
* @internal param string $exception |
*/ |
private function assertContextContainsFormattedException($expected, $actual) |
{ |
$this->assertEquals( |
'{"level_name":"CRITICAL","channel":"core","context":{"exception":'.$expected.'},"datetime":null,"extra":[],"message":"foobar"}'."\n", |
$actual |
); |
} |
|
/** |
* @param JsonFormatter $formatter |
* @param \Exception|\Throwable $exception |
* |
* @return string |
*/ |
private function formatRecordWithExceptionInContext(JsonFormatter $formatter, $exception) |
{ |
$message = $formatter->format(array( |
'level_name' => 'CRITICAL', |
'channel' => 'core', |
'context' => array('exception' => $exception), |
'datetime' => null, |
'extra' => array(), |
'message' => 'foobar', |
)); |
return $message; |
} |
|
/** |
* @param \Exception|\Throwable $exception |
* |
* @return string |
*/ |
private function formatExceptionFilePathWithLine($exception) |
{ |
$options = 0; |
if (version_compare(PHP_VERSION, '5.4.0', '>=')) { |
$options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; |
} |
$path = substr(json_encode($exception->getFile(), $options), 1, -1); |
return $path . ':' . $exception->getLine(); |
} |
|
/** |
* @param \Exception|\Throwable $exception |
* |
* @param null|string $previous |
* |
* @return string |
*/ |
private function formatException($exception, $previous = null) |
{ |
$formattedException = |
'{"class":"' . get_class($exception) . |
'","message":"' . $exception->getMessage() . |
'","code":' . $exception->getCode() . |
',"file":"' . $this->formatExceptionFilePathWithLine($exception) . |
($previous ? '","previous":' . $previous : '"') . |
'}'; |
return $formattedException; |
} |
} |
/vendor/monolog/monolog/tests/Monolog/Formatter/LineFormatterTest.php |
@@ -0,0 +1,222 @@ |
<?php |
|
/* |
* This file is part of the Monolog package. |
* |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
*/ |
|
namespace Monolog\Formatter; |
|
/** |
* @covers Monolog\Formatter\LineFormatter |
*/ |
class LineFormatterTest extends \PHPUnit_Framework_TestCase |
{ |
public function testDefFormatWithString() |
{ |
$formatter = new LineFormatter(null, 'Y-m-d'); |
$message = $formatter->format(array( |
'level_name' => 'WARNING', |
'channel' => 'log', |
'context' => array(), |
'message' => 'foo', |
'datetime' => new \DateTime, |
'extra' => array(), |
)); |
$this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message); |
} |
|
public function testDefFormatWithArrayContext() |
{ |
$formatter = new LineFormatter(null, 'Y-m-d'); |
$message = $formatter->format(array( |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'message' => 'foo', |
'datetime' => new \DateTime, |
'extra' => array(), |
'context' => array( |
'foo' => 'bar', |
'baz' => 'qux', |
'bool' => false, |
'null' => null, |
), |
)); |
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux","bool":false,"null":null} []'."\n", $message); |
} |
|
public function testDefFormatExtras() |
{ |
$formatter = new LineFormatter(null, 'Y-m-d'); |
$message = $formatter->format(array( |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array(), |
'datetime' => new \DateTime, |
'extra' => array('ip' => '127.0.0.1'), |
'message' => 'log', |
)); |
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] {"ip":"127.0.0.1"}'."\n", $message); |
} |
|
public function testFormatExtras() |
{ |
$formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra.file% %extra%\n", 'Y-m-d'); |
$message = $formatter->format(array( |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array(), |
'datetime' => new \DateTime, |
'extra' => array('ip' => '127.0.0.1', 'file' => 'test'), |
'message' => 'log', |
)); |
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] test {"ip":"127.0.0.1"}'."\n", $message); |
} |
|
public function testContextAndExtraOptionallyNotShownIfEmpty() |
{ |
$formatter = new LineFormatter(null, 'Y-m-d', false, true); |
$message = $formatter->format(array( |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array(), |
'datetime' => new \DateTime, |
'extra' => array(), |
'message' => 'log', |
)); |
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log '."\n", $message); |
} |
|
public function testContextAndExtraReplacement() |
{ |
$formatter = new LineFormatter('%context.foo% => %extra.foo%'); |
$message = $formatter->format(array( |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('foo' => 'bar'), |
'datetime' => new \DateTime, |
'extra' => array('foo' => 'xbar'), |
'message' => 'log', |
)); |
$this->assertEquals('bar => xbar', $message); |
} |
|
public function testDefFormatWithObject() |
{ |
$formatter = new LineFormatter(null, 'Y-m-d'); |
$message = $formatter->format(array( |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array(), |
'datetime' => new \DateTime, |
'extra' => array('foo' => new TestFoo, 'bar' => new TestBar, 'baz' => array(), 'res' => fopen('php://memory', 'rb')), |
'message' => 'foobar', |
)); |
|
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":"[object] (Monolog\\\\Formatter\\\\TestFoo: {\\"foo\\":\\"foo\\"})","bar":"[object] (Monolog\\\\Formatter\\\\TestBar: bar)","baz":[],"res":"[resource] (stream)"}'."\n", $message); |
} |
|
public function testDefFormatWithException() |
{ |
$formatter = new LineFormatter(null, 'Y-m-d'); |
$message = $formatter->format(array( |
'level_name' => 'CRITICAL', |
'channel' => 'core', |
'context' => array('exception' => new \RuntimeException('Foo')), |
'datetime' => new \DateTime, |
'extra' => array(), |
'message' => 'foobar', |
)); |
|
$path = str_replace('\\/', '/', json_encode(__FILE__)); |
|
$this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 8).')"} []'."\n", $message); |
} |
|
public function testDefFormatWithPreviousException() |
{ |
$formatter = new LineFormatter(null, 'Y-m-d'); |
$previous = new \LogicException('Wut?'); |
$message = $formatter->format(array( |
'level_name' => 'CRITICAL', |
'channel' => 'core', |
'context' => array('exception' => new \RuntimeException('Foo', 0, $previous)), |
'datetime' => new \DateTime, |
'extra' => array(), |
'message' => 'foobar', |
)); |
|
$path = str_replace('\\/', '/', json_encode(__FILE__)); |
|
$this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 8).', LogicException(code: 0): Wut? at '.substr($path, 1, -1).':'.(__LINE__ - 12).')"} []'."\n", $message); |
} |
|
public function testBatchFormat() |
{ |
$formatter = new LineFormatter(null, 'Y-m-d'); |
$message = $formatter->formatBatch(array( |
array( |
'level_name' => 'CRITICAL', |
'channel' => 'test', |
'message' => 'bar', |
'context' => array(), |
'datetime' => new \DateTime, |
'extra' => array(), |
), |
array( |
'level_name' => 'WARNING', |
'channel' => 'log', |
'message' => 'foo', |
'context' => array(), |
'datetime' => new \DateTime, |
'extra' => array(), |
), |
)); |
$this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message); |
} |
|
public function testFormatShouldStripInlineLineBreaks() |
{ |
$formatter = new LineFormatter(null, 'Y-m-d'); |
$message = $formatter->format( |
array( |
'message' => "foo\nbar", |
'context' => array(), |
'extra' => array(), |
) |
); |
|
$this->assertRegExp('/foo bar/', $message); |
} |
|
public function testFormatShouldNotStripInlineLineBreaksWhenFlagIsSet() |
{ |
$formatter = new LineFormatter(null, 'Y-m-d', true); |
$message = $formatter->format( |
array( |
'message' => "foo\nbar", |
'context' => array(), |
'extra' => array(), |
) |
); |
|
$this->assertRegExp('/foo\nbar/', $message); |
} |
} |
|
class TestFoo |
{ |
public $foo = 'foo'; |
} |
|
class TestBar |
{ |
public function __toString() |
{ |
return 'bar'; |
} |
} |
/vendor/monolog/monolog/tests/Monolog/Formatter/LogstashFormatterTest.php |
@@ -0,0 +1,333 @@ |
<?php |
|
/* |
* This file is part of the Monolog package. |
* |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
*/ |
|
namespace Monolog\Formatter; |
|
use Monolog\Logger; |
|
class LogstashFormatterTest extends \PHPUnit_Framework_TestCase |
{ |
public function tearDown() |
{ |
\PHPUnit_Framework_Error_Warning::$enabled = true; |
|
return parent::tearDown(); |
} |
|
/** |
* @covers Monolog\Formatter\LogstashFormatter::format |
*/ |
public function testDefaultFormatter() |
{ |
$formatter = new LogstashFormatter('test', 'hostname'); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array(), |
'datetime' => new \DateTime("@0"), |
'extra' => array(), |
'message' => 'log', |
); |
|
$message = json_decode($formatter->format($record), true); |
|
$this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']); |
$this->assertEquals('log', $message['@message']); |
$this->assertEquals('meh', $message['@fields']['channel']); |
$this->assertContains('meh', $message['@tags']); |
$this->assertEquals(Logger::ERROR, $message['@fields']['level']); |
$this->assertEquals('test', $message['@type']); |
$this->assertEquals('hostname', $message['@source']); |
|
$formatter = new LogstashFormatter('mysystem'); |
|
$message = json_decode($formatter->format($record), true); |
|
$this->assertEquals('mysystem', $message['@type']); |
} |
|
/** |
* @covers Monolog\Formatter\LogstashFormatter::format |
*/ |
public function testFormatWithFileAndLine() |
{ |
$formatter = new LogstashFormatter('test'); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('file' => 'test', 'line' => 14), |
'message' => 'log', |
); |
|
$message = json_decode($formatter->format($record), true); |
|
$this->assertEquals('test', $message['@fields']['file']); |
$this->assertEquals(14, $message['@fields']['line']); |
} |
|
/** |
* @covers Monolog\Formatter\LogstashFormatter::format |
*/ |
public function testFormatWithContext() |
{ |
$formatter = new LogstashFormatter('test'); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('key' => 'pair'), |
'message' => 'log', |
); |
|
$message = json_decode($formatter->format($record), true); |
|
$message_array = $message['@fields']; |
|
$this->assertArrayHasKey('ctxt_from', $message_array); |
$this->assertEquals('logger', $message_array['ctxt_from']); |
|
// Test with extraPrefix |
$formatter = new LogstashFormatter('test', null, null, 'CTX'); |
$message = json_decode($formatter->format($record), true); |
|
$message_array = $message['@fields']; |
|
$this->assertArrayHasKey('CTXfrom', $message_array); |
$this->assertEquals('logger', $message_array['CTXfrom']); |
} |
|
/** |
* @covers Monolog\Formatter\LogstashFormatter::format |
*/ |
public function testFormatWithExtra() |
{ |
$formatter = new LogstashFormatter('test'); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('key' => 'pair'), |
'message' => 'log', |
); |
|
$message = json_decode($formatter->format($record), true); |
|
$message_array = $message['@fields']; |
|
$this->assertArrayHasKey('key', $message_array); |
$this->assertEquals('pair', $message_array['key']); |
|
// Test with extraPrefix |
$formatter = new LogstashFormatter('test', null, 'EXT'); |
$message = json_decode($formatter->format($record), true); |
|
$message_array = $message['@fields']; |
|
$this->assertArrayHasKey('EXTkey', $message_array); |
$this->assertEquals('pair', $message_array['EXTkey']); |
} |
|
public function testFormatWithApplicationName() |
{ |
$formatter = new LogstashFormatter('app', 'test'); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('key' => 'pair'), |
'message' => 'log', |
); |
|
$message = json_decode($formatter->format($record), true); |
|
$this->assertArrayHasKey('@type', $message); |
$this->assertEquals('app', $message['@type']); |
} |
|
/** |
* @covers Monolog\Formatter\LogstashFormatter::format |
*/ |
public function testDefaultFormatterV1() |
{ |
$formatter = new LogstashFormatter('test', 'hostname', null, 'ctxt_', LogstashFormatter::V1); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array(), |
'datetime' => new \DateTime("@0"), |
'extra' => array(), |
'message' => 'log', |
); |
|
$message = json_decode($formatter->format($record), true); |
|
$this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']); |
$this->assertEquals("1", $message['@version']); |
$this->assertEquals('log', $message['message']); |
$this->assertEquals('meh', $message['channel']); |
$this->assertEquals('ERROR', $message['level']); |
$this->assertEquals('test', $message['type']); |
$this->assertEquals('hostname', $message['host']); |
|
$formatter = new LogstashFormatter('mysystem', null, null, 'ctxt_', LogstashFormatter::V1); |
|
$message = json_decode($formatter->format($record), true); |
|
$this->assertEquals('mysystem', $message['type']); |
} |
|
/** |
* @covers Monolog\Formatter\LogstashFormatter::format |
*/ |
public function testFormatWithFileAndLineV1() |
{ |
$formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('file' => 'test', 'line' => 14), |
'message' => 'log', |
); |
|
$message = json_decode($formatter->format($record), true); |
|
$this->assertEquals('test', $message['file']); |
$this->assertEquals(14, $message['line']); |
} |
|
/** |
* @covers Monolog\Formatter\LogstashFormatter::format |
*/ |
public function testFormatWithContextV1() |
{ |
$formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('key' => 'pair'), |
'message' => 'log', |
); |
|
$message = json_decode($formatter->format($record), true); |
|
$this->assertArrayHasKey('ctxt_from', $message); |
$this->assertEquals('logger', $message['ctxt_from']); |
|
// Test with extraPrefix |
$formatter = new LogstashFormatter('test', null, null, 'CTX', LogstashFormatter::V1); |
$message = json_decode($formatter->format($record), true); |
|
$this->assertArrayHasKey('CTXfrom', $message); |
$this->assertEquals('logger', $message['CTXfrom']); |
} |
|
/** |
* @covers Monolog\Formatter\LogstashFormatter::format |
*/ |
public function testFormatWithExtraV1() |
{ |
$formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('key' => 'pair'), |
'message' => 'log', |
); |
|
$message = json_decode($formatter->format($record), true); |
|
$this->assertArrayHasKey('key', $message); |
$this->assertEquals('pair', $message['key']); |
|
// Test with extraPrefix |
$formatter = new LogstashFormatter('test', null, 'EXT', 'ctxt_', LogstashFormatter::V1); |
$message = json_decode($formatter->format($record), true); |
|
$this->assertArrayHasKey('EXTkey', $message); |
$this->assertEquals('pair', $message['EXTkey']); |
} |
|
public function testFormatWithApplicationNameV1() |
{ |
$formatter = new LogstashFormatter('app', 'test', null, 'ctxt_', LogstashFormatter::V1); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('key' => 'pair'), |
'message' => 'log', |
); |
|
$message = json_decode($formatter->format($record), true); |
|
$this->assertArrayHasKey('type', $message); |
$this->assertEquals('app', $message['type']); |
} |
|
public function testFormatWithLatin9Data() |
{ |
if (version_compare(PHP_VERSION, '5.5.0', '<')) { |
// Ignore the warning that will be emitted by PHP <5.5.0 |
\PHPUnit_Framework_Error_Warning::$enabled = false; |
} |
$formatter = new LogstashFormatter('test', 'hostname'); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => '¯\_(ツ)_/¯', |
'context' => array(), |
'datetime' => new \DateTime("@0"), |
'extra' => array( |
'user_agent' => "\xD6WN; FBCR/OrangeEspa\xF1a; Vers\xE3o/4.0; F\xE4rist", |
), |
'message' => 'log', |
); |
|
$message = json_decode($formatter->format($record), true); |
|
$this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']); |
$this->assertEquals('log', $message['@message']); |
$this->assertEquals('¯\_(ツ)_/¯', $message['@fields']['channel']); |
$this->assertContains('¯\_(ツ)_/¯', $message['@tags']); |
$this->assertEquals(Logger::ERROR, $message['@fields']['level']); |
$this->assertEquals('test', $message['@type']); |
$this->assertEquals('hostname', $message['@source']); |
if (version_compare(PHP_VERSION, '5.5.0', '>=')) { |
$this->assertEquals('ÖWN; FBCR/OrangeEspaña; Versão/4.0; Färist', $message['@fields']['user_agent']); |
} else { |
// PHP <5.5 does not return false for an element encoding failure, |
// instead it emits a warning (possibly) and nulls the value. |
$this->assertEquals(null, $message['@fields']['user_agent']); |
} |
} |
} |
/vendor/monolog/monolog/tests/Monolog/Formatter/MongoDBFormatterTest.php |
@@ -0,0 +1,262 @@ |
<?php |
|
/* |
* This file is part of the Monolog package. |
* |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
*/ |
|
namespace Monolog\Formatter; |
|
use Monolog\Logger; |
|
/** |
* @author Florian Plattner <me@florianplattner.de> |
*/ |
class MongoDBFormatterTest extends \PHPUnit_Framework_TestCase |
{ |
public function setUp() |
{ |
if (!class_exists('MongoDate')) { |
$this->markTestSkipped('mongo extension not installed'); |
} |
} |
|
public function constructArgumentProvider() |
{ |
return array( |
array(1, true, 1, true), |
array(0, false, 0, false), |
); |
} |
|
/** |
* @param $traceDepth |
* @param $traceAsString |
* @param $expectedTraceDepth |
* @param $expectedTraceAsString |
* |
* @dataProvider constructArgumentProvider |
*/ |
public function testConstruct($traceDepth, $traceAsString, $expectedTraceDepth, $expectedTraceAsString) |
{ |
$formatter = new MongoDBFormatter($traceDepth, $traceAsString); |
|
$reflTrace = new \ReflectionProperty($formatter, 'exceptionTraceAsString'); |
$reflTrace->setAccessible(true); |
$this->assertEquals($expectedTraceAsString, $reflTrace->getValue($formatter)); |
|
$reflDepth = new\ReflectionProperty($formatter, 'maxNestingLevel'); |
$reflDepth->setAccessible(true); |
$this->assertEquals($expectedTraceDepth, $reflDepth->getValue($formatter)); |
} |
|
public function testSimpleFormat() |
{ |
$record = array( |
'message' => 'some log message', |
'context' => array(), |
'level' => Logger::WARNING, |
'level_name' => Logger::getLevelName(Logger::WARNING), |
'channel' => 'test', |
'datetime' => new \DateTime('2014-02-01 00:00:00'), |
'extra' => array(), |
); |
|
$formatter = new MongoDBFormatter(); |
$formattedRecord = $formatter->format($record); |
|
$this->assertCount(7, $formattedRecord); |
$this->assertEquals('some log message', $formattedRecord['message']); |
$this->assertEquals(array(), $formattedRecord['context']); |
$this->assertEquals(Logger::WARNING, $formattedRecord['level']); |
$this->assertEquals(Logger::getLevelName(Logger::WARNING), $formattedRecord['level_name']); |
$this->assertEquals('test', $formattedRecord['channel']); |
$this->assertInstanceOf('\MongoDate', $formattedRecord['datetime']); |
$this->assertEquals('0.00000000 1391212800', $formattedRecord['datetime']->__toString()); |
$this->assertEquals(array(), $formattedRecord['extra']); |
} |
|
public function testRecursiveFormat() |
{ |
$someObject = new \stdClass(); |
$someObject->foo = 'something'; |
$someObject->bar = 'stuff'; |
|
$record = array( |
'message' => 'some log message', |
'context' => array( |
'stuff' => new \DateTime('2014-02-01 02:31:33'), |
'some_object' => $someObject, |
'context_string' => 'some string', |
'context_int' => 123456, |
'except' => new \Exception('exception message', 987), |
), |
'level' => Logger::WARNING, |
'level_name' => Logger::getLevelName(Logger::WARNING), |
'channel' => 'test', |
'datetime' => new \DateTime('2014-02-01 00:00:00'), |
'extra' => array(), |
); |
|
$formatter = new MongoDBFormatter(); |
$formattedRecord = $formatter->format($record); |
|
$this->assertCount(5, $formattedRecord['context']); |
$this->assertInstanceOf('\MongoDate', $formattedRecord['context']['stuff']); |
$this->assertEquals('0.00000000 1391221893', $formattedRecord['context']['stuff']->__toString()); |
$this->assertEquals( |
array( |
'foo' => 'something', |
'bar' => 'stuff', |
'class' => 'stdClass', |
), |
$formattedRecord['context']['some_object'] |
); |
$this->assertEquals('some string', $formattedRecord['context']['context_string']); |
$this->assertEquals(123456, $formattedRecord['context']['context_int']); |
|
$this->assertCount(5, $formattedRecord['context']['except']); |
$this->assertEquals('exception message', $formattedRecord['context']['except']['message']); |
$this->assertEquals(987, $formattedRecord['context']['except']['code']); |
$this->assertInternalType('string', $formattedRecord['context']['except']['file']); |
$this->assertInternalType('integer', $formattedRecord['context']['except']['code']); |
$this->assertInternalType('string', $formattedRecord['context']['except']['trace']); |
$this->assertEquals('Exception', $formattedRecord['context']['except']['class']); |
} |
|
public function testFormatDepthArray() |
{ |
$record = array( |
'message' => 'some log message', |
'context' => array( |
'nest2' => array( |
'property' => 'anything', |
'nest3' => array( |
'nest4' => 'value', |
'property' => 'nothing', |
), |
), |
), |
'level' => Logger::WARNING, |
'level_name' => Logger::getLevelName(Logger::WARNING), |
'channel' => 'test', |
'datetime' => new \DateTime('2014-02-01 00:00:00'), |
'extra' => array(), |
); |
|
$formatter = new MongoDBFormatter(2); |
$formattedResult = $formatter->format($record); |
|
$this->assertEquals( |
array( |
'nest2' => array( |
'property' => 'anything', |
'nest3' => '[...]', |
), |
), |
$formattedResult['context'] |
); |
} |
|
public function testFormatDepthArrayInfiniteNesting() |
{ |
$record = array( |
'message' => 'some log message', |
'context' => array( |
'nest2' => array( |
'property' => 'something', |
'nest3' => array( |
'property' => 'anything', |
'nest4' => array( |
'property' => 'nothing', |
), |
), |
), |
), |
'level' => Logger::WARNING, |
'level_name' => Logger::getLevelName(Logger::WARNING), |
'channel' => 'test', |
'datetime' => new \DateTime('2014-02-01 00:00:00'), |
'extra' => array(), |
); |
|
$formatter = new MongoDBFormatter(0); |
$formattedResult = $formatter->format($record); |
|
$this->assertEquals( |
array( |
'nest2' => array( |
'property' => 'something', |
'nest3' => array( |
'property' => 'anything', |
'nest4' => array( |
'property' => 'nothing', |
), |
), |
), |
), |
$formattedResult['context'] |
); |
} |
|
public function testFormatDepthObjects() |
{ |
$someObject = new \stdClass(); |
$someObject->property = 'anything'; |
$someObject->nest3 = new \stdClass(); |
$someObject->nest3->property = 'nothing'; |
$someObject->nest3->nest4 = 'invisible'; |
|
$record = array( |
'message' => 'some log message', |
'context' => array( |
'nest2' => $someObject, |
), |
'level' => Logger::WARNING, |
'level_name' => Logger::getLevelName(Logger::WARNING), |
'channel' => 'test', |
'datetime' => new \DateTime('2014-02-01 00:00:00'), |
'extra' => array(), |
); |
|
$formatter = new MongoDBFormatter(2, true); |
$formattedResult = $formatter->format($record); |
|
$this->assertEquals( |
array( |
'nest2' => array( |
'property' => 'anything', |
'nest3' => '[...]', |
'class' => 'stdClass', |
), |
), |
$formattedResult['context'] |
); |
} |
|
public function testFormatDepthException() |
{ |
$record = array( |
'message' => 'some log message', |
'context' => array( |
'nest2' => new \Exception('exception message', 987), |
), |
'level' => Logger::WARNING, |
'level_name' => Logger::getLevelName(Logger::WARNING), |
'channel' => 'test', |
'datetime' => new \DateTime('2014-02-01 00:00:00'), |
'extra' => array(), |
); |
|
$formatter = new MongoDBFormatter(2, false); |
$formattedRecord = $formatter->format($record); |
|
$this->assertEquals('exception message', $formattedRecord['context']['nest2']['message']); |
$this->assertEquals(987, $formattedRecord['context']['nest2']['code']); |
$this->assertEquals('[...]', $formattedRecord['context']['nest2']['trace']); |
} |
} |
/vendor/monolog/monolog/tests/Monolog/Formatter/NormalizerFormatterTest.php |
@@ -0,0 +1,423 @@ |
<?php |
|
/* |
* This file is part of the Monolog package. |
* |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
*/ |
|
namespace Monolog\Formatter; |
|
/** |
* @covers Monolog\Formatter\NormalizerFormatter |
*/ |
class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase |
{ |
public function tearDown() |
{ |
\PHPUnit_Framework_Error_Warning::$enabled = true; |
|
return parent::tearDown(); |
} |
|
public function testFormat() |
{ |
$formatter = new NormalizerFormatter('Y-m-d'); |
$formatted = $formatter->format(array( |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'message' => 'foo', |
'datetime' => new \DateTime, |
'extra' => array('foo' => new TestFooNorm, 'bar' => new TestBarNorm, 'baz' => array(), 'res' => fopen('php://memory', 'rb')), |
'context' => array( |
'foo' => 'bar', |
'baz' => 'qux', |
'inf' => INF, |
'-inf' => -INF, |
'nan' => acos(4), |
), |
)); |
|
$this->assertEquals(array( |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'message' => 'foo', |
'datetime' => date('Y-m-d'), |
'extra' => array( |
'foo' => '[object] (Monolog\\Formatter\\TestFooNorm: {"foo":"foo"})', |
'bar' => '[object] (Monolog\\Formatter\\TestBarNorm: bar)', |
'baz' => array(), |
'res' => '[resource] (stream)', |
), |
'context' => array( |
'foo' => 'bar', |
'baz' => 'qux', |
'inf' => 'INF', |
'-inf' => '-INF', |
'nan' => 'NaN', |
), |
), $formatted); |
} |
|
public function testFormatExceptions() |
{ |
$formatter = new NormalizerFormatter('Y-m-d'); |
$e = new \LogicException('bar'); |
$e2 = new \RuntimeException('foo', 0, $e); |
$formatted = $formatter->format(array( |
'exception' => $e2, |
)); |
|
$this->assertGreaterThan(5, count($formatted['exception']['trace'])); |
$this->assertTrue(isset($formatted['exception']['previous'])); |
unset($formatted['exception']['trace'], $formatted['exception']['previous']); |
|
$this->assertEquals(array( |
'exception' => array( |
'class' => get_class($e2), |
'message' => $e2->getMessage(), |
'code' => $e2->getCode(), |
'file' => $e2->getFile().':'.$e2->getLine(), |
), |
), $formatted); |
} |
|
public function testFormatSoapFaultException() |
{ |
if (!class_exists('SoapFault')) { |
$this->markTestSkipped('Requires the soap extension'); |
} |
|
$formatter = new NormalizerFormatter('Y-m-d'); |
$e = new \SoapFault('foo', 'bar', 'hello', 'world'); |
$formatted = $formatter->format(array( |
'exception' => $e, |
)); |
|
unset($formatted['exception']['trace']); |
|
$this->assertEquals(array( |
'exception' => array( |
'class' => 'SoapFault', |
'message' => 'bar', |
'code' => 0, |
'file' => $e->getFile().':'.$e->getLine(), |
'faultcode' => 'foo', |
'faultactor' => 'hello', |
'detail' => 'world', |
), |
), $formatted); |
} |
|
public function testFormatToStringExceptionHandle() |
{ |
$formatter = new NormalizerFormatter('Y-m-d'); |
$this->setExpectedException('RuntimeException', 'Could not convert to string'); |
$formatter->format(array( |
'myObject' => new TestToStringError(), |
)); |
} |
|
public function testBatchFormat() |
{ |
$formatter = new NormalizerFormatter('Y-m-d'); |
$formatted = $formatter->formatBatch(array( |
array( |
'level_name' => 'CRITICAL', |
'channel' => 'test', |
'message' => 'bar', |
'context' => array(), |
'datetime' => new \DateTime, |
'extra' => array(), |
), |
array( |
'level_name' => 'WARNING', |
'channel' => 'log', |
'message' => 'foo', |
'context' => array(), |
'datetime' => new \DateTime, |
'extra' => array(), |
), |
)); |
$this->assertEquals(array( |
array( |
'level_name' => 'CRITICAL', |
'channel' => 'test', |
'message' => 'bar', |
'context' => array(), |
'datetime' => date('Y-m-d'), |
'extra' => array(), |
), |
array( |
'level_name' => 'WARNING', |
'channel' => 'log', |
'message' => 'foo', |
'context' => array(), |
'datetime' => date('Y-m-d'), |
'extra' => array(), |
), |
), $formatted); |
} |
|
/** |
* Test issue #137 |
*/ |
public function testIgnoresRecursiveObjectReferences() |
{ |
// set up the recursion |
$foo = new \stdClass(); |
$bar = new \stdClass(); |
|
$foo->bar = $bar; |
$bar->foo = $foo; |
|
// set an error handler to assert that the error is not raised anymore |
$that = $this; |
set_error_handler(function ($level, $message, $file, $line, $context) use ($that) { |
if (error_reporting() & $level) { |
restore_error_handler(); |
$that->fail("$message should not be raised"); |
} |
}); |
|
$formatter = new NormalizerFormatter(); |
$reflMethod = new \ReflectionMethod($formatter, 'toJson'); |
$reflMethod->setAccessible(true); |
$res = $reflMethod->invoke($formatter, array($foo, $bar), true); |
|
restore_error_handler(); |
|
$this->assertEquals(@json_encode(array($foo, $bar)), $res); |
} |
|
public function testIgnoresInvalidTypes() |
{ |
// set up the recursion |
$resource = fopen(__FILE__, 'r'); |
|
// set an error handler to assert that the error is not raised anymore |
$that = $this; |
set_error_handler(function ($level, $message, $file, $line, $context) use ($that) { |
if (error_reporting() & $level) { |
restore_error_handler(); |
$that->fail("$message should not be raised"); |
} |
}); |
|
$formatter = new NormalizerFormatter(); |
$reflMethod = new \ReflectionMethod($formatter, 'toJson'); |
$reflMethod->setAccessible(true); |
$res = $reflMethod->invoke($formatter, array($resource), true); |
|
restore_error_handler(); |
|
$this->assertEquals(@json_encode(array($resource)), $res); |
} |
|
public function testNormalizeHandleLargeArrays() |
{ |
$formatter = new NormalizerFormatter(); |
$largeArray = range(1, 2000); |
|
$res = $formatter->format(array( |
'level_name' => 'CRITICAL', |
'channel' => 'test', |
'message' => 'bar', |
'context' => array($largeArray), |
'datetime' => new \DateTime, |
'extra' => array(), |
)); |
|
$this->assertCount(1000, $res['context'][0]); |
$this->assertEquals('Over 1000 items (2000 total), aborting normalization', $res['context'][0]['...']); |
} |
|
/** |
* @expectedException RuntimeException |
*/ |
public function testThrowsOnInvalidEncoding() |
{ |
if (version_compare(PHP_VERSION, '5.5.0', '<')) { |
// Ignore the warning that will be emitted by PHP <5.5.0 |
\PHPUnit_Framework_Error_Warning::$enabled = false; |
} |
$formatter = new NormalizerFormatter(); |
$reflMethod = new \ReflectionMethod($formatter, 'toJson'); |
$reflMethod->setAccessible(true); |
|
// send an invalid unicode sequence as a object that can't be cleaned |
$record = new \stdClass; |
$record->message = "\xB1\x31"; |
$res = $reflMethod->invoke($formatter, $record); |
if (PHP_VERSION_ID < 50500 && $res === '{"message":null}') { |
throw new \RuntimeException('PHP 5.3/5.4 throw a warning and null the value instead of returning false entirely'); |
} |
} |
|
public function testConvertsInvalidEncodingAsLatin9() |
{ |
if (version_compare(PHP_VERSION, '5.5.0', '<')) { |
// Ignore the warning that will be emitted by PHP <5.5.0 |
\PHPUnit_Framework_Error_Warning::$enabled = false; |
} |
$formatter = new NormalizerFormatter(); |
$reflMethod = new \ReflectionMethod($formatter, 'toJson'); |
$reflMethod->setAccessible(true); |
|
$res = $reflMethod->invoke($formatter, array('message' => "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")); |
|
if (version_compare(PHP_VERSION, '5.5.0', '>=')) { |
$this->assertSame('{"message":"€ŠšŽžŒœŸ"}', $res); |
} else { |
// PHP <5.5 does not return false for an element encoding failure, |
// instead it emits a warning (possibly) and nulls the value. |
$this->assertSame('{"message":null}', $res); |
} |
} |
|
/** |
* @param mixed $in Input |
* @param mixed $expect Expected output |
* @covers Monolog\Formatter\NormalizerFormatter::detectAndCleanUtf8 |
* @dataProvider providesDetectAndCleanUtf8 |
*/ |
public function testDetectAndCleanUtf8($in, $expect) |
{ |
$formatter = new NormalizerFormatter(); |
$formatter->detectAndCleanUtf8($in); |
$this->assertSame($expect, $in); |
} |
|
public function providesDetectAndCleanUtf8() |
{ |
$obj = new \stdClass; |
|
return array( |
'null' => array(null, null), |
'int' => array(123, 123), |
'float' => array(123.45, 123.45), |
'bool false' => array(false, false), |
'bool true' => array(true, true), |
'ascii string' => array('abcdef', 'abcdef'), |
'latin9 string' => array("\xB1\x31\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xFF", '±1€ŠšŽžŒœŸÿ'), |
'unicode string' => array('¤¦¨´¸¼½¾€ŠšŽžŒœŸ', '¤¦¨´¸¼½¾€ŠšŽžŒœŸ'), |
'empty array' => array(array(), array()), |
'array' => array(array('abcdef'), array('abcdef')), |
'object' => array($obj, $obj), |
); |
} |
|
/** |
* @param int $code |
* @param string $msg |
* @dataProvider providesHandleJsonErrorFailure |
*/ |
public function testHandleJsonErrorFailure($code, $msg) |
{ |
$formatter = new NormalizerFormatter(); |
$reflMethod = new \ReflectionMethod($formatter, 'handleJsonError'); |
$reflMethod->setAccessible(true); |
|
$this->setExpectedException('RuntimeException', $msg); |
$reflMethod->invoke($formatter, $code, 'faked'); |
} |
|
public function providesHandleJsonErrorFailure() |
{ |
return array( |
'depth' => array(JSON_ERROR_DEPTH, 'Maximum stack depth exceeded'), |
'state' => array(JSON_ERROR_STATE_MISMATCH, 'Underflow or the modes mismatch'), |
'ctrl' => array(JSON_ERROR_CTRL_CHAR, 'Unexpected control character found'), |
'default' => array(-1, 'Unknown error'), |
); |
} |
|
public function testExceptionTraceWithArgs() |
{ |
if (defined('HHVM_VERSION')) { |
$this->markTestSkipped('Not supported in HHVM since it detects errors differently'); |
} |
|
// This happens i.e. in React promises or Guzzle streams where stream wrappers are registered |
// and no file or line are included in the trace because it's treated as internal function |
set_error_handler(function ($errno, $errstr, $errfile, $errline) { |
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); |
}); |
|
try { |
// This will contain $resource and $wrappedResource as arguments in the trace item |
$resource = fopen('php://memory', 'rw+'); |
fwrite($resource, 'test_resource'); |
$wrappedResource = new TestFooNorm; |
$wrappedResource->foo = $resource; |
// Just do something stupid with a resource/wrapped resource as argument |
array_keys($wrappedResource); |
} catch (\Exception $e) { |
restore_error_handler(); |
} |
|
$formatter = new NormalizerFormatter(); |
$record = array('context' => array('exception' => $e)); |
$result = $formatter->format($record); |
|
$this->assertRegExp( |
'%"resource":"\[resource\] \(stream\)"%', |
$result['context']['exception']['trace'][0] |
); |
|
if (version_compare(PHP_VERSION, '5.5.0', '>=')) { |
$pattern = '%"wrappedResource":"\[object\] \(Monolog\\\\\\\\Formatter\\\\\\\\TestFooNorm: \)"%'; |
} else { |
$pattern = '%\\\\"foo\\\\":null%'; |
} |
|
// Tests that the wrapped resource is ignored while encoding, only works for PHP <= 5.4 |
$this->assertRegExp( |
$pattern, |
$result['context']['exception']['trace'][0] |
); |
} |
} |
|
class TestFooNorm |
{ |
public $foo = 'foo'; |
} |
|
class TestBarNorm |
{ |
public function __toString() |
{ |
return 'bar'; |
} |
} |
|
class TestStreamFoo |
{ |
public $foo; |
public $resource; |
|
public function __construct($resource) |
{ |
$this->resource = $resource; |
$this->foo = 'BAR'; |
} |
|
public function __toString() |
{ |
fseek($this->resource, 0); |
|
return $this->foo . ' - ' . (string) stream_get_contents($this->resource); |
} |
} |
|
class TestToStringError |
{ |
public function __toString() |
{ |
throw new \RuntimeException('Could not convert to string'); |
} |
} |
/vendor/monolog/monolog/tests/Monolog/Formatter/ScalarFormatterTest.php |
@@ -0,0 +1,110 @@ |
<?php |
|
/* |
* This file is part of the Monolog package. |
* |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
*/ |
|
namespace Monolog\Formatter; |
|
class ScalarFormatterTest extends \PHPUnit_Framework_TestCase |
{ |
private $formatter; |
|
public function setUp() |
{ |
$this->formatter = new ScalarFormatter(); |
} |
|
public function buildTrace(\Exception $e) |
{ |
$data = array(); |
$trace = $e->getTrace(); |
foreach ($trace as $frame) { |
if (isset($frame['file'])) { |
$data[] = $frame['file'].':'.$frame['line']; |
} else { |
$data[] = json_encode($frame); |
} |
} |
|
return $data; |
} |
|
public function encodeJson($data) |
{ |
if (version_compare(PHP_VERSION, '5.4.0', '>=')) { |
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
} |
|
return json_encode($data); |
} |
|
public function testFormat() |
{ |
$exception = new \Exception('foo'); |
$formatted = $this->formatter->format(array( |
'foo' => 'string', |
'bar' => 1, |
'baz' => false, |
'bam' => array(1, 2, 3), |
'bat' => array('foo' => 'bar'), |
'bap' => \DateTime::createFromFormat(\DateTime::ISO8601, '1970-01-01T00:00:00+0000'), |
'ban' => $exception, |
)); |
|
$this->assertSame(array( |
'foo' => 'string', |
'bar' => 1, |
'baz' => false, |
'bam' => $this->encodeJson(array(1, 2, 3)), |
'bat' => $this->encodeJson(array('foo' => 'bar')), |
'bap' => '1970-01-01 00:00:00', |
'ban' => $this->encodeJson(array( |
'class' => get_class($exception), |
'message' => $exception->getMessage(), |
'code' => $exception->getCode(), |
'file' => $exception->getFile() . ':' . $exception->getLine(), |
'trace' => $this->buildTrace($exception), |
)), |
), $formatted); |
} |
|
public function testFormatWithErrorContext() |
{ |
$context = array('file' => 'foo', 'line' => 1); |
$formatted = $this->formatter->format(array( |
'context' => $context, |
)); |
|
$this->assertSame(array( |
'context' => $this->encodeJson($context), |
), $formatted); |
} |
|
public function testFormatWithExceptionContext() |
{ |
$exception = new \Exception('foo'); |
$formatted = $this->formatter->format(array( |
'context' => array( |
'exception' => $exception, |
), |
)); |
|
$this->assertSame(array( |
'context' => $this->encodeJson(array( |
'exception' => array( |
'class' => get_class($exception), |
'message' => $exception->getMessage(), |
'code' => $exception->getCode(), |
'file' => $exception->getFile() . ':' . $exception->getLine(), |
'trace' => $this->buildTrace($exception), |
), |
)), |
), $formatted); |
} |
} |
/vendor/monolog/monolog/tests/Monolog/Formatter/WildfireFormatterTest.php |
@@ -0,0 +1,142 @@ |
<?php |
|
/* |
* This file is part of the Monolog package. |
* |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
*/ |
|
namespace Monolog\Formatter; |
|
use Monolog\Logger; |
|
class WildfireFormatterTest extends \PHPUnit_Framework_TestCase |
{ |
/** |
* @covers Monolog\Formatter\WildfireFormatter::format |
*/ |
public function testDefaultFormat() |
{ |
$wildfire = new WildfireFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('ip' => '127.0.0.1'), |
'message' => 'log', |
); |
|
$message = $wildfire->format($record); |
|
$this->assertEquals( |
'125|[{"Type":"ERROR","File":"","Line":"","Label":"meh"},' |
.'{"message":"log","context":{"from":"logger"},"extra":{"ip":"127.0.0.1"}}]|', |
$message |
); |
} |
|
/** |
* @covers Monolog\Formatter\WildfireFormatter::format |
*/ |
public function testFormatWithFileAndLine() |
{ |
$wildfire = new WildfireFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array('from' => 'logger'), |
'datetime' => new \DateTime("@0"), |
'extra' => array('ip' => '127.0.0.1', 'file' => 'test', 'line' => 14), |
'message' => 'log', |
); |
|
$message = $wildfire->format($record); |
|
$this->assertEquals( |
'129|[{"Type":"ERROR","File":"test","Line":14,"Label":"meh"},' |
.'{"message":"log","context":{"from":"logger"},"extra":{"ip":"127.0.0.1"}}]|', |
$message |
); |
} |
|
/** |
* @covers Monolog\Formatter\WildfireFormatter::format |
*/ |
public function testFormatWithoutContext() |
{ |
$wildfire = new WildfireFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array(), |
'datetime' => new \DateTime("@0"), |
'extra' => array(), |
'message' => 'log', |
); |
|
$message = $wildfire->format($record); |
|
$this->assertEquals( |
'58|[{"Type":"ERROR","File":"","Line":"","Label":"meh"},"log"]|', |
$message |
); |
} |
|
/** |
* @covers Monolog\Formatter\WildfireFormatter::formatBatch |
* @expectedException BadMethodCallException |
*/ |
public function testBatchFormatThrowException() |
{ |
$wildfire = new WildfireFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'meh', |
'context' => array(), |
'datetime' => new \DateTime("@0"), |
'extra' => array(), |
'message' => 'log', |
); |
|
$wildfire->formatBatch(array($record)); |
} |
|
/** |
* @covers Monolog\Formatter\WildfireFormatter::format |
*/ |
public function testTableFormat() |
{ |
$wildfire = new WildfireFormatter(); |
$record = array( |
'level' => Logger::ERROR, |
'level_name' => 'ERROR', |
'channel' => 'table-channel', |
'context' => array( |
WildfireFormatter::TABLE => array( |
array('col1', 'col2', 'col3'), |
array('val1', 'val2', 'val3'), |
array('foo1', 'foo2', 'foo3'), |
array('bar1', 'bar2', 'bar3'), |
), |
), |
'datetime' => new \DateTime("@0"), |
'extra' => array(), |
'message' => 'table-message', |
); |
|
$message = $wildfire->format($record); |
|
$this->assertEquals( |
'171|[{"Type":"TABLE","File":"","Line":"","Label":"table-channel: table-message"},[["col1","col2","col3"],["val1","val2","val3"],["foo1","foo2","foo3"],["bar1","bar2","bar3"]]]|', |
$message |
); |
} |
} |