/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Tests/Profiler/AbstractProfilerStorageTest.php

https://gitlab.com/techniconline/kmc · PHP · 254 lines · 191 code · 46 blank · 17 comment · 5 complexity · ef4986a7aa1bd2f7929fea69997be132 MD5 · raw file

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