feature #12666 [Hackday][Stopwatch] added __toString on StopwatchEvent (damienalexandre)

This PR was merged into the 2.7 branch.

Discussion
----------

[Hackday][Stopwatch] added __toString on StopwatchEvent

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #12152
| License       | MIT
| Doc PR        | no

This allow to just print the events in a log or something.

```php
$stopwatch = new Stopwatch();
$event = $stopwatch->start('foo', 'cat');

echo $event; // cat: 0.00 MiB - 0 ms
```

Commits
-------

0b88a23 [Stopwatch] added __toString on StopwatchEvent
This commit is contained in:
Fabien Potencier 2014-11-29 14:10:59 +01:00
commit 0c7df5898f
2 changed files with 20 additions and 0 deletions

View File

@ -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());
}
}

View File

@ -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);
}
}