SqliteProfilerStorageTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. $this->assertTrue(true === self::$storage->write($profile), '->write() returns true when the token is unique');
  64. $this->assertTrue(false === self::$storage->write($profile), '->write() return false when the token is already present in the DB');
  65. }
  66. public function testRetrieveByIp()
  67. {
  68. $profile = new Profile('token');
  69. $profile->setIp('127.0.0.1');
  70. self::$storage->write($profile);
  71. $this->assertEquals(count(self::$storage->find('127.0.0.1', '', 10)), 1, '->find() retrieve a record by IP');
  72. $this->assertEquals(count(self::$storage->find('127.0.%.1', '', 10)), 0, '->find() does not interpret a "%" as a wildcard in the IP');
  73. $this->assertEquals(count(self::$storage->find('127.0._.1', '', 10)), 0, '->find() does not interpret a "_" as a wildcard in the IP');
  74. }
  75. public function testRetrieveByUrl()
  76. {
  77. $profile = new Profile('simple_quote');
  78. $profile->setIp('127.0.0.1');
  79. $profile->setUrl('http://foo.bar/\'');
  80. self::$storage->write($profile);
  81. $profile = new Profile('double_quote');
  82. $profile->setIp('127.0.0.1');
  83. $profile->setUrl('http://foo.bar/"');
  84. self::$storage->write($profile);
  85. $profile = new Profile('backslash');
  86. $profile->setIp('127.0.0.1');
  87. $profile->setUrl('http://foo\\bar/');
  88. self::$storage->write($profile);
  89. $profile = new Profile('percent');
  90. $profile->setIp('127.0.0.1');
  91. $profile->setUrl('http://foo.bar/%');
  92. self::$storage->write($profile);
  93. $profile = new Profile('underscore');
  94. $profile->setIp('127.0.0.1');
  95. $profile->setUrl('http://foo.bar/_');
  96. self::$storage->write($profile);
  97. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/\'', 10)), 1, '->find() accepts single quotes in URLs');
  98. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/"', 10)), 1, '->find() accepts double quotes in URLs');
  99. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo\\bar/', 10)), 1, '->find() accepts backslash in URLs');
  100. $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');
  101. $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');
  102. }
  103. }