ProfilerTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests\Profiler;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
  13. use Symfony\Component\HttpKernel\Profiler\FileProfilerStorage;
  14. use Symfony\Component\HttpKernel\Profiler\Profiler;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. class ProfilerTest extends TestCase
  18. {
  19. private $tmp;
  20. private $storage;
  21. public function testCollect()
  22. {
  23. $request = new Request();
  24. $request->query->set('foo', 'bar');
  25. $response = new Response('', 204);
  26. $collector = new RequestDataCollector();
  27. $profiler = new Profiler($this->storage);
  28. $profiler->add($collector);
  29. $profile = $profiler->collect($request, $response);
  30. $profiler->saveProfile($profile);
  31. $this->assertSame(204, $profile->getStatusCode());
  32. $this->assertSame('GET', $profile->getMethod());
  33. $this->assertSame('bar', $profile->getCollector('request')->getRequestQuery()->all()['foo']->getValue());
  34. }
  35. public function testFindWorksWithDates()
  36. {
  37. $profiler = new Profiler($this->storage);
  38. $this->assertCount(0, $profiler->find(null, null, null, null, '7th April 2014', '9th April 2014'));
  39. }
  40. public function testFindWorksWithTimestamps()
  41. {
  42. $profiler = new Profiler($this->storage);
  43. $this->assertCount(0, $profiler->find(null, null, null, null, '1396828800', '1397001600'));
  44. }
  45. public function testFindWorksWithInvalidDates()
  46. {
  47. $profiler = new Profiler($this->storage);
  48. $this->assertCount(0, $profiler->find(null, null, null, null, 'some string', ''));
  49. }
  50. public function testFindWorksWithStatusCode()
  51. {
  52. $profiler = new Profiler($this->storage);
  53. $this->assertCount(0, $profiler->find(null, null, null, null, null, null, '204'));
  54. }
  55. protected function setUp()
  56. {
  57. $this->tmp = tempnam(sys_get_temp_dir(), 'sf2_profiler');
  58. if (file_exists($this->tmp)) {
  59. @unlink($this->tmp);
  60. }
  61. $this->storage = new FileProfilerStorage('file:'.$this->tmp);
  62. $this->storage->purge();
  63. }
  64. protected function tearDown()
  65. {
  66. if (null !== $this->storage) {
  67. $this->storage->purge();
  68. $this->storage = null;
  69. @unlink($this->tmp);
  70. }
  71. }
  72. }