8000 bug #10669 [Profiler] Prevent throwing fatal errors when searching ti… · sandermarechal/symfony@241dc10 · GitHub
[go: up one dir, main page]

Skip to content

Commit 241dc10

Browse files
committed
bug symfony#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
2 parents d290e79 + eea9d24 commit 241dc10

File tree

2 files changed

+67
-35
lines changed

2 files changed

+67
-35
lines changed

src/Symfony/Component/HttpKernel/Profiler/Profiler.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,25 +167,11 @@ public function import($data)
167167
*
168168
* @return array An array of tokens
169169
*
170-
* @see http://fr2.php.net/manual/en/datetime.formats.php for the supported date/time formats
170+
* @see http://php.net/manual/en/datetime.formats.php for the supported date/time formats
171171
*/
172172
public function find($ip, $url, $limit, $method, $start, $end)
173173
{
174-
if ('' != $start && null !== $start) {
175-
$start = new \DateTime($start);
176-
$start = $start->getTimestamp();
177-
} else {
178-
$start = null;
179-
}
180-
181-
if ('' != $end && null !== $end) {
182-
$end = new \DateTime($end);
183-
$end = $end->getTimestamp();
184-
} else {
185-
$end = null;
186-
}
187-
188-
return $this->storage->find($ip, $url, $limit, $method, $start, $end);
174+
return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end));
189175
}
190176

191177
/**
@@ -283,4 +269,19 @@ public function get($name)
283269

284270
return $this->collectors[$name];
285271
}
272+
273+
private function getTimestamp($value)
274+
{
275+
if (null === $value || '' == $value) {
276+
return null;
277+
}
278+
279+
try {
280+
$value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
281+
} catch (\Exception $e) {
282+
return null;
283+
}
284+
285+
return $value->getTimestamp();
286+
}
286287
}

src/Symfony/Component/HttpKernel/Tests/Profiler/ProfilerTest.php

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,69 @@
1919

2020
class ProfilerTest extends \PHPUnit_Framework_TestCase
2121
{
22-
protected function setUp()
23-
{
24-
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
25-
$this->markTestSkipped('The "HttpFoundation" component is not available');
26-
}
27-
}
22+
private $tmp;
23+
private $storage;
2824

2925
public function testCollect()
3026
{
31-
if (!class_exists('SQLite3') && (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers()))) {
32-
$this->markTestSkipped('This test requires SQLite support in your environment');
33-
}
34-
3527
$request = new Request();
3628
$request->query->set('foo', 'bar');
3729
$response = new Response();
3830
$collector = new RequestDataCollector();
3931

40-
$tmp = tempnam(sys_get_temp_dir(), 'sf2_profiler');
41-
if (file_exists($tmp)) {
42-
@unlink($tmp);
43-
}
44-
$storage = new SqliteProfilerStorage('sqlite:'.$tmp);
45-
$storage->purge();
46-
47-
$profiler = new Profiler($storage);
32+
$profiler = new Profiler($this->storage);
4833
$profiler->add($collector);
4934
$profile = $profiler->collect($request, $response);
5035

5136
$profile = $profiler->loadProfile($profile->getToken());
5237
$this->assertEquals(array('foo' => 'bar'), $profiler->get('request')->getRequestQuery()->all());
38+
}
39+
40+
public function testFindWorksWithDates()
41+
{
42+
$profiler = new Profiler($this->storage);
43+
44+
$this->assertCount(0, $profiler->find(null, null, null, null, '7th April 2014', '9th April 2014'));
45+
}
46+
47+
public function testFindWorksWithTimestamps()
48+
{
49+
$profiler = new Profiler($this->storage);
50+
51+
$this->assertCount(0, $profiler->find(null, null, null, null, '1396828800', '1397001600'));
52+
}
53+
54+
public function testFindWorksWithInvalidDates()
55+
{
56+
$profiler = new Profiler($this->storage);
57+
58+
$this->assertCount(0, $profiler->find(null, null, null, null, 'some string', ''));
59+
}
60+
61+
protected function setUp()
62+
{
63+
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
64+
$this->markTestSkipped('The "HttpFoundation" component is not available');
65+
}
66+
67+
if (!class_exists('SQLite3') && (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers()))) {
68+
$this->markTestSkipped('This test requires SQLite support in your environment');
69+
}
70+
71+
$this->tmp = tempnam(sys_get_temp_dir(), 'sf2_profiler');
72+
if (file_exists($this->tmp)) {
73+
@unlink($this->tmp);
74+
}
75+
76+
$this->storage = new SqliteProfilerStorage('sqlite:'.$this->tmp);
77+
$this->storage->purge();
78+
}
79+
80+
protected function tearDown()
81+
{
82+
$this->storage->purge();
83+
$this->storage = null;
5384

54-
@unlink($tmp);
85+
@unlink($this->tmp);
5586
}
5687
}

0 commit comments

Comments
 (0)
0