ProfilerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
  15. use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
  16. use Symfony\Component\HttpKernel\Profiler\FileProfilerStorage;
  17. use Symfony\Component\HttpKernel\Profiler\Profiler;
  18. class ProfilerTest extends TestCase
  19. {
  20. private $tmp;
  21. private $storage;
  22. public function testCollect()
  23. {
  24. $request = new Request();
  25. $request->query->set('foo', 'bar');
  26. $request->server->set('REMOTE_ADDR', '127.0.0.1');
  27. $response = new Response('', 204);
  28. $collector = new RequestDataCollector();
  29. $profiler = new Profiler($this->storage);
  30. $profiler->add($collector);
  31. $profile = $profiler->collect($request, $response);
  32. $profiler->saveProfile($profile);
  33. $this->assertSame(204, $profile->getStatusCode());
  34. $this->assertSame('GET', $profile->getMethod());
  35. $this->assertSame('bar', $profile->getCollector('request')->getRequestQuery()->all()['foo']->getValue());
  36. }
  37. public function testReset()
  38. {
  39. $collector = $this->getMockBuilder(DataCollectorInterface::class)
  40. ->onlyMethods(['collect', 'getName', 'reset'])
  41. ->getMock();
  42. $collector->expects($this->any())->method('getName')->willReturn('mock');
  43. $collector->expects($this->once())->method('reset');
  44. $profiler = new Profiler($this->storage);
  45. $profiler->add($collector);
  46. $profiler->reset();
  47. }
  48. public function testFindWorksWithDates()
  49. {
  50. $profiler = new Profiler($this->storage);
  51. $this->assertCount(0, $profiler->find(null, null, null, null, '7th April 2014', '9th April 2014'));
  52. }
  53. public function testFindWorksWithTimestamps()
  54. {
  55. $profiler = new Profiler($this->storage);
  56. $this->assertCount(0, $profiler->find(null, null, null, null, '1396828800', '1397001600'));
  57. }
  58. public function testFindWorksWithInvalidDates()
  59. {
  60. $profiler = new Profiler($this->storage);
  61. $this->assertCount(0, $profiler->find(null, null, null, null, 'some string', ''));
  62. }
  63. public function testFindWorksWithStatusCode()
  64. {
  65. $profiler = new Profiler($this->storage);
  66. $this->assertCount(0, $profiler->find(null, null, null, null, null, null, '204'));
  67. }
  68. protected function setUp(): void
  69. {
  70. $this->tmp = tempnam(sys_get_temp_dir(), 'sf_profiler');
  71. if (file_exists($this->tmp)) {
  72. @unlink($this->tmp);
  73. }
  74. $this->storage = new FileProfilerStorage('file:'.$this->tmp);
  75. $this->storage->purge();
  76. }
  77. protected function tearDown(): void
  78. {
  79. if (null !== $this->storage) {
  80. $this->storage->purge();
  81. $this->storage = null;
  82. @unlink($this->tmp);
  83. }
  84. }
  85. }