SQLiteProfilerStorageTest.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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(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 testRetrieveByIp()
  51. {
  52. self::$storage->write('token', 'data', '127.0.0.1', 'http://foo.bar', time());
  53. $this->assertEquals(count(self::$storage->find('127.0.0.1', '', 10)), 1, '->find() retrieve a record by IP');
  54. $this->assertEquals(count(self::$storage->find('127.0.%.1', '', 10)), 0, '->find() does not interpret a "%" as a wildcard in the IP');
  55. $this->assertEquals(count(self::$storage->find('127.0._.1', '', 10)), 0, '->find() does not interpret a "_" as a wildcard in the IP');
  56. }
  57. public function testRetrieveByUrl()
  58. {
  59. self::$storage->write('simple_quote', 'data', '127.0.0.1', 'http://foo.bar/\'', time());
  60. self::$storage->write('double_quote', 'data', '127.0.0.1', 'http://foo.bar/"', time());
  61. self::$storage->write('backslash', 'data', '127.0.0.1', 'http://foo\\bar/', time());
  62. self::$storage->write('percent', 'data', '127.0.0.1', 'http://foo.bar/%', time());
  63. self::$storage->write('underscore', 'data', '127.0.0.1', 'http://foo.bar/_', time());
  64. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/\'', 10)), 1, '->find() accepts single quotes in URLs');
  65. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/"', 10)), 1, '->find() accepts double quotes in URLs');
  66. $this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo\\bar/', 10)), 1, '->find() accepts backslash in URLs');
  67. $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');
  68. $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');
  69. }
  70. }