FileProfilerStorageTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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\Profiler\FileProfilerStorage;
  13. use Symfony\Component\HttpKernel\Profiler\Profile;
  14. class FileProfilerStorageTest extends TestCase
  15. {
  16. private $tmpDir;
  17. private $storage;
  18. protected function setUp()
  19. {
  20. $this->tmpDir = sys_get_temp_dir().'/sf2_profiler_file_storage';
  21. if (is_dir($this->tmpDir)) {
  22. self::cleanDir();
  23. }
  24. $this->storage = new FileProfilerStorage('file:'.$this->tmpDir);
  25. $this->storage->purge();
  26. }
  27. protected function tearDown()
  28. {
  29. self::cleanDir();
  30. }
  31. public function testStore()
  32. {
  33. for ($i = 0; $i < 10; ++$i) {
  34. $profile = new Profile('token_'.$i);
  35. $profile->setIp('127.0.0.1');
  36. $profile->setUrl('http://foo.bar');
  37. $profile->setMethod('GET');
  38. $this->storage->write($profile);
  39. }
  40. $this->assertCount(10, $this->storage->find('127.0.0.1', 'http://foo.bar', 20, 'GET'), '->write() stores data in the storage');
  41. }
  42. public function testChildren()
  43. {
  44. $parentProfile = new Profile('token_parent');
  45. $parentProfile->setIp('127.0.0.1');
  46. $parentProfile->setUrl('http://foo.bar/parent');
  47. $childProfile = new Profile('token_child');
  48. $childProfile->setIp('127.0.0.1');
  49. $childProfile->setUrl('http://foo.bar/child');
  50. $parentProfile->addChild($childProfile);
  51. $this->storage->write($parentProfile);
  52. $this->storage->write($childProfile);
  53. // Load them from storage
  54. $parentProfile = $this->storage->read('token_parent');
  55. $childProfile = $this->storage->read('token_child');
  56. // Check child has link to parent
  57. $this->assertNotNull($childProfile->getParent());
  58. $this->assertEquals($parentProfile->getToken(), $childProfile->getParentToken());
  59. // Check parent has child
  60. $children = $parentProfile->getChildren();
  61. $this->assertCount(1, $children);
  62. $this->assertEquals($childProfile->getToken(), $children[0]->getToken());
  63. }
  64. public function testStoreSpecialCharsInUrl()
  65. {
  66. // The storage accepts special characters in URLs (Even though URLs are not
  67. // supposed to contain them)
  68. $profile = new Profile('simple_quote');
  69. $profile->setUrl('http://foo.bar/\'');
  70. $this->storage->write($profile);
  71. $this->assertTrue(false !== $this->storage->read('simple_quote'), '->write() accepts single quotes in URL');
  72. $profile = new Profile('double_quote');
  73. $profile->setUrl('http://foo.bar/"');
  74. $this->storage->write($profile);
  75. $this->assertTrue(false !== $this->storage->read('double_quote'), '->write() accepts double quotes in URL');
  76. $profile = new Profile('backslash');
  77. $profile->setUrl('http://foo.bar/\\');
  78. $this->storage->write($profile);
  79. $this->assertTrue(false !== $this->storage->read('backslash'), '->write() accepts backslash in URL');
  80. $profile = new Profile('comma');
  81. $profile->setUrl('http://foo.bar/,');
  82. $this->storage->write($profile);
  83. $this->assertTrue(false !== $this->storage->read('comma'), '->write() accepts comma in URL');
  84. }
  85. public function testStoreDuplicateToken()
  86. {
  87. $profile = new Profile('token');
  88. $profile->setUrl('http://example.com/');
  89. $this->assertTrue($this->storage->write($profile), '->write() returns true when the token is unique');
  90. $profile->setUrl('http://example.net/');
  91. $this->assertTrue($this->storage->write($profile), '->write() returns true when the token is already present in the storage');
  92. $this->assertEquals('http://example.net/', $this->storage->read('token')->getUrl(), '->write() overwrites the current profile data');
  93. $this->assertCount(1, $this->storage->find('', '', 1000, ''), '->find() does not return the same profile twice');
  94. }
  95. public function testRetrieveByIp()
  96. {
  97. $profile = new Profile('token');
  98. $profile->setIp('127.0.0.1');
  99. $profile->setMethod('GET');
  100. $this->storage->write($profile);
  101. $this->assertCount(1, $this->storage->find('127.0.0.1', '', 10, 'GET'), '->find() retrieve a record by IP');
  102. $this->assertCount(0, $this->storage->find('127.0.%.1', '', 10, 'GET'), '->find() does not interpret a "%" as a wildcard in the IP');
  103. $this->assertCount(0, $this->storage->find('127.0._.1', '', 10, 'GET'), '->find() does not interpret a "_" as a wildcard in the IP');
  104. }
  105. public function testRetrieveByStatusCode()
  106. {
  107. $profile200 = new Profile('statuscode200');
  108. $profile200->setStatusCode(200);
  109. $this->storage->write($profile200);
  110. $profile404 = new Profile('statuscode404');
  111. $profile404->setStatusCode(404);
  112. $this->storage->write($profile404);
  113. $this->assertCount(1, $this->storage->find(null, null, 10, null, null, null, '200'), '->find() retrieve a record by Status code 200');
  114. $this->assertCount(1, $this->storage->find(null, null, 10, null, null, null, '404'), '->find() retrieve a record by Status code 404');
  115. }
  116. public function testRetrieveByUrl()
  117. {
  118. $profile = new Profile('simple_quote');
  119. $profile->setIp('127.0.0.1');
  120. $profile->setUrl('http://foo.bar/\'');
  121. $profile->setMethod('GET');
  122. $this->storage->write($profile);
  123. $profile = new Profile('double_quote');
  124. $profile->setIp('127.0.0.1');
  125. $profile->setUrl('http://foo.bar/"');
  126. $profile->setMethod('GET');
  127. $this->storage->write($profile);
  128. $profile = new Profile('backslash');
  129. $profile->setIp('127.0.0.1');
  130. $profile->setUrl('http://foo\\bar/');
  131. $profile->setMethod('GET');
  132. $this->storage->write($profile);
  133. $profile = new Profile('percent');
  134. $profile->setIp('127.0.0.1');
  135. $profile->setUrl('http://foo.bar/%');
  136. $profile->setMethod('GET');
  137. $this->storage->write($profile);
  138. $profile = new Profile('underscore');
  139. $profile->setIp('127.0.0.1');
  140. $profile->setUrl('http://foo.bar/_');
  141. $profile->setMethod('GET');
  142. $this->storage->write($profile);
  143. $profile = new Profile('semicolon');
  144. $profile->setIp('127.0.0.1');
  145. $profile->setUrl('http://foo.bar/;');
  146. $profile->setMethod('GET');
  147. $this->storage->write($profile);
  148. $this->assertCount(1, $this->storage->find('127.0.0.1', 'http://foo.bar/\'', 10, 'GET'), '->find() accepts single quotes in URLs');
  149. $this->assertCount(1, $this->storage->find('127.0.0.1', 'http://foo.bar/"', 10, 'GET'), '->find() accepts double quotes in URLs');
  150. $this->assertCount(1, $this->storage->find('127.0.0.1', 'http://foo\\bar/', 10, 'GET'), '->find() accepts backslash in URLs');
  151. $this->assertCount(1, $this->storage->find('127.0.0.1', 'http://foo.bar/;', 10, 'GET'), '->find() accepts semicolon in URLs');
  152. $this->assertCount(1, $this->storage->find('127.0.0.1', 'http://foo.bar/%', 10, 'GET'), '->find() does not interpret a "%" as a wildcard in the URL');
  153. $this->assertCount(1, $this->storage->find('127.0.0.1', 'http://foo.bar/_', 10, 'GET'), '->find() does not interpret a "_" as a wildcard in the URL');
  154. }
  155. public function testStoreTime()
  156. {
  157. $dt = new \DateTime('now');
  158. $start = $dt->getTimestamp();
  159. for ($i = 0; $i < 3; ++$i) {
  160. $dt->modify('+1 minute');
  161. $profile = new Profile('time_'.$i);
  162. $profile->setIp('127.0.0.1');
  163. $profile->setUrl('http://foo.bar');
  164. $profile->setTime($dt->getTimestamp());
  165. $profile->setMethod('GET');
  166. $this->storage->write($profile);
  167. }
  168. $records = $this->storage->find('', '', 3, 'GET', $start, time() + 3 * 60);
  169. $this->assertCount(3, $records, '->find() returns all previously added records');
  170. $this->assertEquals($records[0]['token'], 'time_2', '->find() returns records ordered by time in descendant order');
  171. $this->assertEquals($records[1]['token'], 'time_1', '->find() returns records ordered by time in descendant order');
  172. $this->assertEquals($records[2]['token'], 'time_0', '->find() returns records ordered by time in descendant order');
  173. $records = $this->storage->find('', '', 3, 'GET', $start, time() + 2 * 60);
  174. $this->assertCount(2, $records, '->find() should return only first two of the previously added records');
  175. }
  176. public function testRetrieveByEmptyUrlAndIp()
  177. {
  178. for ($i = 0; $i < 5; ++$i) {
  179. $profile = new Profile('token_'.$i);
  180. $profile->setMethod('GET');
  181. $this->storage->write($profile);
  182. }
  183. $this->assertCount(5, $this->storage->find('', '', 10, 'GET'), '->find() returns all previously added records');
  184. $this->storage->purge();
  185. }
  186. public function testRetrieveByMethodAndLimit()
  187. {
  188. foreach (array('POST', 'GET') as $method) {
  189. for ($i = 0; $i < 5; ++$i) {
  190. $profile = new Profile('token_'.$i.$method);
  191. $profile->setMethod($method);
  192. $this->storage->write($profile);
  193. }
  194. }
  195. $this->assertCount(5, $this->storage->find('', '', 5, 'POST'));
  196. $this->storage->purge();
  197. }
  198. public function testPurge()
  199. {
  200. $profile = new Profile('token1');
  201. $profile->setIp('127.0.0.1');
  202. $profile->setUrl('http://example.com/');
  203. $profile->setMethod('GET');
  204. $this->storage->write($profile);
  205. $this->assertTrue(false !== $this->storage->read('token1'));
  206. $this->assertCount(1, $this->storage->find('127.0.0.1', '', 10, 'GET'));
  207. $profile = new Profile('token2');
  208. $profile->setIp('127.0.0.1');
  209. $profile->setUrl('http://example.net/');
  210. $profile->setMethod('GET');
  211. $this->storage->write($profile);
  212. $this->assertTrue(false !== $this->storage->read('token2'));
  213. $this->assertCount(2, $this->storage->find('127.0.0.1', '', 10, 'GET'));
  214. $this->storage->purge();
  215. $this->assertEmpty($this->storage->read('token'), '->purge() removes all data stored by profiler');
  216. $this->assertCount(0, $this->storage->find('127.0.0.1', '', 10, 'GET'), '->purge() removes all items from index');
  217. }
  218. public function testDuplicates()
  219. {
  220. for ($i = 1; $i <= 5; ++$i) {
  221. $profile = new Profile('foo'.$i);
  222. $profile->setIp('127.0.0.1');
  223. $profile->setUrl('http://example.net/');
  224. $profile->setMethod('GET');
  225. ///three duplicates
  226. $this->storage->write($profile);
  227. $this->storage->write($profile);
  228. $this->storage->write($profile);
  229. }
  230. $this->assertCount(3, $this->storage->find('127.0.0.1', 'http://example.net/', 3, 'GET'), '->find() method returns incorrect number of entries');
  231. }
  232. public function testStatusCode()
  233. {
  234. $profile = new Profile('token1');
  235. $profile->setStatusCode(200);
  236. $this->storage->write($profile);
  237. $profile = new Profile('token2');
  238. $profile->setStatusCode(404);
  239. $this->storage->write($profile);
  240. $tokens = $this->storage->find('', '', 10, '');
  241. $this->assertCount(2, $tokens);
  242. $this->assertContains($tokens[0]['status_code'], array(200, 404));
  243. $this->assertContains($tokens[1]['status_code'], array(200, 404));
  244. }
  245. public function testMultiRowIndexFile()
  246. {
  247. $iteration = 3;
  248. for ($i = 0; $i < $iteration; ++$i) {
  249. $profile = new Profile('token'.$i);
  250. $profile->setIp('127.0.0.'.$i);
  251. $profile->setUrl('http://foo.bar/'.$i);
  252. $this->storage->write($profile);
  253. $this->storage->write($profile);
  254. $this->storage->write($profile);
  255. }
  256. $handle = fopen($this->tmpDir.'/index.csv', 'r');
  257. for ($i = 0; $i < $iteration; ++$i) {
  258. $row = fgetcsv($handle);
  259. $this->assertEquals('token'.$i, $row[0]);
  260. $this->assertEquals('127.0.0.'.$i, $row[1]);
  261. $this->assertEquals('http://foo.bar/'.$i, $row[3]);
  262. }
  263. $this->assertFalse(fgetcsv($handle));
  264. }
  265. public function testReadLineFromFile()
  266. {
  267. $r = new \ReflectionMethod($this->storage, 'readLineFromFile');
  268. $r->setAccessible(true);
  269. $h = tmpfile();
  270. fwrite($h, "line1\n\n\nline2\n");
  271. fseek($h, 0, SEEK_END);
  272. $this->assertEquals('line2', $r->invoke($this->storage, $h));
  273. $this->assertEquals('line1', $r->invoke($this->storage, $h));
  274. }
  275. protected function cleanDir()
  276. {
  277. $flags = \FilesystemIterator::SKIP_DOTS;
  278. $iterator = new \RecursiveDirectoryIterator($this->tmpDir, $flags);
  279. $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
  280. foreach ($iterator as $file) {
  281. if (is_file($file)) {
  282. unlink($file);
  283. }
  284. }
  285. }
  286. }