SqliteProfilerStorageTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\SqliteProfilerStorage;
  12. use Symfony\Component\HttpKernel\Profiler\Profile;
  13. class SqliteProfilerStorageTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected static $dbFile;
  16. protected static $storage;
  17. public static function setUpBeforeClass()
  18. {
  19. self::$dbFile = tempnam(sys_get_temp_dir(), 'sf2_sqlite_storage');
  20. if (file_exists(self::$dbFile)) {
  21. @unlink(self::$dbFile);
  22. }
  23. self::$storage = new SqliteProfilerStorage('sqlite:'.self::$dbFile);
  24. }
  25. public static function tearDownAfterClass()
  26. {
  27. @unlink(self::$dbFile);
  28. }
  29. protected function setUp()
  30. {
  31. self::$storage->purge();
  32. }
  33. public function testStore()
  34. {
  35. for ($i = 0; $i < 10; $i ++) {
  36. $profile = new Profile('token_'.$i);
  37. $profile->setIp('127.0.0.1');
  38. $profile->setUrl('http://foo.bar');
  39. self::$storage->write($profile);
  40. }
  41. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar', 20)), 10, '->write() stores data in the database');
  42. }
  43. public function testStoreSpecialCharsInUrl()
  44. {
  45. // The SQLite storage accepts special characters in URLs (Even though URLs are not
  46. // supposed to contain them)
  47. $profile = new Profile('simple_quote');
  48. $profile->setUrl('127.0.0.1', 'http://foo.bar/\'');
  49. self::$storage->write($profile);
  50. $this->assertTrue(false !== self::$storage->read('simple_quote'), '->write() accepts single quotes in URL');
  51. $profile = new Profile('double_quote');
  52. $profile->setUrl('127.0.0.1', 'http://foo.bar/"');
  53. self::$storage->write($profile);
  54. $this->assertTrue(false !== self::$storage->read('double_quote'), '->write() accepts double quotes in URL');
  55. $profile = new Profile('backslash');
  56. $profile->setUrl('127.0.0.1', 'http://foo.bar/\\');
  57. self::$storage->write($profile);
  58. $this->assertTrue(false !== self::$storage->read('backslash'), '->write() accpets backslash in URL');
  59. }
  60. public function testStoreDuplicateToken()
  61. {
  62. $profile = new Profile('token');
  63. $profile->setUrl('http://example.com/');
  64. $this->assertTrue(self::$storage->write($profile), '->write() returns true when the token is unique');
  65. $profile->setUrl('http://example.net/');
  66. $this->assertTrue(self::$storage->write($profile), '->write() returns true when the token is already present in the DB');
  67. $this->assertEquals('http://example.net/', self::$storage->read('token')->getUrl(), '->write() overwrites the current profile data');
  68. }
  69. public function testRetrieveByIp()
  70. {
  71. $profile = new Profile('token');
  72. $profile->setIp('127.0.0.1');
  73. self::$storage->write($profile);
  74. $this->assertEquals(count(self::$storage->find('127.0.0.1', '', 10)), 1, '->find() retrieve a record by IP');
  75. $this->assertEquals(count(self::$storage->find('127.0.%.1', '', 10)), 0, '->find() does not interpret a "%" as a wildcard in the IP');
  76. $this->assertEquals(count(self::$storage->find('127.0._.1', '', 10)), 0, '->find() does not interpret a "_" as a wildcard in the IP');
  77. }
  78. public function testRetrieveByUrl()
  79. {
  80. $profile = new Profile('simple_quote');
  81. $profile->setIp('127.0.0.1');
  82. $profile->setUrl('http://foo.bar/\'');
  83. self::$storage->write($profile);
  84. $profile = new Profile('double_quote');
  85. $profile->setIp('127.0.0.1');
  86. $profile->setUrl('http://foo.bar/"');
  87. self::$storage->write($profile);
  88. $profile = new Profile('backslash');
  89. $profile->setIp('127.0.0.1');
  90. $profile->setUrl('http://foo\\bar/');
  91. self::$storage->write($profile);
  92. $profile = new Profile('percent');
  93. $profile->setIp('127.0.0.1');
  94. $profile->setUrl('http://foo.bar/%');
  95. self::$storage->write($profile);
  96. $profile = new Profile('underscore');
  97. $profile->setIp('127.0.0.1');
  98. $profile->setUrl('http://foo.bar/_');
  99. self::$storage->write($profile);
  100. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/\'', 10)), 1, '->find() accepts single quotes in URLs');
  101. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/"', 10)), 1, '->find() accepts double quotes in URLs');
  102. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo\\bar/', 10)), 1, '->find() accepts backslash in URLs');
  103. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/%', 10)), 1, '->find() does not interpret a "%" as a wildcard in the URL');
  104. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/_', 10)), 1, '->find() does not interpret a "_" as a wildcard in the URL');
  105. }
  106. }