AbstractProfilerStorageTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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\Profile;
  13. abstract class AbstractProfilerStorageTest extends TestCase
  14. {
  15. public function testStore()
  16. {
  17. for ($i = 0; $i < 10; ++$i) {
  18. $profile = new Profile('token_'.$i);
  19. $profile->setIp('127.0.0.1');
  20. $profile->setUrl('http://foo.bar');
  21. $profile->setMethod('GET');
  22. $this->getStorage()->write($profile);
  23. }
  24. $this->assertCount(10, $this->getStorage()->find('127.0.0.1', 'http://foo.bar', 20, 'GET'), '->write() stores data in the storage');
  25. }
  26. public function testChildren()
  27. {
  28. $parentProfile = new Profile('token_parent');
  29. $parentProfile->setIp('127.0.0.1');
  30. $parentProfile->setUrl('http://foo.bar/parent');
  31. $childProfile = new Profile('token_child');
  32. $childProfile->setIp('127.0.0.1');
  33. $childProfile->setUrl('http://foo.bar/child');
  34. $parentProfile->addChild($childProfile);
  35. $this->getStorage()->write($parentProfile);
  36. $this->getStorage()->write($childProfile);
  37. // Load them from storage
  38. $parentProfile = $this->getStorage()->read('token_parent');
  39. $childProfile = $this->getStorage()->read('token_child');
  40. // Check child has link to parent
  41. $this->assertNotNull($childProfile->getParent());
  42. $this->assertEquals($parentProfile->getToken(), $childProfile->getParentToken());
  43. // Check parent has child
  44. $children = $parentProfile->getChildren();
  45. $this->assertCount(1, $children);
  46. $this->assertEquals($childProfile->getToken(), $children[0]->getToken());
  47. }
  48. public function testStoreSpecialCharsInUrl()
  49. {
  50. // The storage accepts special characters in URLs (Even though URLs are not
  51. // supposed to contain them)
  52. $profile = new Profile('simple_quote');
  53. $profile->setUrl('http://foo.bar/\'');
  54. $this->getStorage()->write($profile);
  55. $this->assertNotFalse($this->getStorage()->read('simple_quote'), '->write() accepts single quotes in URL');
  56. $profile = new Profile('double_quote');
  57. $profile->setUrl('http://foo.bar/"');
  58. $this->getStorage()->write($profile);
  59. $this->assertNotFalse($this->getStorage()->read('double_quote'), '->write() accepts double quotes in URL');
  60. $profile = new Profile('backslash');
  61. $profile->setUrl('http://foo.bar/\\');
  62. $this->getStorage()->write($profile);
  63. $this->assertNotFalse($this->getStorage()->read('backslash'), '->write() accepts backslash in URL');
  64. $profile = new Profile('comma');
  65. $profile->setUrl('http://foo.bar/,');
  66. $this->getStorage()->write($profile);
  67. $this->assertNotFalse($this->getStorage()->read('comma'), '->write() accepts comma in URL');
  68. }
  69. public function testStoreDuplicateToken()
  70. {
  71. $profile = new Profile('token');
  72. $profile->setUrl('http://example.com/');
  73. $this->assertTrue($this->getStorage()->write($profile), '->write() returns true when the token is unique');
  74. $profile->setUrl('http://example.net/');
  75. $this->assertTrue($this->getStorage()->write($profile), '->write() returns true when the token is already present in the storage');
  76. $this->assertEquals('http://example.net/', $this->getStorage()->read('token')->getUrl(), '->write() overwrites the current profile data');
  77. $this->assertCount(1, $this->getStorage()->find('', '', 1000, ''), '->find() does not return the same profile twice');
  78. }
  79. public function testRetrieveByIp()
  80. {
  81. $profile = new Profile('token');
  82. $profile->setIp('127.0.0.1');
  83. $profile->setMethod('GET');
  84. $this->getStorage()->write($profile);
  85. $this->assertCount(1, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'), '->find() retrieve a record by IP');
  86. $this->assertCount(0, $this->getStorage()->find('127.0.%.1', '', 10, 'GET'), '->find() does not interpret a "%" as a wildcard in the IP');
  87. $this->assertCount(0, $this->getStorage()->find('127.0._.1', '', 10, 'GET'), '->find() does not interpret a "_" as a wildcard in the IP');
  88. }
  89. public function testRetrieveByUrl()
  90. {
  91. $profile = new Profile('simple_quote');
  92. $profile->setIp('127.0.0.1');
  93. $profile->setUrl('http://foo.bar/\'');
  94. $profile->setMethod('GET');
  95. $this->getStorage()->write($profile);
  96. $profile = new Profile('double_quote');
  97. $profile->setIp('127.0.0.1');
  98. $profile->setUrl('http://foo.bar/"');
  99. $profile->setMethod('GET');
  100. $this->getStorage()->write($profile);
  101. $profile = new Profile('backslash');
  102. $profile->setIp('127.0.0.1');
  103. $profile->setUrl('http://foo\\bar/');
  104. $profile->setMethod('GET');
  105. $this->getStorage()->write($profile);
  106. $profile = new Profile('percent');
  107. $profile->setIp('127.0.0.1');
  108. $profile->setUrl('http://foo.bar/%');
  109. $profile->setMethod('GET');
  110. $this->getStorage()->write($profile);
  111. $profile = new Profile('underscore');
  112. $profile->setIp('127.0.0.1');
  113. $profile->setUrl('http://foo.bar/_');
  114. $profile->setMethod('GET');
  115. $this->getStorage()->write($profile);
  116. $profile = new Profile('semicolon');
  117. $profile->setIp('127.0.0.1');
  118. $profile->setUrl('http://foo.bar/;');
  119. $profile->setMethod('GET');
  120. $this->getStorage()->write($profile);
  121. $this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/\'', 10, 'GET'), '->find() accepts single quotes in URLs');
  122. $this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/"', 10, 'GET'), '->find() accepts double quotes in URLs');
  123. $this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo\\bar/', 10, 'GET'), '->find() accepts backslash in URLs');
  124. $this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/;', 10, 'GET'), '->find() accepts semicolon in URLs');
  125. $this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/%', 10, 'GET'), '->find() does not interpret a "%" as a wildcard in the URL');
  126. $this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/_', 10, 'GET'), '->find() does not interpret a "_" as a wildcard in the URL');
  127. }
  128. public function testStoreTime()
  129. {
  130. $dt = new \DateTime('now');
  131. $start = $dt->getTimestamp();
  132. for ($i = 0; $i < 3; ++$i) {
  133. $dt->modify('+1 minute');
  134. $profile = new Profile('time_'.$i);
  135. $profile->setIp('127.0.0.1');
  136. $profile->setUrl('http://foo.bar');
  137. $profile->setTime($dt->getTimestamp());
  138. $profile->setMethod('GET');
  139. $this->getStorage()->write($profile);
  140. }
  141. $records = $this->getStorage()->find('', '', 3, 'GET', $start, time() + 3 * 60);
  142. $this->assertCount(3, $records, '->find() returns all previously added records');
  143. $this->assertEquals($records[0]['token'], 'time_2', '->find() returns records ordered by time in descendant order');
  144. $this->assertEquals($records[1]['token'], 'time_1', '->find() returns records ordered by time in descendant order');
  145. $this->assertEquals($records[2]['token'], 'time_0', '->find() returns records ordered by time in descendant order');
  146. $records = $this->getStorage()->find('', '', 3, 'GET', $start, time() + 2 * 60);
  147. $this->assertCount(2, $records, '->find() should return only first two of the previously added records');
  148. }
  149. public function testRetrieveByEmptyUrlAndIp()
  150. {
  151. for ($i = 0; $i < 5; ++$i) {
  152. $profile = new Profile('token_'.$i);
  153. $profile->setMethod('GET');
  154. $this->getStorage()->write($profile);
  155. }
  156. $this->assertCount(5, $this->getStorage()->find('', '', 10, 'GET'), '->find() returns all previously added records');
  157. $this->getStorage()->purge();
  158. }
  159. public function testRetrieveByMethodAndLimit()
  160. {
  161. foreach (array('POST', 'GET') as $method) {
  162. for ($i = 0; $i < 5; ++$i) {
  163. $profile = new Profile('token_'.$i.$method);
  164. $profile->setMethod($method);
  165. $this->getStorage()->write($profile);
  166. }
  167. }
  168. $this->assertCount(5, $this->getStorage()->find('', '', 5, 'POST'));
  169. $this->getStorage()->purge();
  170. }
  171. public function testPurge()
  172. {
  173. $profile = new Profile('token1');
  174. $profile->setIp('127.0.0.1');
  175. $profile->setUrl('http://example.com/');
  176. $profile->setMethod('GET');
  177. $this->getStorage()->write($profile);
  178. $this->assertNotFalse($this->getStorage()->read('token1'));
  179. $this->assertCount(1, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'));
  180. $profile = new Profile('token2');
  181. $profile->setIp('127.0.0.1');
  182. $profile->setUrl('http://example.net/');
  183. $profile->setMethod('GET');
  184. $this->getStorage()->write($profile);
  185. $this->assertNotFalse($this->getStorage()->read('token2'));
  186. $this->assertCount(2, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'));
  187. $this->getStorage()->purge();
  188. $this->assertEmpty($this->getStorage()->read('token'), '->purge() removes all data stored by profiler');
  189. $this->assertCount(0, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'), '->purge() removes all items from index');
  190. }
  191. public function testDuplicates()
  192. {
  193. for ($i = 1; $i <= 5; ++$i) {
  194. $profile = new Profile('foo'.$i);
  195. $profile->setIp('127.0.0.1');
  196. $profile->setUrl('http://example.net/');
  197. $profile->setMethod('GET');
  198. ///three duplicates
  199. $this->getStorage()->write($profile);
  200. $this->getStorage()->write($profile);
  201. $this->getStorage()->write($profile);
  202. }
  203. $this->assertCount(3, $this->getStorage()->find('127.0.0.1', 'http://example.net/', 3, 'GET'), '->find() method returns incorrect number of entries');
  204. }
  205. public function testStatusCode()
  206. {
  207. $profile = new Profile('token1');
  208. $profile->setStatusCode(200);
  209. $this->getStorage()->write($profile);
  210. $profile = new Profile('token2');
  211. $profile->setStatusCode(404);
  212. $this->getStorage()->write($profile);
  213. $tokens = $this->getStorage()->find('', '', 10, '');
  214. $this->assertCount(2, $tokens);
  215. $this->assertContains($tokens[0]['status_code'], array(200, 404));
  216. $this->assertContains($tokens[1]['status_code'], array(200, 404));
  217. }
  218. /**
  219. * @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
  220. */
  221. abstract protected function getStorage();
  222. }