diff --git a/core/tests/Drupal/Tests/Core/Logger/LogMessageParserTest.php b/core/tests/Drupal/Tests/Core/Logger/LogMessageParserTest.php index e853dd0..da6d85e 100644 --- a/core/tests/Drupal/Tests/Core/Logger/LogMessageParserTest.php +++ b/core/tests/Drupal/Tests/Core/Logger/LogMessageParserTest.php @@ -39,23 +39,40 @@ class LogMessageParserTest extends UnitTestCase { * An array with the expected values after the test has run. * - message: The expected parsed message. * - context: The expected values of the placeholders. - * @param bool $stringable_placeholders - * A boolean flag indicating if the placeholders should be convertible - * into strings. * * @dataProvider providerTestParseMessagePlaceholders * @covers ::parseMessagePlaceholders */ - public function testParseMessagePlaceholders(array $value, array $expected, $stringable_placeholders) { + public function testParseMessagePlaceholders(array $value, array $expected) { + $parser = new LogMessageParser(); + + $message_placeholders = $parser->parseMessagePlaceholders($value['message'], $value['context']); + $this->assertEquals($expected['message'], $value['message']); + $this->assertEquals($expected['context'], $message_placeholders); + } + + /** + * Test for LogMessageParserTrait::parseMessagePlaceholders() + * + * @param array $value + * An array containing: + * - message: A string that contains a message with placeholders. + * - context: An array with placeholder values. + * @param array $expected + * An array with the expected values after the test has run. + * - message: The expected parsed message. + * - context: The expected values of the placeholders. + * + * @dataProvider providerTestParseMessageNonStringablePlaceholders + * @covers ::parseMessagePlaceholders + */ + public function testParseMessageInvalidPlaceholders(array $value, array $expected) { $parser = new LogMessageParser(); try { $message_placeholders = $parser->parseMessagePlaceholders($value['message'], $value['context']); - $this->assertEquals($expected['message'], $value['message']); - $this->assertEquals($expected['context'], $message_placeholders); - $this->assertTrue($stringable_placeholders); + $this->fail("Never executed."); } catch (InvalidArgumentException $e) { - $this->assertFalse($stringable_placeholders); $this->assertStringStartsWith('Log context contains non-string value for key', $e->getMessage()); } } @@ -64,86 +81,85 @@ public function testParseMessagePlaceholders(array $value, array $expected, $str * Data provider for testParseMessagePlaceholders(). */ public function providerTestParseMessagePlaceholders() { + // @codingStandardsIgnoreStart return [ // PSR3 only message. [ ['message' => 'User {username} created', 'context' => ['username' => 'Dries']], ['message' => 'User @username created', 'context' => ['@username' => 'Dries']], - TRUE, ], // PSR3 style mixed in a format_string style message. [ ['message' => 'User {username} created @time', 'context' => ['username' => 'Dries', '@time' => 'now']], ['message' => 'User @username created @time', 'context' => ['@username' => 'Dries', '@time' => 'now']], - TRUE, ], // format_string style message only. [ ['message' => 'User @username created', 'context' => ['@username' => 'Dries']], ['message' => 'User @username created', 'context' => ['@username' => 'Dries']], - TRUE, ], // format_string style message only. [ ['message' => 'Example :url', 'context' => [':url' => 'http://example.com']], ['message' => 'Example :url', 'context' => [':url' => 'http://example.com']], - TRUE, ], // Message without placeholders but wildcard characters. [ ['message' => 'User W-\\};~{&! created @', 'context' => ['' => '']], ['message' => 'User W-\\};~{&! created @', 'context' => []], - TRUE, ], // Message with double PSR3 style messages. [ ['message' => 'Test {with} two {encapsuled} strings', 'context' => ['with' => 'together', 'encapsuled' => 'awesome']], ['message' => 'Test @with two @encapsuled strings', 'context' => ['@with' => 'together', '@encapsuled' => 'awesome']], - TRUE, ], - // Placeholders not convertible into a string. + // Placeholders convertible into a string. [ - ['message' => 'array @a', 'context' => ['@a' => []]], - ['message' => 'array @a', 'context' => ['@a' => []]], - FALSE, + ['message' => 'object @b', 'context' => ['@b' => new StringConvertible()]], + ['message' => 'object @b', 'context' => ['@b' => 'convertible']], + ], + // Message without placeholders, but with other context values. + [ + ['message' => 'message', 'context' => ['not_a_placeholder' => new NotStringConvertible()]], + ['message' => 'message', 'context' => []], // All non placeholders are filtered out. ], + ]; + // @codingStandardsIgnoreEnd + } + + /** + * Data provider for testParseMessageInvalidPlaceholders(). + */ + public function providerTestParseMessageNonStringablePlaceholders() { + // @codingStandardsIgnoreStart + return [ // Placeholders not convertible into a string. [ - ['message' => 'object @b', 'context' => ['@b' => new StringConvertible()]], - ['message' => 'object @b', 'context' => ['@b' => 'convertible']], - TRUE, + ['message' => 'array @a', 'context' => ['@a' => []]], + ['message' => 'array @a', 'context' => ['@a' => []]], ], // Placeholders not convertible into a string. [ ['message' => 'object @b', 'context' => ['@b' => new NotStringConvertible()]], ['message' => 'object @b', 'context' => ['@b' => new NotStringConvertible()]], - FALSE, ], // Closures are not convertible into a string. [ ['message' => 'closure @c', 'context' => ['@c' => function() {} ]], ['message' => 'closure @c', 'context' => ['@c' => function() {} ]], - FALSE, ], // Resources cannot be converted into a string. [ ['message' => 'resource @r', 'context' => ['@r' => fopen('php://memory', 'r+') ]], ['message' => 'resource @r', 'context' => ['@r' => fopen('php://memory', 'r+') ]], - FALSE, ], // Placeholders not converible into strings that are not the first placeholder. [ ['message' => 'mixed @a @b @c', 'context' => ['@a' => 123, '@b' => [1], '@c' => TRUE]], ['message' => 'mixed @a @b @c', 'context' => ['@a' => 123, '@b' => [1], '@c' => TRUE]], - FALSE, - ], - // Message without placeholders, but with other context values. - [ - ['message' => 'message', 'context' => ['not_a_placeholder' => new NotStringConvertible()]], - ['message' => 'message', 'context' => []], // All non placeholders are filtered out. - TRUE, ], ]; + // @codingStandardsIgnoreEnd } }