2011-01-18 14:25:10 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2011-01-18 14:25:10 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Tests\Component\HttpKernel\Profiler ;
2011-02-15 03:07:07 +00:00
use Symfony\Component\HttpKernel\Profiler\SqliteProfilerStorage ;
2011-05-30 17:54:40 +01:00
use Symfony\Component\HttpKernel\Profiler\Profile ;
2011-01-18 14:25:10 +00:00
2011-02-15 03:07:07 +00:00
class SqliteProfilerStorageTest extends \PHPUnit_Framework_TestCase
2011-01-18 14:25:10 +00:00
{
protected static $dbFile ;
protected static $storage ;
public static function setUpBeforeClass ()
{
self :: $dbFile = tempnam ( sys_get_temp_dir (), 'sf2_sqlite_storage' );
if ( file_exists ( self :: $dbFile )) {
@ unlink ( self :: $dbFile );
}
2011-03-16 14:16:21 +00:00
self :: $storage = new SqliteProfilerStorage ( 'sqlite:' . self :: $dbFile );
2011-01-18 14:25:10 +00:00
}
public static function tearDownAfterClass ()
{
@ unlink ( self :: $dbFile );
}
protected function setUp ()
{
2011-11-14 17:37:25 +00:00
if ( ! class_exists ( 'SQLite3' ) && ( ! class_exists ( 'PDO' ) || ! in_array ( 'sqlite' , \PDO :: getAvailableDrivers ()))) {
$this -> markTestSkipped ( 'This test requires SQLite support in your environment' );
}
2011-01-18 14:25:10 +00:00
self :: $storage -> purge ();
}
public function testStore ()
{
for ( $i = 0 ; $i < 10 ; $i ++ ) {
2011-05-30 17:54:40 +01:00
$profile = new Profile ( 'token_' . $i );
$profile -> setIp ( '127.0.0.1' );
$profile -> setUrl ( 'http://foo.bar' );
2011-09-24 10:20:46 +01:00
$profile -> setMethod ( 'GET' );
2011-05-30 17:54:40 +01:00
self :: $storage -> write ( $profile );
2011-01-18 14:25:10 +00:00
}
2011-09-24 10:20:46 +01:00
$this -> assertEquals ( count ( self :: $storage -> find ( '127.0.0.1' , 'http://foo.bar' , 20 , 'GET' )), 10 , '->write() stores data in the database' );
2011-01-18 14:25:10 +00:00
}
public function testStoreSpecialCharsInUrl ()
{
// The SQLite storage accepts special characters in URLs (Even though URLs are not
// supposed to contain them)
2011-05-30 17:54:40 +01:00
$profile = new Profile ( 'simple_quote' );
$profile -> setUrl ( '127.0.0.1' , 'http://foo.bar/\'' );
self :: $storage -> write ( $profile );
2011-01-18 14:25:10 +00:00
$this -> assertTrue ( false !== self :: $storage -> read ( 'simple_quote' ), '->write() accepts single quotes in URL' );
2011-05-30 17:54:40 +01:00
$profile = new Profile ( 'double_quote' );
$profile -> setUrl ( '127.0.0.1' , 'http://foo.bar/"' );
self :: $storage -> write ( $profile );
2011-01-18 14:25:10 +00:00
$this -> assertTrue ( false !== self :: $storage -> read ( 'double_quote' ), '->write() accepts double quotes in URL' );
2011-05-30 17:54:40 +01:00
$profile = new Profile ( 'backslash' );
$profile -> setUrl ( '127.0.0.1' , 'http://foo.bar/\\' );
self :: $storage -> write ( $profile );
2011-01-18 14:25:10 +00:00
$this -> assertTrue ( false !== self :: $storage -> read ( 'backslash' ), '->write() accpets backslash in URL' );
}
2011-01-18 14:41:19 +00:00
public function testStoreDuplicateToken ()
{
2011-05-30 17:54:40 +01:00
$profile = new Profile ( 'token' );
2011-10-17 01:32:06 +01:00
$profile -> setUrl ( 'http://example.com/' );
2011-05-30 17:54:40 +01:00
2011-10-17 01:32:06 +01:00
$this -> assertTrue ( self :: $storage -> write ( $profile ), '->write() returns true when the token is unique' );
$profile -> setUrl ( 'http://example.net/' );
$this -> assertTrue ( self :: $storage -> write ( $profile ), '->write() returns true when the token is already present in the DB' );
$this -> assertEquals ( 'http://example.net/' , self :: $storage -> read ( 'token' ) -> getUrl (), '->write() overwrites the current profile data' );
2011-01-18 14:41:19 +00:00
}
2011-01-18 14:25:10 +00:00
public function testRetrieveByIp ()
{
2011-05-30 17:54:40 +01:00
$profile = new Profile ( 'token' );
$profile -> setIp ( '127.0.0.1' );
2011-09-24 10:20:46 +01:00
$profile -> setMethod ( 'GET' );
2011-05-30 17:54:40 +01:00
self :: $storage -> write ( $profile );
2011-01-18 14:25:10 +00:00
2011-09-24 10:20:46 +01:00
$this -> assertEquals ( count ( self :: $storage -> find ( '127.0.0.1' , '' , 10 , 'GET' )), 1 , '->find() retrieve a record by IP' );
$this -> assertEquals ( count ( self :: $storage -> find ( '127.0.%.1' , '' , 10 , 'GET' )), 0 , '->find() does not interpret a "%" as a wildcard in the IP' );
$this -> assertEquals ( count ( self :: $storage -> find ( '127.0._.1' , '' , 10 , 'GET' )), 0 , '->find() does not interpret a "_" as a wildcard in the IP' );
2011-01-18 14:25:10 +00:00
}
public function testRetrieveByUrl ()
{
2011-05-30 17:54:40 +01:00
$profile = new Profile ( 'simple_quote' );
$profile -> setIp ( '127.0.0.1' );
$profile -> setUrl ( 'http://foo.bar/\'' );
2011-09-24 10:20:46 +01:00
$profile -> setMethod ( 'GET' );
2011-05-30 17:54:40 +01:00
self :: $storage -> write ( $profile );
$profile = new Profile ( 'double_quote' );
$profile -> setIp ( '127.0.0.1' );
$profile -> setUrl ( 'http://foo.bar/"' );
2011-09-24 10:20:46 +01:00
$profile -> setMethod ( 'GET' );
2011-05-30 17:54:40 +01:00
self :: $storage -> write ( $profile );
$profile = new Profile ( 'backslash' );
$profile -> setIp ( '127.0.0.1' );
$profile -> setUrl ( 'http://foo\\bar/' );
2011-09-24 10:20:46 +01:00
$profile -> setMethod ( 'GET' );
2011-05-30 17:54:40 +01:00
self :: $storage -> write ( $profile );
$profile = new Profile ( 'percent' );
$profile -> setIp ( '127.0.0.1' );
$profile -> setUrl ( 'http://foo.bar/%' );
2011-09-24 10:20:46 +01:00
$profile -> setMethod ( 'GET' );
2011-05-30 17:54:40 +01:00
self :: $storage -> write ( $profile );
$profile = new Profile ( 'underscore' );
$profile -> setIp ( '127.0.0.1' );
$profile -> setUrl ( 'http://foo.bar/_' );
2011-09-24 10:20:46 +01:00
$profile -> setMethod ( 'GET' );
2011-05-30 17:54:40 +01:00
self :: $storage -> write ( $profile );
2011-01-18 14:25:10 +00:00
2011-09-24 10:20:46 +01:00
$this -> assertEquals ( count ( self :: $storage -> find ( '127.0.0.1' , 'http://foo.bar/\'' , 10 , 'GET' )), 1 , '->find() accepts single quotes in URLs' );
$this -> assertEquals ( count ( self :: $storage -> find ( '127.0.0.1' , 'http://foo.bar/"' , 10 , 'GET' )), 1 , '->find() accepts double quotes in URLs' );
$this -> assertEquals ( count ( self :: $storage -> find ( '127.0.0.1' , 'http://foo\\bar/' , 10 , 'GET' )), 1 , '->find() accepts backslash in URLs' );
$this -> assertEquals ( count ( self :: $storage -> find ( '127.0.0.1' , 'http://foo.bar/%' , 10 , 'GET' )), 1 , '->find() does not interpret a "%" as a wildcard in the URL' );
$this -> assertEquals ( count ( self :: $storage -> find ( '127.0.0.1' , 'http://foo.bar/_' , 10 , 'GET' )), 1 , '->find() does not interpret a "_" as a wildcard in the URL' );
2011-01-18 14:25:10 +00:00
}
}