bug #10669 [Profiler] Prevent throwing fatal errors when searching timestamps or invalid dates (stloyd)

This PR was merged into the 2.3 branch.

Discussion
----------

[Profiler] Prevent throwing fatal errors when searching timestamps or invalid dates

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

Commits
-------

eea9d24 [Profiler] Prevent throwing fatal errors when searching timestamps or invalid dates
This commit is contained in:
Fabien Potencier 2014-04-11 13:44:37 +02:00
commit 241dc10245
2 changed files with 67 additions and 35 deletions

View File

@ -167,25 +167,11 @@ class Profiler
*
* @return array An array of tokens
*
* @see http://fr2.php.net/manual/en/datetime.formats.php for the supported date/time formats
* @see http://php.net/manual/en/datetime.formats.php for the supported date/time formats
*/
public function find($ip, $url, $limit, $method, $start, $end)
{
if ('' != $start && null !== $start) {
$start = new \DateTime($start);
$start = $start->getTimestamp();
} else {
$start = null;
}
if ('' != $end && null !== $end) {
$end = new \DateTime($end);
$end = $end->getTimestamp();
} else {
$end = null;
}
return $this->storage->find($ip, $url, $limit, $method, $start, $end);
return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end));
}
/**
@ -283,4 +269,19 @@ class Profiler
return $this->collectors[$name];
}
private function getTimestamp($value)
{
if (null === $value || '' == $value) {
return null;
}
try {
$value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
} catch (\Exception $e) {
return null;
}
return $value->getTimestamp();
}
}

View File

@ -19,38 +19,69 @@ use Symfony\Component\HttpFoundation\Response;
class ProfilerTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
$this->markTestSkipped('The "HttpFoundation" component is not available');
}
}
private $tmp;
private $storage;
public function testCollect()
{
if (!class_exists('SQLite3') && (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers()))) {
$this->markTestSkipped('This test requires SQLite support in your environment');
}
$request = new Request();
$request->query->set('foo', 'bar');
$response = new Response();
$collector = new RequestDataCollector();
$tmp = tempnam(sys_get_temp_dir(), 'sf2_profiler');
if (file_exists($tmp)) {
@unlink($tmp);
}
$storage = new SqliteProfilerStorage('sqlite:'.$tmp);
$storage->purge();
$profiler = new Profiler($storage);
$profiler = new Profiler($this->storage);
$profiler->add($collector);
$profile = $profiler->collect($request, $response);
$profile = $profiler->loadProfile($profile->getToken());
$this->assertEquals(array('foo' => 'bar'), $profiler->get('request')->getRequestQuery()->all());
}
@unlink($tmp);
public function testFindWorksWithDates()
{
$profiler = new Profiler($this->storage);
$this->assertCount(0, $profiler->find(null, null, null, null, '7th April 2014', '9th April 2014'));
}
public function testFindWorksWithTimestamps()
{
$profiler = new Profiler($this->storage);
$this->assertCount(0, $profiler->find(null, null, null, null, '1396828800', '1397001600'));
}
public function testFindWorksWithInvalidDates()
{
$profiler = new Profiler($this->storage);
$this->assertCount(0, $profiler->find(null, null, null, null, 'some string', ''));
}
protected function setUp()
{
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
$this->markTestSkipped('The "HttpFoundation" component is not available');
}
if (!class_exists('SQLite3') && (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers()))) {
$this->markTestSkipped('This test requires SQLite support in your environment');
}
$this->tmp = tempnam(sys_get_temp_dir(), 'sf2_profiler');
if (file_exists($this->tmp)) {
@unlink($this->tmp);
}
$this->storage = new SqliteProfilerStorage('sqlite:'.$this->tmp);
$this->storage->purge();
}
protected function tearDown()
{
$this->storage->purge();
$this->storage = null;
@unlink($this->tmp);
}
}