SqliteProfilerStorageTest.php 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. class SqliteProfilerStorageTest extends \PHPUnit_Framework_TestCase
  13. {
  14. protected static $dbFile;
  15. protected static $storage;
  16. public static function setUpBeforeClass()
  17. {
  18. self::$dbFile = tempnam(sys_get_temp_dir(), 'sf2_sqlite_storage');
  19. if (file_exists(self::$dbFile)) {
  20. @unlink(self::$dbFile);
  21. }
  22. self::$storage = new SqliteProfilerStorage('sqlite:' . self::$dbFile);
  23. }
  24. public static function tearDownAfterClass()
  25. {
  26. @unlink(self::$dbFile);
  27. }
  28. protected function setUp()
  29. {
  30. self::$storage->purge();
  31. }
  32. public function testStore()
  33. {
  34. for ($i = 0; $i < 10; $i ++) {
  35. self::$storage->write('token_'.$i, '', 'data', '127.0.0.1', 'http://foo.bar', time());
  36. }
  37. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar', 20)), 10, '->write() stores data in the database');
  38. }
  39. public function testStoreSpecialCharsInUrl()
  40. {
  41. // The SQLite storage accepts special characters in URLs (Even though URLs are not
  42. // supposed to contain them)
  43. self::$storage->write('simple_quote', '', 'data', '127.0.0.1', 'http://foo.bar/\'', time());
  44. self::$storage->write('double_quote', '', 'data', '127.0.0.1', 'http://foo.bar/"', time());
  45. self::$storage->write('backslash', '', 'data', '127.0.0.1', 'http://foo.bar/\\', time());
  46. $this->assertTrue(false !== self::$storage->read('simple_quote'), '->write() accepts single quotes in URL');
  47. $this->assertTrue(false !== self::$storage->read('double_quote'), '->write() accepts double quotes in URL');
  48. $this->assertTrue(false !== self::$storage->read('backslash'), '->write() accpets backslash in URL');
  49. }
  50. public function testStoreDuplicateToken()
  51. {
  52. $this->assertTrue(true === self::$storage->write('token', '', 'data', '127.0.0.1', 'http://foo.bar', time()), '->write() returns true when the token is unique');
  53. $this->assertTrue(false === self::$storage->write('token', '', 'data', '127.0.0.1', 'http://foo.bar', time()), '->write() return false when the token is already present in the DB');
  54. }
  55. public function testRetrieveByIp()
  56. {
  57. self::$storage->write('token', '', 'data', '127.0.0.1', 'http://foo.bar', time());
  58. $this->assertEquals(count(self::$storage->find('127.0.0.1', '', 10)), 1, '->find() retrieve a record by IP');
  59. $this->assertEquals(count(self::$storage->find('127.0.%.1', '', 10)), 0, '->find() does not interpret a "%" as a wildcard in the IP');
  60. $this->assertEquals(count(self::$storage->find('127.0._.1', '', 10)), 0, '->find() does not interpret a "_" as a wildcard in the IP');
  61. }
  62. public function testRetrieveByUrl()
  63. {
  64. self::$storage->write('simple_quote', '', 'data', '127.0.0.1', 'http://foo.bar/\'', time());
  65. self::$storage->write('double_quote', '', 'data', '127.0.0.1', 'http://foo.bar/"', time());
  66. self::$storage->write('backslash', '', 'data', '127.0.0.1', 'http://foo\\bar/', time());
  67. self::$storage->write('percent', '', 'data', '127.0.0.1', 'http://foo.bar/%', time());
  68. self::$storage->write('underscore', '', 'data', '127.0.0.1', 'http://foo.bar/_', time());
  69. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/\'', 10)), 1, '->find() accepts single quotes in URLs');
  70. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/"', 10)), 1, '->find() accepts double quotes in URLs');
  71. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo\\bar/', 10)), 1, '->find() accepts backslash in URLs');
  72. $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');
  73. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/_', 10)), 1, '->find() does not interpret a "_" as a wlidcard in the URL');
  74. }
  75. }