diff --git a/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php b/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php index b2437dc1fc..4462519196 100644 --- a/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php +++ b/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\EventDispatcher\Tests\Debug; use PHPUnit\Framework\TestCase; +use Symfony\Component\Debug\BufferingLogger; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\EventDispatcher; @@ -206,41 +207,57 @@ class TraceableEventDispatcherTest extends TestCase public function testLogger() { - $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock(); + $logger = new BufferingLogger(); $dispatcher = new EventDispatcher(); $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger); $tdispatcher->addListener('foo', $listener1 = function () {}); $tdispatcher->addListener('foo', $listener2 = function () {}); - $logger->expects($this->exactly(2)) - ->method('debug') - ->withConsecutive( - ['Notified event "{event}" to listener "{listener}".', ['event' => 'foo', 'listener' => 'closure']], - ['Notified event "{event}" to listener "{listener}".', ['event' => 'foo', 'listener' => 'closure']] - ); - $tdispatcher->dispatch(new Event(), 'foo'); + + $this->assertSame([ + [ + 'debug', + 'Notified event "{event}" to listener "{listener}".', + ['event' => 'foo', 'listener' => 'closure'], + ], + [ + 'debug', + 'Notified event "{event}" to listener "{listener}".', + ['event' => 'foo', 'listener' => 'closure'], + ], + ], $logger->cleanLogs()); } public function testLoggerWithStoppedEvent() { - $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock(); + $logger = new BufferingLogger(); $dispatcher = new EventDispatcher(); $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger); $tdispatcher->addListener('foo', $listener1 = function (Event $event) { $event->stopPropagation(); }); $tdispatcher->addListener('foo', $listener2 = function () {}); - $logger->expects($this->exactly(3)) - ->method('debug') - ->withConsecutive( - ['Notified event "{event}" to listener "{listener}".', ['event' => 'foo', 'listener' => 'closure']], - ['Listener "{listener}" stopped propagation of the event "{event}".', ['event' => 'foo', 'listener' => 'closure']], - ['Listener "{listener}" was not called for event "{event}".', ['event' => 'foo', 'listener' => 'closure']] - ); - $tdispatcher->dispatch(new Event(), 'foo'); + + $this->assertSame([ + [ + 'debug', + 'Notified event "{event}" to listener "{listener}".', + ['event' => 'foo', 'listener' => 'closure'], + ], + [ + 'debug', + 'Listener "{listener}" stopped propagation of the event "{event}".', + ['event' => 'foo', 'listener' => 'closure'], + ], + [ + 'debug', + 'Listener "{listener}" was not called for event "{event}".', + ['event' => 'foo', 'listener' => 'closure'], + ], + ], $logger->cleanLogs()); } public function testDispatchCallListeners() diff --git a/src/Symfony/Component/EventDispatcher/composer.json b/src/Symfony/Component/EventDispatcher/composer.json index 62bea6cb11..16c35a156c 100644 --- a/src/Symfony/Component/EventDispatcher/composer.json +++ b/src/Symfony/Component/EventDispatcher/composer.json @@ -23,6 +23,7 @@ "symfony/dependency-injection": "^3.4|^4.0|^5.0", "symfony/expression-language": "^3.4|^4.0|^5.0", "symfony/config": "^3.4|^4.0|^5.0", + "symfony/debug": "~3.4|~4.4", "symfony/http-foundation": "^3.4|^4.0|^5.0", "symfony/service-contracts": "^1.1|^2", "symfony/stopwatch": "^3.4|^4.0|^5.0", diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index bb1e1f1590..3875144dc1 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -90,7 +90,7 @@ class Inline } // some comments are allowed at the end - if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) { + if (preg_replace('/\s*#.*$/A', '', substr($value, $i))) { throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)), self::$parsedLineNumber + 1, $value, self::$parsedFilename); } diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index b5e19dd692..997de27965 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -102,6 +102,7 @@ class Parser $this->refs = []; $this->skippedLineNumbers = []; $this->locallySkippedLineNumbers = []; + $this->totalNumberOfLines = null; } return $data; diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index c2f79c8530..b4143b9c17 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -799,6 +799,16 @@ class InlineTest extends TestCase self::assertSame('-0123456789', Inline::parse('-0123456789')); } + public function testParseCommentNotPrefixedBySpaces() + { + self::assertSame('foo', Inline::parse('"foo"#comment')); + } + + public function testParseUnquotedStringContainingHashTagNotPrefixedBySpace() + { + self::assertSame('foo#nocomment', Inline::parse('foo#nocomment')); + } + /** * @dataProvider unquotedExclamationMarkThrowsProvider */ diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 2c19375c25..ce682228ac 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -2438,6 +2438,39 @@ YAML; $this->parser->parse($yaml) ); } + + /** + * This is a regression test for a bug where a YAML block with a nested multiline string using | was parsed without + * a trailing \n when a shorter YAML document was parsed before. + * + * When a shorter document was parsed before, the nested string did not have a \n at the end of the string, because + * the Parser thought it was the end of the file, even though it is not. + */ + public function testParsingMultipleDocuments() + { + $shortDocument = 'foo: bar'; + $longDocument = << ['b' => "row\nrow2\n"], 'c' => 'd']; + + // The parser was not used before, so there is a new line after row2 + $this->assertSame($expected, $this->parser->parse($longDocument)); + + $parser = new Parser(); + // The first parsing set and fixed the totalNumberOfLines in the Parser before, so parsing the short document here + // to reproduce the issue. If the issue would not have been fixed, the next assertion will fail + $parser->parse($shortDocument); + + // After the total number of lines has been rset the result will be the same as if a new parser was used + // (before, there was no \n after row2) + $this->assertSame($expected, $parser->parse($longDocument)); + } } class B