4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\Profiler;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
18 use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
19 use Symfony\Component\HttpKernel\Profiler\FileProfilerStorage;
20 use Symfony\Component\HttpKernel\Profiler\Profiler;
22 class ProfilerTest extends TestCase
27 public function testCollect()
29 $request = new Request();
30 $request->query->set('foo', 'bar');
31 $response = new Response('', 204);
32 $collector = new RequestDataCollector();
34 $profiler = new Profiler($this->storage);
35 $profiler->add($collector);
36 $profile = $profiler->collect($request, $response);
37 $profiler->saveProfile($profile);
39 $this->assertSame(204, $profile->getStatusCode());
40 $this->assertSame('GET', $profile->getMethod());
41 $this->assertSame('bar', $profile->getCollector('request')->getRequestQuery()->all()['foo']->getValue());
44 public function testReset()
46 $collector = $this->getMockBuilder(DataCollectorInterface::class)
47 ->setMethods(array('collect', 'getName', 'reset'))
49 $collector->expects($this->any())->method('getName')->willReturn('mock');
50 $collector->expects($this->once())->method('reset');
52 $profiler = new Profiler($this->storage);
53 $profiler->add($collector);
57 public function testFindWorksWithDates()
59 $profiler = new Profiler($this->storage);
61 $this->assertCount(0, $profiler->find(null, null, null, null, '7th April 2014', '9th April 2014'));
64 public function testFindWorksWithTimestamps()
66 $profiler = new Profiler($this->storage);
68 $this->assertCount(0, $profiler->find(null, null, null, null, '1396828800', '1397001600'));
71 public function testFindWorksWithInvalidDates()
73 $profiler = new Profiler($this->storage);
75 $this->assertCount(0, $profiler->find(null, null, null, null, 'some string', ''));
78 public function testFindWorksWithStatusCode()
80 $profiler = new Profiler($this->storage);
82 $this->assertCount(0, $profiler->find(null, null, null, null, null, null, '204'));
85 protected function setUp()
87 $this->tmp = tempnam(sys_get_temp_dir(), 'sf2_profiler');
88 if (file_exists($this->tmp)) {
92 $this->storage = new FileProfilerStorage('file:'.$this->tmp);
93 $this->storage->purge();
96 protected function tearDown()
98 if (null !== $this->storage) {
99 $this->storage->purge();
100 $this->storage = null;