| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?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\DateTimeImmutable;
- 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)
- {
- return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
- }
- 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' => $dt = new DateTimeImmutable(true),
- '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' => (string) $dt,
- '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);
- }
- }
|