FileProfilerStorageTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Symfony\Component\HttpKernel\Profiler\FileProfilerStorage;
  12. use Symfony\Component\HttpKernel\Profiler\Profile;
  13. class FileProfilerStorageTest extends AbstractProfilerStorageTest
  14. {
  15. private $tmpDir;
  16. private $storage;
  17. protected function cleanDir()
  18. {
  19. $flags = \FilesystemIterator::SKIP_DOTS;
  20. $iterator = new \RecursiveDirectoryIterator($this->tmpDir, $flags);
  21. $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
  22. foreach ($iterator as $file) {
  23. if (is_file($file)) {
  24. unlink($file);
  25. }
  26. }
  27. }
  28. protected function setUp()
  29. {
  30. $this->tmpDir = sys_get_temp_dir().'/sf2_profiler_file_storage';
  31. if (is_dir($this->tmpDir)) {
  32. self::cleanDir();
  33. }
  34. $this->storage = new FileProfilerStorage('file:'.$this->tmpDir);
  35. $this->storage->purge();
  36. }
  37. protected function tearDown()
  38. {
  39. self::cleanDir();
  40. }
  41. /**
  42. * @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
  43. */
  44. protected function getStorage()
  45. {
  46. return $this->storage;
  47. }
  48. public function testMultiRowIndexFile()
  49. {
  50. $iteration = 3;
  51. for ($i = 0; $i < $iteration; ++$i) {
  52. $profile = new Profile('token'.$i);
  53. $profile->setIp('127.0.0.'.$i);
  54. $profile->setUrl('http://foo.bar/'.$i);
  55. $storage = $this->getStorage();
  56. $storage->write($profile);
  57. $storage->write($profile);
  58. $storage->write($profile);
  59. }
  60. $handle = fopen($this->tmpDir.'/index.csv', 'r');
  61. for ($i = 0; $i < $iteration; ++$i) {
  62. $row = fgetcsv($handle);
  63. $this->assertEquals('token'.$i, $row[0]);
  64. $this->assertEquals('127.0.0.'.$i, $row[1]);
  65. $this->assertEquals('http://foo.bar/'.$i, $row[3]);
  66. }
  67. $this->assertFalse(fgetcsv($handle));
  68. }
  69. public function testReadLineFromFile()
  70. {
  71. $r = new \ReflectionMethod($this->storage, 'readLineFromFile');
  72. $r->setAccessible(true);
  73. $h = tmpfile();
  74. fwrite($h, "line1\n\n\nline2\n");
  75. fseek($h, 0, SEEK_END);
  76. $this->assertEquals('line2', $r->invoke($this->storage, $h));
  77. $this->assertEquals('line1', $r->invoke($this->storage, $h));
  78. }
  79. }