SqliteProfilerStorageTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. if (!class_exists('SQLite3') && (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers()))) {
  32. $this->markTestSkipped('This test requires SQLite support in your environment');
  33. }
  34. self::$storage->purge();
  35. }
  36. public function testStore()
  37. {
  38. for ($i = 0; $i < 10; $i ++) {
  39. $profile = new Profile('token_'.$i);
  40. $profile->setIp('127.0.0.1');
  41. $profile->setUrl('http://foo.bar');
  42. self::$storage->write($profile);
  43. }
  44. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar', 20)), 10, '->write() stores data in the database');
  45. }
  46. public function testStoreSpecialCharsInUrl()
  47. {
  48. // The SQLite storage accepts special characters in URLs (Even though URLs are not
  49. // supposed to contain them)
  50. $profile = new Profile('simple_quote');
  51. $profile->setUrl('127.0.0.1', 'http://foo.bar/\'');
  52. self::$storage->write($profile);
  53. $this->assertTrue(false !== self::$storage->read('simple_quote'), '->write() accepts single quotes in URL');
  54. $profile = new Profile('double_quote');
  55. $profile->setUrl('127.0.0.1', 'http://foo.bar/"');
  56. self::$storage->write($profile);
  57. $this->assertTrue(false !== self::$storage->read('double_quote'), '->write() accepts double quotes in URL');
  58. $profile = new Profile('backslash');
  59. $profile->setUrl('127.0.0.1', 'http://foo.bar/\\');
  60. self::$storage->write($profile);
  61. $this->assertTrue(false !== self::$storage->read('backslash'), '->write() accpets backslash in URL');
  62. }
  63. public function testStoreDuplicateToken()
  64. {
  65. $profile = new Profile('token');
  66. $profile->setUrl('http://example.com/');
  67. $this->assertTrue(self::$storage->write($profile), '->write() returns true when the token is unique');
  68. $profile->setUrl('http://example.net/');
  69. $this->assertTrue(self::$storage->write($profile), '->write() returns true when the token is already present in the DB');
  70. $this->assertEquals('http://example.net/', self::$storage->read('token')->getUrl(), '->write() overwrites the current profile data');
  71. }
  72. public function testRetrieveByIp()
  73. {
  74. $profile = new Profile('token');
  75. $profile->setIp('127.0.0.1');
  76. self::$storage->write($profile);
  77. $this->assertEquals(count(self::$storage->find('127.0.0.1', '', 10)), 1, '->find() retrieve a record by IP');
  78. $this->assertEquals(count(self::$storage->find('127.0.%.1', '', 10)), 0, '->find() does not interpret a "%" as a wildcard in the IP');
  79. $this->assertEquals(count(self::$storage->find('127.0._.1', '', 10)), 0, '->find() does not interpret a "_" as a wildcard in the IP');
  80. }
  81. public function testRetrieveByUrl()
  82. {
  83. $profile = new Profile('simple_quote');
  84. $profile->setIp('127.0.0.1');
  85. $profile->setUrl('http://foo.bar/\'');
  86. self::$storage->write($profile);
  87. $profile = new Profile('double_quote');
  88. $profile->setIp('127.0.0.1');
  89. $profile->setUrl('http://foo.bar/"');
  90. self::$storage->write($profile);
  91. $profile = new Profile('backslash');
  92. $profile->setIp('127.0.0.1');
  93. $profile->setUrl('http://foo\\bar/');
  94. self::$storage->write($profile);
  95. $profile = new Profile('percent');
  96. $profile->setIp('127.0.0.1');
  97. $profile->setUrl('http://foo.bar/%');
  98. self::$storage->write($profile);
  99. $profile = new Profile('underscore');
  100. $profile->setIp('127.0.0.1');
  101. $profile->setUrl('http://foo.bar/_');
  102. self::$storage->write($profile);
  103. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/\'', 10)), 1, '->find() accepts single quotes in URLs');
  104. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/"', 10)), 1, '->find() accepts double quotes in URLs');
  105. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo\\bar/', 10)), 1, '->find() accepts backslash in URLs');
  106. $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');
  107. $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');
  108. }
  109. }