From b1b2950d5d4d6c26717b59972bcd343ee4e9be3c Mon Sep 17 00:00:00 2001 From: Abdul Rahmad Pasaribu Date: Fri, 22 Jul 2022 15:24:50 +0700 Subject: [PATCH 1/3] test: add more tests for simple formatter --- test/Formatter/SimpleTest.php | 83 +++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/test/Formatter/SimpleTest.php b/test/Formatter/SimpleTest.php index d344bdf8..4e3f367e 100644 --- a/test/Formatter/SimpleTest.php +++ b/test/Formatter/SimpleTest.php @@ -4,11 +4,18 @@ namespace LaminasTest\Log\Formatter; +use ArrayIterator; use DateTime; +use EmptyIterator; use Laminas\Log\Exception\InvalidArgumentException; use Laminas\Log\Formatter\Simple; +use LaminasTest\Log\TestAsset\StringObject; use PHPUnit\Framework\TestCase; use RuntimeException; +use stdClass; + +use function fopen; +use function range; class SimpleTest extends TestCase { @@ -53,6 +60,82 @@ public function testDefaultFormat(): void $this->assertEquals($outputExpected, $formatter->format($fields)); } + public function testFormatAllTypes(): void + { + $date = new DateTime('2012-08-28T18:15:00Z'); + $object = new stdClass(); + $object->foo = 'bar'; + $fields = [ + 'timestamp' => $date, + 'message' => 'foo', + 'priority' => 42, + 'priorityName' => 'bar', + 'extra' => [ + 'float' => 0.2, + 'boolean' => false, + 'array_empty' => [], + 'array' => range(0, 4), + 'traversable_empty' => new EmptyIterator(), + 'traversable' => new ArrayIterator(['id', 42]), + 'null' => null, + 'object_empty' => new stdClass(), + 'object' => $object, + 'string object' => new StringObject(), + 'resource' => fopen('php://stdout', 'w'), + ], + ]; + + $outputExpected = '2012-08-28T18:15:00+00:00 bar (42): foo {' + . '"float":0.2,' + . '"boolean":false,' + . '"array_empty":"[]",' + . '"array":"[0,1,2,3,4]",' + . '"traversable_empty":"[]",' + . '"traversable":"[\"id\",42]",' + . '"null":null,' + . '"object_empty":"object(stdClass) {}",' + . '"object":"object(stdClass) {\"foo\":\"bar\"}",' + . '"string object":"Hello World",' + . '"resource":"resource(stream)"}'; + $formatter = new Simple(); + + $this->assertEquals($outputExpected, $formatter->format($fields)); + } + + public function testFormatExtraArrayKeyWithNonArrayValue(): void + { + $date = new DateTime('2012-08-28T18:15:00Z'); + $fields = [ + 'timestamp' => $date, + 'message' => 'foo', + 'priority' => 42, + 'priorityName' => 'bar', + 'extra' => '', + ]; + + $outputExpected = '2012-08-28T18:15:00+00:00 bar (42): foo'; + $formatter = new Simple(); + + $this->assertEquals($outputExpected, $formatter->format($fields)); + } + + public function testFormatExtraArrayKeyWithNullValue(): void + { + $date = new DateTime('2012-08-28T18:15:00Z'); + $fields = [ + 'timestamp' => $date, + 'message' => 'foo', + 'priority' => 42, + 'priorityName' => 'bar', + 'extra' => null, + ]; + + $outputExpected = '2012-08-28T18:15:00+00:00 bar (42): foo'; + $formatter = new Simple(); + + $this->assertEquals($outputExpected, $formatter->format($fields)); + } + /** * @dataProvider provideDateTimeFormats */ From 5f7d124e82a7180fd8fb036043e880354d25cf03 Mon Sep 17 00:00:00 2001 From: Abdul Rahmad Pasaribu Date: Fri, 22 Jul 2022 15:26:06 +0700 Subject: [PATCH 2/3] fix: fix laminas/laminas-log#38 --- src/Formatter/Simple.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Formatter/Simple.php b/src/Formatter/Simple.php index 5a360d81..5c4c2d13 100644 --- a/src/Formatter/Simple.php +++ b/src/Formatter/Simple.php @@ -7,6 +7,7 @@ use Laminas\Log\Exception; use Traversable; +use function array_key_exists; use function count; use function is_array; use function is_string; @@ -65,7 +66,7 @@ public function format($event) $event = parent::format($event); foreach ($event as $name => $value) { - if ('extra' === $name && count($value)) { + if ('extra' === $name && is_array($value) && count($value)) { $value = $this->normalize($value); } elseif ('extra' === $name) { // Don't print an empty array @@ -75,7 +76,7 @@ public function format($event) } if ( - isset($event['extra']) && empty($event['extra']) + array_key_exists('extra', $event) && empty($event['extra']) && false !== strpos($this->format, '%extra%') ) { $output = rtrim($output, ' '); From 3526cbee66db1cb7d450ad1fa989bf87725f6446 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 22 Jul 2022 10:54:42 +0200 Subject: [PATCH 3/3] Allow auto-installation of CS rulesets via composer plugins --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 33a0c646..458ec4c4 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,9 @@ "forum": "https://discourse.laminas.dev" }, "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + }, "sort-packages": true }, "extra": {