/vendor/symfony/tests/Symfony/Tests/Component/HttpKernel/Profiler/FileProfilerStorageTest.php

https://github.com/israelnoguera/parejas · PHP · 187 lines · 140 code · 34 blank · 13 comment · 3 complexity · b273121aaf02a9328985bf18807b05aa 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\Tests\Component\HttpKernel\Profiler;
  11. use Symfony\Component\HttpKernel\Profiler\FileProfilerStorage;
  12. use Symfony\Component\HttpKernel\Profiler\Profile;
  13. class FileProfilerStorageTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected static $tmpDir;
  16. protected static $storage;
  17. protected static function cleanDir()
  18. {
  19. $flags = \FilesystemIterator::SKIP_DOTS;
  20. $iterator = new \RecursiveDirectoryIterator(self::$tmpDir, $flags);
  21. $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
  22. foreach ($iterator as $file) {
  23. if (is_file($file)) {
  24. unlink($file);
  25. }
  26. }
  27. }
  28. public static function setUpBeforeClass()
  29. {
  30. self::$tmpDir = sys_get_temp_dir() . '/sf2_profiler_file_storage';
  31. if (is_dir(self::$tmpDir)) {
  32. self::cleanDir();
  33. }
  34. self::$storage = new FileProfilerStorage('file:'.self::$tmpDir);
  35. }
  36. public static function tearDownAfterClass()
  37. {
  38. self::cleanDir();
  39. }
  40. protected function setUp()
  41. {
  42. self::$storage->purge();
  43. }
  44. public function testStore()
  45. {
  46. for ($i = 0; $i < 10; $i ++) {
  47. $profile = new Profile('token_'.$i);
  48. $profile->setIp('127.0.0.1');
  49. $profile->setUrl('http://foo.bar');
  50. $profile->setMethod('GET');
  51. self::$storage->write($profile);
  52. }
  53. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar', 20, 'GET')), 10, '->write() stores data in the database');
  54. }
  55. public function testChildren()
  56. {
  57. $parentProfile = new Profile('token_parent');
  58. $parentProfile->setIp('127.0.0.1');
  59. $parentProfile->setUrl('http://foo.bar/parent');
  60. $childProfile = new Profile('token_child');
  61. $childProfile->setIp('127.0.0.1');
  62. $childProfile->setUrl('http://foo.bar/child');
  63. $parentProfile->addChild($childProfile);
  64. self::$storage->write($parentProfile);
  65. self::$storage->write($childProfile);
  66. // Load them from storage
  67. $parentProfile = self::$storage->read('token_parent');
  68. $childProfile = self::$storage->read('token_child');
  69. // Check child has link to parent
  70. $this->assertNotNull($childProfile->getParent());
  71. $this->assertEquals($parentProfile->getToken(), $childProfile->getParent()->getToken());
  72. // Check parent has child
  73. $children = $parentProfile->getChildren();
  74. $this->assertEquals(1, count($children));
  75. $this->assertEquals($childProfile->getToken(), $children[0]->getToken());
  76. }
  77. public function testStoreSpecialCharsInUrl()
  78. {
  79. // The SQLite storage accepts special characters in URLs (Even though URLs are not
  80. // supposed to contain them)
  81. $profile = new Profile('simple_quote');
  82. $profile->setUrl('127.0.0.1', 'http://foo.bar/\'');
  83. self::$storage->write($profile);
  84. $this->assertTrue(false !== self::$storage->read('simple_quote'), '->write() accepts single quotes in URL');
  85. $profile = new Profile('double_quote');
  86. $profile->setUrl('127.0.0.1', 'http://foo.bar/"');
  87. self::$storage->write($profile);
  88. $this->assertTrue(false !== self::$storage->read('double_quote'), '->write() accepts double quotes in URL');
  89. $profile = new Profile('backslash');
  90. $profile->setUrl('127.0.0.1', 'http://foo.bar/\\');
  91. self::$storage->write($profile);
  92. $this->assertTrue(false !== self::$storage->read('backslash'), '->write() accepts backslash in URL');
  93. $profile = new Profile('comma');
  94. $profile->setUrl('127.0.0.1', 'http://foo.bar/,');
  95. self::$storage->write($profile);
  96. $this->assertTrue(false !== self::$storage->read('comma'), '->write() accepts comma in URL');
  97. }
  98. public function testStoreDuplicateToken()
  99. {
  100. $profile = new Profile('token');
  101. $this->assertTrue(true === self::$storage->write($profile), '->write() returns true when the token is unique');
  102. $this->assertTrue(true === self::$storage->write($profile), '->write() overwrites when the token is already present in the DB');
  103. }
  104. public function testRetrieveByIp()
  105. {
  106. $profile = new Profile('token');
  107. $profile->setIp('127.0.0.1');
  108. $profile->setMethod('GET');
  109. self::$storage->write($profile);
  110. $this->assertEquals(count(self::$storage->find('127.0.0.1', '', 10, 'GET')), 1, '->find() retrieve a record by IP');
  111. $this->assertEquals(count(self::$storage->find('127.0.%.1', '', 10, 'GET')), 0, '->find() does not interpret a "%" as a wildcard in the IP');
  112. $this->assertEquals(count(self::$storage->find('127.0._.1', '', 10, 'GET')), 0, '->find() does not interpret a "_" as a wildcard in the IP');
  113. }
  114. public function testRetrieveByUrl()
  115. {
  116. $profile = new Profile('simple_quote');
  117. $profile->setIp('127.0.0.1');
  118. $profile->setUrl('http://foo.bar/\'');
  119. $profile->setMethod('GET');
  120. self::$storage->write($profile);
  121. $profile = new Profile('double_quote');
  122. $profile->setIp('127.0.0.1');
  123. $profile->setUrl('http://foo.bar/"');
  124. $profile->setMethod('GET');
  125. self::$storage->write($profile);
  126. $profile = new Profile('backslash');
  127. $profile->setIp('127.0.0.1');
  128. $profile->setUrl('http://foo\\bar/');
  129. $profile->setMethod('GET');
  130. self::$storage->write($profile);
  131. $profile = new Profile('percent');
  132. $profile->setIp('127.0.0.1');
  133. $profile->setUrl('http://foo.bar/%');
  134. $profile->setMethod('GET');
  135. self::$storage->write($profile);
  136. $profile = new Profile('underscore');
  137. $profile->setIp('127.0.0.1');
  138. $profile->setUrl('http://foo.bar/_');
  139. $profile->setMethod('GET');
  140. self::$storage->write($profile);
  141. $profile = new Profile('semicolon');
  142. $profile->setIp('127.0.0.1');
  143. $profile->setUrl('http://foo.bar/;');
  144. $profile->setMethod('GET');
  145. self::$storage->write($profile);
  146. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/\'', 10, 'GET')), 1, '->find() accepts single quotes in URLs');
  147. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/"', 10, 'GET')), 1, '->find() accepts double quotes in URLs');
  148. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo\\bar/', 10, 'GET')), 1, '->find() accepts backslash in URLs');
  149. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/;', 10, 'GET')), 1, '->find() accepts semicolon in URLs');
  150. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/%', 10, 'GET')), 1, '->find() does not interpret a "%" as a wildcard in the URL');
  151. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/_', 10, 'GET')), 1, '->find() does not interpret a "_" as a wildcard in the URL');
  152. }
  153. }