3 namespace Drupal\Tests\Core\Logger;
5 use Drupal\Core\Logger\LogMessageParser;
6 use Drupal\Tests\UnitTestCase;
9 * @coversDefaultClass \Drupal\Core\Logger\LogMessageParser
12 class LogMessageParserTest extends UnitTestCase {
15 * Test for LogMessageParserTrait::parseMessagePlaceholders()
18 * An array containing:
19 * - message: A string that contains a message with placeholders.
20 * - context: An array with placeholder values.
21 * @param array $expected
22 * An array with the expected values after the test has run.
23 * - message: The expected parsed message.
24 * - context: The expected values of the placeholders.
26 * @dataProvider providerTestParseMessagePlaceholders
27 * @covers ::parseMessagePlaceholders
29 public function testParseMessagePlaceholders(array $value, array $expected) {
30 $parser = new LogMessageParser();
31 $message_placeholders = $parser->parseMessagePlaceholders($value['message'], $value['context']);
32 $this->assertEquals($expected['message'], $value['message']);
33 $this->assertEquals($expected['context'], $message_placeholders);
37 * Data provider for testParseMessagePlaceholders().
39 public function providerTestParseMessagePlaceholders() {
43 ['message' => 'User {username} created', 'context' => ['username' => 'Dries']],
44 ['message' => 'User @username created', 'context' => ['@username' => 'Dries']],
46 // PSR3 style mixed in a format_string style message.
48 ['message' => 'User {username} created @time', 'context' => ['username' => 'Dries', '@time' => 'now']],
49 ['message' => 'User @username created @time', 'context' => ['@username' => 'Dries', '@time' => 'now']],
51 // format_string style message only.
53 ['message' => 'User @username created', 'context' => ['@username' => 'Dries']],
54 ['message' => 'User @username created', 'context' => ['@username' => 'Dries']],
56 // Message without placeholders but wildcard characters.
58 ['message' => 'User W-\\};~{&! created @', 'context' => ['' => '']],
59 ['message' => 'User W-\\};~{&! created @', 'context' => []],
61 // Message with double PSR3 style messages.
63 ['message' => 'Test {with} two {encapsuled} strings', 'context' => ['with' => 'together', 'encapsuled' => 'awesome']],
64 ['message' => 'Test @with two @encapsuled strings', 'context' => ['@with' => 'together', '@encapsuled' => 'awesome']],