FileProfilerStorageTest.php 13 KB

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