db backup prior to drupal security update
[yaffs-website] / vendor / symfony / http-kernel / Tests / Profiler / FileProfilerStorageTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\Profiler;
13
14 use Symfony\Component\HttpKernel\Profiler\FileProfilerStorage;
15 use Symfony\Component\HttpKernel\Profiler\Profile;
16
17 class FileProfilerStorageTest extends AbstractProfilerStorageTest
18 {
19     private $tmpDir;
20     private $storage;
21
22     protected function cleanDir()
23     {
24         $flags = \FilesystemIterator::SKIP_DOTS;
25         $iterator = new \RecursiveDirectoryIterator($this->tmpDir, $flags);
26         $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
27
28         foreach ($iterator as $file) {
29             if (is_file($file)) {
30                 unlink($file);
31             }
32         }
33     }
34
35     protected function setUp()
36     {
37         $this->tmpDir = sys_get_temp_dir().'/sf2_profiler_file_storage';
38         if (is_dir($this->tmpDir)) {
39             self::cleanDir();
40         }
41         $this->storage = new FileProfilerStorage('file:'.$this->tmpDir);
42         $this->storage->purge();
43     }
44
45     protected function tearDown()
46     {
47         self::cleanDir();
48     }
49
50     /**
51      * @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
52      */
53     protected function getStorage()
54     {
55         return $this->storage;
56     }
57
58     public function testMultiRowIndexFile()
59     {
60         $iteration = 3;
61         for ($i = 0; $i < $iteration; ++$i) {
62             $profile = new Profile('token'.$i);
63             $profile->setIp('127.0.0.'.$i);
64             $profile->setUrl('http://foo.bar/'.$i);
65             $storage = $this->getStorage();
66
67             $storage->write($profile);
68             $storage->write($profile);
69             $storage->write($profile);
70         }
71
72         $handle = fopen($this->tmpDir.'/index.csv', 'r');
73         for ($i = 0; $i < $iteration; ++$i) {
74             $row = fgetcsv($handle);
75             $this->assertEquals('token'.$i, $row[0]);
76             $this->assertEquals('127.0.0.'.$i, $row[1]);
77             $this->assertEquals('http://foo.bar/'.$i, $row[3]);
78         }
79         $this->assertFalse(fgetcsv($handle));
80     }
81
82     public function testReadLineFromFile()
83     {
84         $r = new \ReflectionMethod($this->storage, 'readLineFromFile');
85
86         $r->setAccessible(true);
87
88         $h = tmpfile();
89
90         fwrite($h, "line1\n\n\nline2\n");
91         fseek($h, 0, SEEK_END);
92
93         $this->assertEquals('line2', $r->invoke($this->storage, $h));
94         $this->assertEquals('line1', $r->invoke($this->storage, $h));
95     }
96 }