From 0b88a23cb85d0c67503ba031b2c64e1d0ac9a994 Mon Sep 17 00:00:00 2001 From: Damien Alexandre Date: Sat, 29 Nov 2014 12:05:04 +0100 Subject: [PATCH] [Stopwatch] added __toString on StopwatchEvent --- src/Symfony/Component/Stopwatch/StopwatchEvent.php | 8 ++++++++ .../Component/Stopwatch/Tests/StopwatchEventTest.php | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Symfony/Component/Stopwatch/StopwatchEvent.php b/src/Symfony/Component/Stopwatch/StopwatchEvent.php index 453437f011..81140421ed 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchEvent.php +++ b/src/Symfony/Component/Stopwatch/StopwatchEvent.php @@ -234,4 +234,12 @@ class StopwatchEvent return round($time, 1); } + + /** + * @return string + */ + public function __toString() + { + return sprintf('%s: %.2F MiB - %d ms', $this->getCategory(), $this->getMemory() / 1024 / 1024, $this->getDuration()); + } } diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php index 1881f8afeb..fb7183d5e4 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php @@ -156,4 +156,16 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase { new StopwatchEvent("abc"); } + + public function testHumanRepresentation() + { + $event = new StopwatchEvent(microtime(true) * 1000); + $this->assertEquals('default: 0.00 MiB - 0 ms', (string) $event); + $event->start(); + $event->stop(); + $this->assertEquals(1, preg_match('/default: [0-9\.]+ MiB - [0-9]+ ms/', (string) $event)); + + $event = new StopwatchEvent(microtime(true) * 1000, 'foo'); + $this->assertEquals('foo: 0.00 MiB - 0 ms', (string) $event); + } }