|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Log\Tests\Message; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Yiisoft\Log\Message\ContextValueExtractor; |
| 9 | + |
| 10 | +final class ContextValueExtractorTest extends TestCase |
| 11 | +{ |
| 12 | + public static function dataExtract(): array |
| 13 | + { |
| 14 | + return [ |
| 15 | + 'empty-key-found' => [['' => 'found'], '', [true, 'found']], |
| 16 | + 'empty-key-not-found' => [['foo' => 'bar'], '', [false, null]], |
| 17 | + 'simple-key' => [['foo' => 'bar'], 'foo', [true, 'bar']], |
| 18 | + 'missing-key' => [['foo' => 'bar'], 'baz', [false, null]], |
| 19 | + 'nested-key' => [['user' => ['name' => 'John']], 'user.name', [true, 'John']], |
| 20 | + 'nested-key-not-found' => [['user' => ['name' => 'John']], 'user.age', [false, null]], |
| 21 | + 'nested-key-non-array-intermediate' => [['user' => 'string'], 'user.name', [false, null]], |
| 22 | + 'escaped-dot' => [['user.name' => 'John'], 'user\.name', [true, 'John']], |
| 23 | + 'escaped-backslash' => [['user\\' => 'John'], 'user\\\\', [true, 'John']], |
| 24 | + 'deeply-nested' => [['a' => ['b' => ['c' => 'deep']]], 'a.b.c', [true, 'deep']], |
| 25 | + 'backslash-key-nested' => [['a\\' => ['b' => 'value']], 'a\\\\.b', [true, 'value']], |
| 26 | + 'multiple-backslashes-before-dot' => [['a\\\\' => ['b' => 'value']], 'a\\\\\\\\.b', [true, 'value']], |
| 27 | + 'escaped-dot-and-nesting' => [['a.b' => ['c' => 'value']], 'a\\.b.c', [true, 'value']], |
| 28 | + ]; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @dataProvider dataExtract |
| 33 | + */ |
| 34 | + public function testExtract(array $context, string $key, array $expected): void |
| 35 | + { |
| 36 | + $this->assertSame($expected, ContextValueExtractor::extract($context, $key)); |
| 37 | + } |
| 38 | +} |
0 commit comments