ProfilerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $response = new Response('', 204);
  27. $collector = new RequestDataCollector();
  28. $profiler = new Profiler($this->storage);
  29. $profiler->add($collector);
  30. $profile = $profiler->collect($request, $response);
  31. $profiler->saveProfile($profile);
  32. $this->assertSame(204, $profile->getStatusCode());
  33. $this->assertSame('GET', $profile->getMethod());
  34. $this->assertSame('bar', $profile->getCollector('request')->getRequestQuery()->all()['foo']->getValue());
  35. }
  36. public function testReset()
  37. {
  38. $collector = $this->getMockBuilder(DataCollectorInterface::class)
  39. ->setMethods(['collect', 'getName', 'reset'])
  40. ->getMock();
  41. $collector->expects($this->any())->method('getName')->willReturn('mock');
  42. $collector->expects($this->once())->method('reset');
  43. $profiler = new Profiler($this->storage);
  44. $profiler->add($collector);
  45. $profiler->reset();
  46. }
  47. public function testFindWorksWithDates()
  48. {
  49. $profiler = new Profiler($this->storage);
  50. $this->assertCount(0, $profiler->find(null, null, null, null, '7th April 2014', '9th April 2014'));
  51. }
  52. public function testFindWorksWithTimestamps()
  53. {
  54. $profiler = new Profiler($this->storage);
  55. $this->assertCount(0, $profiler->find(null, null, null, null, '1396828800', '1397001600'));
  56. }
  57. public function testFindWorksWithInvalidDates()
  58. {
  59. $profiler = new Profiler($this->storage);
  60. $this->assertCount(0, $profiler->find(null, null, null, null, 'some string', ''));
  61. }
  62. public function testFindWorksWithStatusCode()
  63. {
  64. $profiler = new Profiler($this->storage);
  65. $this->assertCount(0, $profiler->find(null, null, null, null, null, null, '204'));
  66. }
  67. protected function setUp()
  68. {
  69. $this->tmp = tempnam(sys_get_temp_dir(), 'sf_profiler');
  70. if (file_exists($this->tmp)) {
  71. @unlink($this->tmp);
  72. }
  73. $this->storage = new FileProfilerStorage('file:'.$this->tmp);
  74. $this->storage->purge();
  75. }
  76. protected function tearDown()
  77. {
  78. if (null !== $this->storage) {
  79. $this->storage->purge();
  80. $this->storage = null;
  81. @unlink($this->tmp);
  82. }
  83. }
  84. }