FileProfilerStorageTest.php 14 KB

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